Terminal application to save web server process
Is there an application that can, given the command and parameters, execute for the lifetime of the process and ping the given URL indefinitely for a certain interval?
If not, it can be done on the terminal how bash script
? I'm pretty sure it can be done via terminal, but I'm not free enough to hack it in a few minutes.
Found this post which has part of the solution minus a bit ping
. ping
works on linux indefinitely; until he is actively killed. How can I kill it from bash after two pins?
source to share
General Script
Like others have suggested, use this in pseudocode:
- execute command and save
PID
- while
PID
active, ping and sleep - Output
This results in the following script:
#!/bin/bash
# execute command, use '&' at the end to run in background
<command here> &
# store pid
pid=$!
while ps | awk '{ print $1 }' | grep $pid; do
ping <address here>
sleep <timeout here in seconds>
done
Please note that content inside <>
must be replaced with actual material. Whether it's a command or an IP address.
Break from the cycle
To answer the second question, which is cycle dependent. In the above loop, just keep track of the number of loops with a variable. To do this, add ((count++))
inside the loop. And do the following: [[ $count -eq 2 ]] && break
. Now the loop will break when we ping a second time.
Something like that:
...
while ...; do
...
((count++))
[[ $count -eq 2 ]] && break
done
ping twice
To ping only a few times, use the option -c
:
ping -c <count here> <address here>
Example:
ping -c 2 www.google.com
Use man ping
for more information.
Best practice
As hek2mgl noted in the comment below, the current solution may not be enough to fix the problem. To answer the question, the underlying problem will continue to persist. To help this problem, cron , in which a simple wget orcurlThe http request is sent periodically. This results in a fairly light script containing just one line:
#!/bin/bash
curl <address here> > /dev/null 2>&1
This script can be added as a cron job . Leave a comment if you would like more information on how to set up such a scheduled task. Special thanks to hek2mgl for analyzing the problem and suggesting a reasonable solution.
source to share
Suppose you want to start downloading with wget
, and while it is running, ping url:
wget http://example.com/large_file.tgz & #put in background
pid=$!
while kill -s 0 $pid #test if process is running
do
ping -c 1 127.0.0.1 #ping your adress once
sleep 5 #and sleep for 5 seconds
done
source to share
A nice little universal utility for this is Daemonize . Its corresponding options:
Usage: daemonize [OPTIONS] path [arg] ...
-c <dir> # Set daemon working directory to <dir>.
-E var=value # Pass environment setting to daemon. May appear multiple times.
-p <pidfile> # Save PID to <pidfile>.
-u <user> # Run daemon as user <user>. Requires invocation as root.
-l <lockfile> # Single-instance checking using lockfile <lockfile>.
Here's a run / kill example: flickd
To get more complex, you can turn your ping script into a systemd service now standard on many recent Linux.
source to share