ubuntu上使用cron执行定时任务计划

在ubuntu下,cron为自带的系统服务。

任意用户下,输入“crontab -e”即可进入任务编写。相关提示大致如下:

  1. # Edit this file to introduce tasks to be run by cron.  
  2. #  
  3. # Each task to run has to be defined through a single line  
  4. # indicating with different fields when the task will be run  
  5. # and what command to run for the task  
  6. #  
  7. # To define the time you can provide concrete values for  
  8. # minute (m), hour (h), day of month (dom), month (mon),  
  9. # and day of week (dow) or use ’*’ in these fields (for ‘any’).#  
  10. # Notice that tasks will be started based on the cron’s system  
  11. # daemon’s notion of time and timezones.  
  12. #  
  13. # Output of the crontab jobs (including errors) is sent through  
  14. # email to the user the crontab file belongs to (unless redirected).  
  15. #  
  16. # For example, you can run a backup of all your user accounts  
  17. # at 5 a.m every week with:  
  18. # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/  
  19. #  
  20. # For more information see the manual pages of crontab(5) and cron(8)  
  21. #  
  22. # m h  dom mon dow   command  
# 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) # # m h dom mon dow command

如果是第一次执行“crontab -e”,会提示选择编辑器,选择合适的编辑器即可。我选择的是“4”,vim。

  1. Select an editor.  To change later, run ‘select-editor’.  
  2.   1. /bin/ed  
  3.   2. /bin/nano        <—- easiest  
  4.   3. /usr/bin/vim.basic  
  5.   4. /usr/bin/vim.tiny  
  6.   
  7. Choose 1-4 [2]:   
Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]: 


然后,在最后面添加下面内容,然后保存并退出。

  1. # test  
  2. 3 * * * * date >>/tmp/test.txt  
# test
3 * * * * date >>/tmp/test.txt

至于上面这个表达式的说明,请各看官自行度娘或谷歌。

好了,去看看“/tmp/test.txt”是否有内容吧。


下面是我期间遇到的几个问题。

1.输入“crontab -e”,提示“no crontab for root - using an empty one”

处理:这个是正常的,提示你,当前设定的用户,没有cron任务。

2.输入“crontab -e”,输入内容后,退出时,提示保存;保存后,任务不能成功添加。

处理:你选择的编辑器有问题。请在root角色下输入“select-editor”,可以重新进行选择。

3.编辑完任务并保存后,重启服务的问题。

有些网文说是“service crond restart”进行服务重启,在这里,需要特别注意的是,在ubuntu下cron服务的重启应该是“service cron restart ”。注意,是“cron”,而不是“crond”。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值