-2

So I read this How do I write a bash script to restart a process if it dies? and quickly discovered that this was not the dilemma I'm facing.

I copied the below into /etc/init/ and it appears to be working.

description "Forever my process"

start on started mountall
stop on shutdown

respawn
respawn limit 5 5

script
    export HOME="/root"

    exec my_process >> /var/log/my-process.log 2>&1
end script
Community
  • 1
  • 1
ovatsug25
  • 7,786
  • 7
  • 34
  • 48
  • 1
    So what's the problem then? – Michael Jaros Mar 11 '15 at 17:06
  • The best answer is "don't" -- any modern OS will have a process supervision system built in that doesn't require rolling your own thing in bash. (On MacOS, this is launchd; on Fedora and other very new Linuxen it's supervisord; on Ubuntu it's upstart... and there are very strong competitors you can build/install for any UNIX like djb's daemontools and its BSD-licensed clone runit). – Charles Duffy Mar 11 '15 at 17:12
  • @CharlesDuffy that seems to be what's been posted. This is an upstart script so it will be system managed – arco444 Mar 11 '15 at 17:13
  • Then again, what you're already doing _is_ "don't"; you're using... upstart, it looks like? – Charles Duffy Mar 11 '15 at 17:14
  • 1
    It's hardly a bash script doing the restarting, then, is it? – Charles Duffy Mar 11 '15 at 17:14
  • @CharlesDuffy - that is the short answer for if it dies too, I'm trying to see if the wise have any better methods to immediately restart a process once it exits gracefully. below there is a method using sleep though that isn't exactly what I'm looking for – ovatsug25 Mar 11 '15 at 17:15
  • @ovatsug25, then that's a question about writing upstart configuration files, not about bash -- `upstart` can (and should!) be configured to do the restart itself, with the bash script embedded not changing at all from how it's already written. – Charles Duffy Mar 11 '15 at 17:15
  • 1
    And to add to that, as you have it that's exactly what will happen. The `respawn` keyword is for exactly your desired behaviour – arco444 Mar 11 '15 at 17:16
  • @CharlesDuffy - I guess I learned what upstart is today then! thanks. would this be the 'modern OS process supervision system' you speak of? if it isn't, i'd gladly take a better answer or elaboration – ovatsug25 Mar 11 '15 at 17:17
  • Yes, upstart is one of the (many) available modern process supervision systems. – Charles Duffy Mar 11 '15 at 17:17

2 Answers2

2

A simple way:

while sleep 1; do 
    echo "success"
done

Seems to work fine for me.

Replace sleep 1 with the command to start your process.


edit: this is an answer for the question in the title, I'm not sure what /etc/init or the code you gave has to do with the question

dshepherd
  • 4,989
  • 4
  • 39
  • 46
  • interesting. it does sleep though and I wanted to avoid that – ovatsug25 Mar 11 '15 at 17:12
  • No no, I used `sleep 1` as a test command (just because I wanted something that wouldn't exit immediately). In an actual script you would replace that with the real command you want to run. – dshepherd Mar 11 '15 at 17:23
-3

Given that this works, I'm posting this as an answer. If there is a better way to do it, please add.

description "Forever my process"

start on started mountall
stop on shutdown

respawn
# respawn limit 5 5 - use this if you want to limit it for any reason

script
    export HOME="/root"

    exec my_process >> /var/log/my-process.log 2>&1
end script
ovatsug25
  • 7,786
  • 7
  • 34
  • 48
  • 1
    But this is also your question... I don't understand what the problem is? – arco444 Mar 11 '15 at 17:12
  • Why do you have the `restart limit` directive, unless you genuinely want to limit the restart rate (in this case, no longer restarting if there are 5 failures inside a 5-second window)? – Charles Duffy Mar 11 '15 at 17:17