The cron service is installed by default on Ubuntu12.04server. You can use the crontab-e command to bring up the default editor in your system to edit the crontab command format. For details, refer to the following article.
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.
Crontab command format can refer to the following article, very good: http://www.linuxidc.com/Linux/2007-09/7681.htm and http://www.linuxidc.com/Linux/2012-08/67836.htm
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. pid
- Log_file =/home/dist/monitor/watcher. log
- If[! -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 start
- Fi
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