You are not logged in.

#1 2014-08-22 20:41:46

lykwydchykyn
Member
Registered: 2013-07-11
Posts: 91

Looping a program that forks into the background

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

#2 2014-08-22 20:45:35

cris9288
Member
Registered: 2013-01-07
Posts: 348

Re: Looping a program that forks into the background

man systemd.service

Offline

#3 2014-08-22 21:01:37

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: Looping a program that forks into the background

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

#4 2014-08-22 21:04:09

lykwydchykyn
Member
Registered: 2013-07-11
Posts: 91

Re: Looping a program that forks into the background

Thanks, I'll try these solutions.

Offline

#5 2014-08-22 21:51:33

cris9288
Member
Registered: 2013-01-07
Posts: 348

Re: Looping a program that forks into the background

emeres wrote:

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

#6 2014-08-22 22:09:34

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: Looping a program that forks into the background

cris9288 wrote:

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.

cris9288 wrote:

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

Board footer

Powered by FluxBB