格式要求如下:
# 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 -e
就可以编辑定时任务,一般是将任务写在脚本里,比如我这里
* * * * * /data/test/shell_test.sh
脚本里简单输出一些信息到文件里:
#!/bin/bash
echo "good afternoon!" >> /data/test/test_crond
echo "good afternoon,crontab job"
date >> /data/test/test_crond
注意:
- 定时任务脚本一定增加可执行权限!!!
chmod +x shell_test.sh
;- 注意全部是
*
的意思是每分钟执行一次,而不是第一个;- 在里执行
echo
命令是无法输出到屏幕的,而是通过mail通知,这与直接在终端打echo
指令不同。
为了使用方便,可以直接将定时任务写在xx.cron
脚本里,放在/etc/cron.d
目录下即可。
Spring里的定时任务与此类似,格式上有点区别。
参考:
http://www.cnblogs.com/hafiz/p/6155059.html
https://www.cnblogs.com/lzhp/p/6087525.html