Z
Z
zednight2015-02-04 10:11:42
linux
zednight, 2015-02-04 10:11:42

Infinite loop with launch control, how?

There is a certain procedure that processes records, depending on the number of records, it can be performed at very different time intervals. The procedure may crash with an error. You cannot run it in parallel, otherwise several records may start to be processed at the same time. In general, now she has a blocking mechanism with a task skip limit and it hangs in the crown. The number of records to be processed is growing quite quickly and soon the procedure will not be able to cope with this volume when started in 10 minutes. I want to put it on a continuous loop like:
while true; do ./my_scripts; sleep10; done;
But if for some reason the loop breaks, or the terminal where the script is running shuts down, or the computer reboots, then things will get even worse. How can you do something similar with launch control, and say once every 10 minutes in the cron check that the script is running?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Petrikov, 2015-02-04
@zednight

Well, I came up with something like this:

#!/bin/bash

mkdir -p "$HOME/tmp"
PIDFILE="$HOME/tmp/myprogram.pid"

if [ -e "${PIDFILE}" ] && (ps -u $(whoami) -opid= |
                           grep -P "^\s*$(cat ${PIDFILE})$" &> /dev/null); then
  echo "Already running."
  exit 99
fi

/path/to/myprogram > $HOME/tmp/myprogram.log &

echo $! > "${PIDFILE}"
chmod 644 "${PIDFILE}"

And in cron type:
In general, what you want is called watchdog, look for this word, there are enough implementations in any scripting language.

A
aardvarkx1, 2015-02-04
@aardvarkx1

Try monitor. It will monitor the process and if it needs to be restarted, it will automatically start on reboot, etc.

M
mayorovp, 2015-02-04
@mayorovp

Look at the daemon wrappers: daemon, start-stop-daemon, etc. There are many of them, but they do pretty much the same thing. Some of them are able to restore a fallen demon, for others you can check the status in the same crown.

A
Archie Kuznetsov, 2015-02-04
@Wolf4ara

For this, we have written a dog on the bash, checks whether the script (pgrep) is running, and starts it if it dies, leaving entries in the log. In general, monit is also not bad.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question