You are not logged in.
I have an application that I want to run in an endless loop, so that it respawns if it's closed.
Normally this is easy to accomplish with a while loop.
The problem is that this program forks into the background after successfully launching, and there doesn't appear to be any way to change this behavior and make it block.
Other than icky things like grepping ps, is there an elegant way to monitor a program like this and respawning it on close?
Offline
man systemd.service
Offline
Something simple like this?
while :; do
program;
while [ ! -z $(pidof program) ]; do sleep 1; done;
#or
wait $(pidof program);
#or
while $(pidof program); do sleep 1; done;
done;
Running a systemd service might be excessive, even though the Restart directive would do exactly what you want, see 'man systemd.service'.
Edit: Added something more kiss.
Last edited by emeres (2014-08-22 21:16:00)
Offline
Thanks, I'll try these solutions.
Offline
Something simple like this?
while :; do program; while [ ! -z $(pidof program) ]; do sleep 1; done; #or wait $(pidof program); #or while $(pidof program); do sleep 1; done; done;
Running a systemd service might be excessive, even though the Restart directive would do exactly what you want, see 'man systemd.service'.
Edit: Added something more kiss.
I disagree with it being excessive because as you said, the Restart directive does 'exactly' what OP wants. I would estimate the added overhead to be negligible as systemd is already running and doing it's thing. A bash script would also be trivial, but i feel it might be more clunky to manage on its own.
Offline
I disagree with it being excessive because as you said, the Restart directive does 'exactly' what OP wants.
That is not an argument against excessive, I want just to point that out and am not trying to flame.
I would estimate the added overhead to be negligible as systemd is already running and doing it's thing. A bash script would also be trivial, but i feel it might be more clunky to manage on its own.
Myself, I use dozens services under systemd, but I want to keep them in check. OP did not specify what exactly he wants to do, so the simplest solution seems appropriate. Systemd is a little bit more abstract than bash scripts, ergo increased learning curve, more complex and look at the [size of] logs systemds journal creates. Again, I am not interested in arguing, just wanting to point out a few aspects. If the task is important and needs monitoring I would suggest systemd myself.
Offline