I want to have a process which will be restarted if it crashes. I googled a bit and found that an easy solution is to use daemontools.
I didn't understand how to configure it.
- What is the easiest way to acomplish such a functionality?
- How to configure it?
6 Answers
This answer applies to Ubuntu versions with Upstart (<= 14.10). Use another approach for versions with Systemd (>= 15.04).
It seems you're looking for a functionality already provided in Ubuntu with Upstart. The respawn stanza in the configuration will do exactly what you need. I would recommend against the use of a less standard way of dealing with this.
Without more details about the process you're trying to manage with it, it's hard to say what the configuration should look like. It depends on whether it forks and puts itself in the background for example. The Upstart documentation on the respawn stanza should give you more information.
Unfortunately, it's not yet possible to run User Jobs properly: cannot get upstart to run user job
Example
Let's assume we want to keep the Calculator app running, even when it's being killed with fire (signal 9).
Create a configuration file in
/etc/init/calculator.conf(based on this article):#!upstart description "Calculator" # Start job via the daemon control script. Replace "gert" with your username. exec su -l gert -c 'export DISPLAY=:0; /usr/bin/gnome-calculator' # Restart the process if it dies with a signal # or exit code not given by the 'normal exit' stanza. respawn # Give up if restart occurs 10 times in 90 seconds. respawn limit 10 90Start it by running
sudo start calculatorIt opens on your current display (
:0) and enjoy the awesomeness by seeing it restarting after closing it.Identify the process ID, e.g. by doing
ps aux | grep calculator:gert 13695 0.2 0.4 349744 16460 ? Sl 13:38 0:00 /usr/bin/gnome-calculatorKill it with fire.
sudo kill -9 13695Watch it reappearing:
gert 16059 4.6 0.4 349736 16448 ? Sl 13:40 0:00 /usr/bin/gnome-calculator
Note that this will be more elegant with the plans for the Ubuntu 13.04 plans with proper User Job support.
5An extremely simple tool that can accomplish the same effect without needing configuration is immortal:
Simply execute the command like this:
immortal <cmd>
It will run in the background and restart automatically if it exits.
immortalctl: View the status of running jobs. There is anamecolumn which will be printed here, you can use this to refer to the job with other control commands.immortalctl stop <name>: Stops monitoring the job with the given name (it will not be automatically restarted, but the current process will continue to execute)immortalctl -k <name>: Sends SIGKILL to the current process.immortalctl exit <name>: Stops monitoring the job with the given name and removes it from the list of jobs.
Startup and respawning processes are managed by "systemd" since Ubuntu 15.04, a good introduction can be found here
an example that get you started is available on
This approach supersedes older ones based on /etc/inittab and upstart.
If you don't need daemon/service/background process functionality and just want to do it in the console then create a file called autorestart.sh:
#!/bin/bash
while [ 1 ]; do some_command
doneThen ./autorestart.sh will automatically restart some_command if it exits or crashes. CTRL-C breaks the loop and quits the program cleanly.
Tested on Ubuntu 18.04.
There is a way without "Upstart". The keyword is "inittab" :-). Although that's a file not usually altered from default, it can be used to achieve the desired respawning on SYSV machines.
2ps-watcher is a tool that watches which processes are running and executes an action on defined circumstances. Let’s install it:
apt-get install ps-watcherSee the bottom of Running HHVM with fallback to PHP-FPM for details - this has worked well for me.
1