One of the oldest is a "respawn" entry in /etc/inittab. There are a number of ways to monitor and respawn processes under UNIX/Linux. So, if your script is not running, the pgrep will fail since it will find nothing and your script will be launched. The || symbol means "do this if the previous command failed". pgrep -f makes it search the entire command line used to launch the program and not only the program name (useful because this is a python script). ![]() ![]() pgrep foo will search for a program named foo and return its process identifier. The actual command being run is pgrep which searches running processes for the string given in the command line. The official Ubuntu docs on cron are here. See here for a nice little tutorial on cron. You just created a new crontab which will be run every 5 minutes and launch your script unless it is already running. This will bring up a window of your favorite text editor.Īdd this line to the file that just opened */5 * * * * pgrep -f testing.py || nohup python /home/you/scripts/testing.py > test.out Have your cron look for your script and relaunch it if it dies.Ĭreate a new crontab by running crontab -e. You could also take a more shell oriented approach. This is my python script - #!/usr/bin/python Testing start/running, process ps ax | grep testing.pyĢ7806 pts/3 S+ 0:00 grep -color=auto testing.pyĪny idea why px ax is not showing me anything? And how do I check whether my program is running or not? I ran below sudo command to start it but I cannot see that process running behind using ps ax? sudo start testing Is this possible to do? If yes, then what's the best way to solve these kind of problem?Īfter creating the nf file like this - chdir /tekooz So I am thinking to have some sort of cron agent in bash shell script which can restart my above Python script automatically if it is killed for whatever reason. Now, it might be possible that at some stage my above Python script can die for whatever reason. In the next interval, monit automatically starts the cron daemon without the administrator interaction as shown in the syslog messages below.ĭec 5 13:06:55 (none) monit: 'crond' process is not runningĭec 5 13:06:55 (none) monit: 'crond' trying to restartĭec 5 13:06:55 (none) monit: 'crond' start: /etc/init.d/cronĭo a ps -ef to make sure the cron job is started automatically by monit, when you killed it.I am running my Python script in the background in my Ubuntu machine (12.04) like this - nohup python testing.py > test.out & To test this, let us kill the running cron daemon as shown below. With pidfile PIDFILENAME-WITHABSOLUTE-PATHįor example, to monitor the cron daemon, append the following lines to the monitrc file.įor further configurations (for example, restarting a process when it crosses a memory or process limit), refer the HOW TO MONITOR section in “man monit”.Īlso, enable syslog by uncommenting the following line in /etc/monit/monitrc.Įdit the /etc/default/monit file and enable the start up flag, which ensures that the configuration is done so that monit can start. ![]() Configure monitĪ sample process monitoring entry in the monit configuration file /etc/monit/monitrc looks like the following. If your distribution don’t have the monit package, download monit source and install it. For example, on Debian (and Ubuntu), install monit using apt-get as shown below. On Fedora, openSUSE, Debian install monit as a package from the distribution repository. We also discuss a specific example related to processes monitoring. This article provides a jumpstart guide on monit installation and configuration. Monit is an open source utility that provides several system monitoring functionality that are extremely helpful to sysadmins.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |