码迷,mamicode.com
首页 > 系统相关 > 详细

Ubuntu 14.04 定时任务

时间:2018-10-13 02:26:11      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:errors   erro   保存   apt   job   time   重启   计划任务   single   

如何在Ubuntu上启动一个定时任务,使得可以定时删除机器上的日志

首先,

#查看cron状态
service cron status
 
如果提示没有安装
#安装cron服务
apt-get install cron
 
如果提示正在运行
#编辑当前用户的计划任务文件
crontab -e 
 
弹出编辑界面,如下文件,加入最后一行代码,意思是每两分钟向/home/cron_test文件写入一次Hello World
# 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
*/2 * * * * echo "Hello World" >> /home/cron_test 

  

CTRL+x 退出,此时提示是否保存,输入yes,然后回车

#重载cron
sudo service cron reload
 
#显示当前用户的计划任务文件
crontab -l 
此时会弹出当前用户的计划任务的文件内容
 
#重启cron服务
sudo service cron restart
 
然后tail -f /home/cron_test,将会每两分钟显示一次Hello World
 
结束~~

Ubuntu 14.04 定时任务

标签:errors   erro   保存   apt   job   time   重启   计划任务   single   

原文地址:https://www.cnblogs.com/sea-stream/p/9780928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!