Linux安装配置crond定时任务详解

crond联网安装:

$ yum install crontabs

 

查看crond服务是否运行:

$ ps -ef|grep crond

crond服务操作命令:

/sbin/service crond start //启动服务  
/sbin/service crond stop //关闭服务  
/sbin/service crond restart //重启服务  

/sbin/service crond reload //重新载入配置

 

设置crond开机自动启动:

查看crond是否开机自动启动:

$ chkconfig --list crond

设置crond开机自动启动:

chkconfig --level 35 crond on

 

常用方法:

  • 列出crontab文件

       $ crontab -l

  • 编辑crontab文件

       $ crontab -e

  • 删除crontab文件

       $ crontab -r

 

crontab任务配置基本格式:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

*   *  *  *  *  command

分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0代表星期天)  命令

实例:

* * * * * /home/test.sh #每1分钟执行一次test.sh脚本

*/5 * * * * /home/test.sh #每5分钟执行一次test.sh脚本

 

配置定时任务:

运行crontab -e编写一条定时任务*/5 * * * * /home/job.sh

 

crontab不执行的问题分析:

  1. 当我们手动执行脚本成功而crontab配置定时任务不能执行脚本的时候,很有可能就是执行环境的问题,例如相关路径的设置问题。解决方案:在脚本代码最前面执行 source /home/user/.bash_profile
  2. 脚本是否有可执行权限。必须保证执行脚本的用户有执行改文件的权限。
  3. 系统时间不正确。解决方案:date -s ********
  4. crond没有启动

 

 

 

你可能感兴趣的:(开发经验,解决方案)