I have a process that other processes are trying to kill and if they manage to do it, how can I make sure the process will run again?
Check out inittab. Look for respawn :)
The process will be restarted whenever it terminates
Here is an example from my own inittab:
c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux
c1
is an id (is has to be unique), 2,3,4,5
are the runlevels to which this applies, respawn states what I have said earlier and the rest is a command. There you can simply add your command and it should work well.
Signal handling differs between the signals. Most signals reach the target process where a signal handler can take care of it e.g. SIGTERM which is what kill
normally sends. If kill -sigkill
(or kill -9
, they are equivalent) is used the signal never reaches the process; the process is killed as soon as it can in a way that is safe for the OS (if it is busy in a system call, it will be killed as soon as the system call is finished, as a kill of a thread running in kernel mode could possibly chrash the OS if locks are not released etc).
So there is no way at all for a process to block or recover from a SIGKILL, regardless of programming language.
Some Linux versions (like Ubuntu) prefer /etc/event.d instead of inittab. It's a similar concept, but different implementation.
You can respawn from inittab
, but you have little control of when and how the process should be restarted--you have to edit the inittab to stop respawning. Also, inittab respawning can't do anything about locked processes and processes gone wild that are consuming massive CPU or RAM resources. You will also have no notice that your process is restarting every two seconds.
You can use a process monitoring daemon to get extra capabilities and flexibility. Monitoring daemons allow you to add arbitrarily complex behaviors to your process control. Of course, if you go too far in your demands, you may wind up having to write your own daemon.
It's better to use an existing solution that meets your needs.
I have had good results with Monit. The configuration language is quite powerful and you can do fairly complex things if you need to. You can also enable a web interface that allows you to stop, start and disable processes remotely. Check out the presentation for an overview.
You could always pull a Robin Hood/Friar Tuck thing, but these days you can kill both processes at the same time.