第一次使用crontab 时,会出现
no crontab for root - using an empty one
“Select a editor ......”下面有几个选项,就是叫你选择编辑器。(选vim)就可以了,我选的第三个。
但是如果你选错了,就可能会遇到点麻烦,没有办法正常编辑,crontab -e。 怎么办?
执行这个命令:select-editor (针对crontab的一个命令), 可以让你重新选一次。
分三步走:
然后执行 1 crontab -e (linux 命令行输入)
敲击键盘符 a 开始编辑如下
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
*/1 * * * * echo “Hello World.” >> /alidata/test
*/1 * * * * /alidata/tomcatMonitorNew.sh
~
~
~
~
"/tmp/crontab.D4qssN/crontab" 23L, 961C
2 ctrl+c 退出编辑
3 输入:wq 保存退出
在test 文件里面 查看执行结果如下
echo "please enter two number"“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”
继续执行一个sh脚本
echo "please enter two number" >> /alidata/result
发现执行没有效果 单独执行这个文件
提示权限不够
sudo chmod +x xxxx 把xxxx换成那个文件名
再次执行就可以了
ps:
linux下crontab每隔5分钟执行一次任务的写法
有两种写法
第一种写法是*/5,这种写法有的系统会不支持
*/5 * * * * /xxx/task.sh
第二种写法比较繁琐,但所有系统都支持:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /xx/task.sh