Bash script to start process, wait random, kill process, restart

Asked
Active3 hr before
Viewed126 times

6 Answers

start
90%

Amended to meet the additional random wait period before start requirement:, Can a random forest learn time series correlations? ,Please be sure to answer the question. Provide details and share your research!,This is essentially what I need to accomplish in the randomizer.sh script, in a bit of pseudocode:

In bash, $! is the PID of the last launched process, so something patterned along the lines of this should work:

mycommand &
   last_pid = $!
   sleep($RANDOM)
kill - KILL $last_pid

It's somewhat unlikely that a new process will get the same PID unless either a) the sleep time is very long or b) your machine launches a lot of short-lived processes. On Linux, PIDs are allocated cyclically with a max of 32,765, so, roughly speaking, you would have to have launched about that many processes in the sleep time to risk hitting the same PID belonging to a different process. If that's a risk, you could add a test (technically, there's a race here, but it's very unlikely to be a problem). The following seems like it would do what you want.

signal = KILL

sleep_a_while() {
   sleep $[($RANDOM % 150) + 60] m
}

while true;
do
   # Note: command launched in background:
   /path/to / applicationfile - s 111.222 .333 .444 - u username - p password &

   # Save PID of command just launched:
   last_pid = $!

   # Sleep
for
a
while:
sleep_a_while

# See
if the command is still running, and kill it and sleep more
if it is:
   if ps - p $last_pid - o comm = | grep - qs '^applicationfile$';
then
kill - $signal $last_pid 2 > /dev/null
sleep_a_while
fi

# Go back to the beginning and launch the command again
done

If there is intended to be at most one instance of applicationfile running at a time, then this race can be avoided entirely by replacing:

# See
if the command is still running, and kill it and sleep more
if it is:
   if ps - p $last_pid - o comm = | grep - qs '^applicationfile$';
then
kill - $signal $last_pid 2 > /dev/null
sleep_a_while
fi

With:

killall - q applicationfile && sleep_a_while

If this cannot be used, Keith Reynolds's variant of the test is better, since it avoids an unnecessary grep, i.e. using:

# See
if the command is still running, and kill it and sleep more
if it is:
   if ["$(ps -p $last_pid -o comm=)" = "applicationfile"];
then
kill - $signal $last_pid 2 > /dev/null
sleep_a_while
fi
load more v
88%

Avoid PID-files, crons, or anything else that tries to evaluate processes that aren't their children.,Instead you need the process that monitors your process to be the process' parent. What does this mean? It means only the process that starts your process can reliably wait for it to end. In bash, this is absolutely trivial.,If you don't want to manage the process yourself; there are some perfectly good systems out there that will act as monitor for your processes. Look into runit, for example.,A while later: any random process that starts (call it bar) takes a random PID, imagine it taking foo's old PID.

Instead you need the process that monitors your process to be the process' parent. What does this mean? It means only the process that starts your process can reliably wait for it to end. In bash, this is absolutely trivial.

until myserver;
do
   echo "Server 'myserver' crashed with exit code $?.  Respawning.." > & 2
sleep 1
done
load more v
72%

I managed to kill it using, Did the universe need the presence of matter and radiation to start expanding? ,When I hit ^C I do get a bash prompt, but the output still comes and I still see the script in the process table:, Meta Discuss the workings and policies of this site

I managed to kill it using

pkill - f restart.py

From the man page:

   -f The pattern is normally only matched against the process name.
   When - f is set, the full command line is used.
load more v
65%

How can I check to see if my game server is still running? I'll put a script in crontab, and if it's not running, I'll restart it...,There is also no way to wait for a process that is not your child. You can't hang around the schoolyard and pick up someone else's kids. ,If you just wanted to check for the existence of a process by name, use pgrep. ,By default, wait waits for all of your shell's children to exit.

   1 command &
load more v
75%

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time. ,The sleep instruction suspends the calling process for at least the specified number of seconds (the default), minutes, hours or days. ,The sleep command has also been ported to the IBM i operating system.[12] ,sleep for Unix-like systems is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX and the Single Unix Specification.[1] It first appeared in Version 4 Unix.[2]

 sleep number
load more v
40%

This script processes the text file specified on the command line with the pr command and stores the result in a temporary file. Next, it asks the user if they want to print the file. If the user types "y", then the temporary file is passed to the lpr program for printing (substitute less for lpr if there isn't a printer attached to the system.),"signals" is a list of signals to intercept and "arg" is a command to execute when one of the signals is received. For our printing script, we might handle the signal problem this way:,The trap command allows us to execute a command when our script receives a signal. It works like this:,This would seem to solve the problem, but what happens if the user types ctrl-c when the "Print file? [y/n]:" prompt appears? The script will terminate at the read command and the rm command is never executed. Clearly, we need a way to respond to signals such as SIGINT when the Ctrl-c key is typed.

echo
while
do
:
done
load more v

Other "start-undefined" queries related to "Bash script to start process, wait random, kill process, restart"