I have a custom init script and I want that script to be respawned if in case the process dies.
In RHEL/Centos 6 /etc/inittab is deprecated. So how should I ensure the process will be respawned?
On RHEL 6 to respawn a script, go to /etc/init and create a file
cd /etc/init
vi scriptFileName.conf
And add this content
start on stopped rc RUNLEVEL=[12345]
stop on runlevel [!12345]
respawn
exec /you/respawned/script.sh -your -parameters
Save file and then launch this command (without .conf of file)
start scriptFileName
Thats all!
I normally use supervisord.
clean and simple.
Here's a nice example:
[program:vmstats]
; this assumes that there's a symlink from vmstats->vmstats-VERSION
command=/apps/vmstats/jdk1.6.0_32/bin/java -Dlog4j.configuration=file:/apps/vmstats/log4j.properties -jar /apps/vmstats/vmstats-2.0.1-jar-with-dependencies.jar
numprocs=1 ; number of processes copies to start (def 1)
directory=/apps/vmstats/ ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; whether/when to restart (default: unexpected)
startsecs=3 ; number of secs prog must stay running (def. 1)
startretries=3 ; max # of serial start failures (default 3)
user=vmstats ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/var/log/vmstats/vmstats-stdout.log ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false ; emit events on stdout writes (default false)
stderr_logfile=/var/log/vmstats/vmstats-stderr.log ; stdout log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stderr_events_enabled=false ; emit events on stderr writes (default false)
you can see it here: https://github.com/Nordstrom/vmstats/blob/master/supervisord/vmstats.conf