您的位置:首页 > 运维架构 > Linux

「学习笔记——Linux」Linux下如何在特定时间执行特定任务

2013-02-18 13:59 501 查看

Linux下如何在特定时间执行特定任务

1 仅执行一次的任务
2 循环执行的任务
3 可唤醒停机期间的工作任务

1 仅执行一次的任务

所需程序

atd服务
at命令

启动atd
# service atd restart
atd stop/waiting
atd start/running, process 11031

千万注意要在root下执行,不然会提示Rejected send message.

at的使用

使用at的权限(因为安全性考虑最好设置)

在/etc/at.allow中设置可以使用at的账号
在/etc/at.deny中设置不可以使用at的账号

示例

1分钟后执行mkdir
$ at now + 1 minutes
warning: commands will be executed using /bin/sh
at> mkdir helloAt
at> <EOT>
job 1 at Wed Feb  6 14:36:00 2013


查看工作
# at -c 2
#!/bin/sh
# atrun uid=0 gid=0
# ...
$ mkdir helloAt


特定时间关机
$ at 14:00 2013-2-9
warning: commands will be executed using /bin/sh
at> /bin/sync
at> /bin/sync
at> /sbin/shutdown -h now
at> <EOT>
job 4 at Sat Feb  9 14:00:00 2013


at如何执行

输入输出:标准输入输出会重定向到mailbox里,所以执行echo "hello"在终端下是看不到的
后台工作:系统会将at工作独立于shell,交给atd程序接管,即使关了shell也没关系

查询与删除at中的工作

查询:atq
删除:atrm jobid

batch:和at相似,只是在cpu负载低时才执行

2 循环执行的任务

所需程序

crontab

权限限制

/etc/cron.allow
/etc/cron.deny

示例

$ crontab -e #添加任务

进入编辑环境,使用vim编辑
格式:# m h dom mon dow command

m:minute, h:hour, dom:day of month
mon:month, dow:day of week

任务示例:0 5 * * 1 tar -zcf var/backups/home.tgz /home

*表示任何时间

$ crontab -l #查询任务
$ crontab -r #删除任务

3 可唤醒停机期间的工作任务

anacron:crontab任务因为关机没有执行,anacron可以在开机时检测没有执行的crontab任务,都执行一遍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: