The cron service is installed by default on ubuntu12.04 server.
You can use the crontab-e command to bring up the default editor in your system for editing.
For the crontab command format, refer to the following article, which is very good:
Http://blog.csdn.net/love__coder/article/details/6890997
The Ubuntu site also provides the following manuals:
Http://wiki.ubuntu.org.cn/CronHowto
My requirement is that the nginx service has been installed in the system. The start command is service nginx start.
Due to nginx's stability, I chose a scheduled task for checking every minute:
*/1 * * * * /home/dist/monitor/nginx_watcher.sh
*/1 indicates running once a minute
Others * indicates no settings
My script file is as follows:
File Edit Options Buffers Tools Sh-Script Help #! /bin/bash #PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games pid_file=/usr/nginx/logs/nginx.pidlog_file=/home/dist/monitor/watcher.logif [ ! -f $pid_file ]; then echo "----------------" >> $log_file echo `date` >> $log_file echo "nginx is found stopped, restaring it now" >> $log_file echo "----------------" >> $log_file service nginx startfi
Check whether the nginx process ID file exists to determine whether nginx is running. If not, call the service nginx start command.
Notes:
1. Execute crontab-e with the root permission.
2. If you do not have to worry about setting a scheduled task, run the following command to restart the cron service. In my experience, I don't have.
service cron restart
3. If you want to ensure that the path of the environment variable is correct, you can set the path of the environment variable in your own script, and then export it out.
4. Use absolute path for file paths
5. The following scheduled tasks can help us diagnose the environment variables of the cron service:
* * * * * env > /tmp/env.output