You are not logged in.

#1 2014-05-14 19:52:50

kruuth
Member
Registered: 2014-03-23
Posts: 15

Restart binary if it dies

I've got a binary that executes once xorg is started.  Periodically it will die off.  Is there a simple method that would check to be sure that this is still running and if not, start it again?

Offline

#2 2014-05-14 20:00:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,565
Website

Re: Restart binary if it dies

You can run it in a loop.

What is the program?  Does it give a particular (or any non-zero) exit code when it quits unexpectedly?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2014-05-14 20:00:22

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: Restart binary if it dies

while :; sleep 5; if ! ps -C yourProcess; then (yourProcess &); fi; done

Offline

#4 2014-05-14 20:06:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,565
Website

Re: Restart binary if it dies

HiImTye, that's a bit more complex than needed:

while :; yourProcess; done &

would do.  Both of these, however, make actually quitting intentionally non-trivial.  This is why I asked about the exit code.  The following would be best if it will work:

while [[ $(yourProcess) -ne 0 ]]; do :; done

EDIT: I guess this depends on if the process puts anything on stdout too ...

Last edited by Trilby (2014-05-14 20:09:13)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2014-05-14 20:09:44

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: Restart binary if it dies

Trilby wrote:
while :; do yourProcess; done &

I had no idea until now this syntax would work lol

Last edited by HiImTye (2014-05-14 20:11:09)

Offline

#6 2014-05-14 20:16:11

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Restart binary if it dies

You could run it as a systemd service with 'Restart=' set to an appropriate value (see man systemd.service). This would always restart it or perhaps only when it failed, depending on the values you set.

Scott

Offline

#7 2014-05-14 22:36:35

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Restart binary if it dies

Trilby wrote:

HiImTye, that's a bit more complex than needed:

while :; yourProcess; done &

would do.  Both of these, however, make actually quitting intentionally non-trivial.  This is why I asked about the exit code.  The following would be best if it will work:

while [[ $(yourProcess) -ne 0 ]]; do :; done

EDIT: I guess this depends on if the process puts anything on stdout too ...

Of course, if you test the output, it depends on the output. If you test the exit code, it depends on the exit code:

while ! yourProcess; do :; done

Offline

#8 2014-05-14 22:40:42

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: Restart binary if it dies

Offline

#9 2014-05-15 12:21:28

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: Restart binary if it dies

If you wrote it by yourself, just fork it. If the child dies, the next fork will jump in...and so on.

Offline

#10 2014-05-16 17:09:17

kruuth
Member
Registered: 2014-03-23
Posts: 15

Re: Restart binary if it dies

It's unfortunately not mine.  One thing....I would like it to execute after I start X.

Offline

#11 2014-05-16 18:11:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,565
Website

Re: Restart binary if it dies

So ... put it in your xinitrc.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#12 2014-05-16 19:08:13

kruuth
Member
Registered: 2014-03-23
Posts: 15

Re: Restart binary if it dies

Ah ok.  I had it in the jwmrc and it would die and not restart.

Offline

Board footer

Powered by FluxBB