1.手工写入内容
新建文件夹
1 [demo@HUNDSUN ~]$ mkdir crontab
新建文件
1 [demo@HUNDSUN crontab]$ mkdir demo-cron
手工将hello!写入demo-cron文件中
1 [demo@HUNDSUN crontab]$ echo hello! >> demo-cron
查看文件内容
[demo@HUNDSUN crontab]$ cat demo-cron hello!
2.通过定时任务写入
安装定时任务模块
1 [root@HUNDSUN ~]# yum install crontabs
crontab服务操作说明
service crond start //启动服务 service crond stop //关闭服务 service crond restart //重启服务 service crond reload //重新载入配置
查看crontab运行状态
[demo@HUNDSUN crontab]$ service crond status Redirecting to /bin/systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 五 2020-06-05 14:41:49 CST; 13min ago Main PID: 630 (crond) CGroup: /system.slice/crond.service └─630 /usr/sbin/crond -n
crontab文件内容分析
[root@HUNDSUN ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- 分 minute (0 - 59),如:每分钟就是*/1 # | .------------- 时 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
添加每1分钟执行一次
[demo@HUNDSUN ~]$ crontab -e */1 * * * * echo `date` >> /home/demo/crontab/demo-cron
执行结果
[demo@HUNDSUN crontab]$ cat demo-cron ddd 2020年 06月 05日 星期五 16:44:02 CST 2020年 06月 05日 星期五 16:45:01 CST 2020年 06月 05日 星期五 16:46:01 CST 2020年 06月 05日 星期五 16:47:01 CST 2020年 06月 05日 星期五 16:48:01 CST 2020年 06月 05日 星期五 16:49:01 CST 2020年 06月 05日 星期五 16:50:01 CST 2020年 06月 05日 星期五 16:51:01 CST 2020年 06月 05日 星期五 16:52:01 CST 2020年 06月 05日 星期五 16:53:01 CST
注:
1.一个 > 是覆盖,两个>>是追加。
2.提供一个可以在线验证定时任务执行节点的网站:https://tool.lu/crontab/
浙公网安备 33010602011771号