I have a queue script that finds new jobs from the db every second and handles them. Sometimes it doesn't do anything for 10 hours, and sometimes it receives 1000 new jobs in 3 min. The queue works fine, mostly.
It needs restarting though. (I'm not entirely sure why. I think other services the jobs talk to don't like connections being open for a long time. Restarting the queue, resets all connections. Maybe that's not why.) And sometimes the queue script just dies. Maybe memory error, I can't pinpoint it.
There's 2 ways to restart that I'm both fine with, (but it has to be automatic):
- Explicitly restart it every 24h:
ctrl C
+./queue.sh
- Wait until it dies, and start it again
I'm not sure about either… The queue runs in a screen
so I can follow output when I want. How can one command listen for another command to finish and restart it, without being a daemon?
I can't install anything. It's a crappy Redhat server that I don't have decent admin access to.
I've thought about creating a cronjob that fires every 24h and kills itself after 24h, but that just sounds so wrong… I can't use the cronjob for the queue, because new jobs must be executed almost-instantly.
Best Answer
You can run
./queue.sh
as a child of a monitor script, like this:How it works:
The monitor script runs a
./queue.sh
process in background (line 3) and a subprocess that sleeps for one day and kills./queue.sh
(line 4). Then, it waits./queue.sh
to finish (line 5).If
./queue.sh
ends prematurely, the monitor kills thesleep
process (line 6), so it will not kill an innocent process with the same PID in the future. Thewait
command at line 7 prevents thesleep
process from being a zombie.If
./queue.sh
execution lasts for more than 24 hours, it is forcefully finished when thesleep
process ends (line 4).