linux-crond

October 17, 2018

1.写定时任务要执行的脚本

如:test.sh

   vim test.sh
   echo "hello world"

2.写crontab 表达式

  • 查看表达式书写
cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# 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
  • 命令使用

crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
crontab -l //列出某个用户cron服务的详细内容
crontab -r //删除没个用户的cron服务
crontab -e //编辑某个用户的cron服务

如:

crontab -e 进入编辑页面

0 03 * * * /bin/bash test.sh

每天凌晨三点执行这个脚本

  • 服务使用命令
  • 帮助
$ service crond
用法:/etc/init.d/crond {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查询运行状态

LRF 记录学习、生活的点滴