如何在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
 
结束~~
05-11 16:11