All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Kill and restart a process everyday
Hello.
I have a python script that needs to be kept running consistently, sometimes will crash and not deployable as a system service. In order to keep it running for as much time as possible, I have deployed a cron that restarts it unconditionally at 2:01 am every day, no matter if it's crashed or not.
The python script is placed at /usr/bin/(scriptprocessname) and is executable.
Currently my crontab (on ubuntu) is set as followed:
1 2 * * * /etc/restart.sh
/etc/restart.sh is executable, and is as follows:
#!/bin/sh killall (scriptprocessname) nohup (scriptprocessname) -(arguments) 2>1 &
with "(scriptprocessname)" as the name of process and "-(arguments)" as the arguments.
However, there is a problem. As according to the log of cron, if the python script has already crashed before the cronjob being executed, /etc/restart.sh will return "killall: no such process" and stuck there, without continuing on to start the python script again.
I have very little experience in bash scripting. I wish someone could provide an effective way to kill&restart a process no matter it is currently running or not. Thanks.
Comments
I don't see why you would unconditionally kill and restart it, and just once a day at that, if the aim is to ensure that it just runs always. And what if it crashes at 03am, the next 23 hours you are left without your script running?
Something along the lines of:
will check every 5 minutes and start it if it's not running.
Use Supervisord http://supervisord.org/installing.html
You can setup the script to automatically restart if it crashes and you could always add in a mail notification upon restart.
@rm_ @MarkTurner
Sorry I forgot to mention another purpose.
This script reads from a configuration file, and sometimes the config will be updated on a day sometime just before 2 am, so I do wish to get it restarted at 2 am daily. The script itself just forwards some commands to a target server, and does not carry data in the RAM, so I also with to restart it at 2 am anyway.
I might just cron a kill at 2:01 am everyday, and leave another cronjob as introduced by @rm_ to pick it up a little later.
Thank you.
So setup up supervisord to monitor the config file.
Use monit with a pid file. It will restart your script within 1 minute of dying.
Run something like that every minute from cron, if it's running it'll do nothing but return $service is running (or remove that if you want and just put exit instead) and if it's not running it'll reboot it.
Worth noting that with most of the "proper" services you should be able to just blindly execute "start", and it won't launch dozens of copies of them, it'll check if the service is already running and refuse to start it again if it is. And this will be done very quickly and with not a lot of extra load, launching those two greps is almost slower than that:
But in this thread we're talking about just a raw script that needs to be running, not a service wrapped into a real init.d script.