Im having a bit of troble coming up with a script that does this monitors processes to see if they die, if they do die, restart the process
and write out to a log file that the process was restarted with the new PID and the date and time the new process was launched.
Any suggestions?
Which OS are you using?
Assuming you want to monitor a process named atd and pidof is available:
#!/bin/bash
_prg=atd
_logfile=monitor.log
while sleep 60; do # poll every minute ...
pgrep -f "$_prg" >/dev/null || { # if process not available
start atd >/dev/null 2>&1 # start it and log the operation
printf '[%s] %s started with pid %d at %s\n' \
"$( date )" "$_prg" $( pgrep -f "$_prg" ) >> "$_logfile"
}
done
If pgrep is not available, you may try some of these:
ps -C <your_program> -o pid=
The last option could be something like this (assuming your process is atd):
ps -eo pid,args= | grep '[a]td' |cut -d\ -f2
Bare in mind, that if you're starting your process in background, its pid will be available in the $! variable (so you won't need all the stuff above).