Linux的例行性工作


当你想要在某个时刻点执行某个操作,或者每星期的特定时间执行计划,而自己又怕忘记,这时候就需要用到Linux中的例行性工作了。

Linux中的任务计划主要分为两种:
1.一次性的例行性工作
2.周期性的例行工作

1.一次性的例行性工作
例如,你想要在11点时,让计算机提醒你该去吃饭了。像这种只需要进行一次的工作,就叫做一次性的例行性工作。这里需要的命令时at。
at的简单用法,
at time
at>  执行的任务
ctrl + d

以上面说的提醒吃饭为例:
[root@FLyence ~]# tty
/dev/pts/2
[root@FLyence ~]# at 11:00
at> echo -e "\n\033[31mIt's time for lunch.\033[0m" >> /dev/pts/2
at> <EOT>
job 7 at 2013-10-09 11:00

[root@FLyence ~]# tty
/dev/pts/2
[root@FLyence ~]# at 11:00
at> echo -e "\n\033[31mIt's time for lunch.\033[0m"
at> <EOT>
job 7 at 2013-10-09 11:00

首先确定要输出到的终端,为/dev/pts/2,at后面加上提醒的时间,在at>后面输入执行的语句,最后按下crtl+D提交任务。
这里要注意的是,默认情况下执行结果以邮件方式发送给任务发起者。
再如:在20分钟后关机。
[root@FLyence ~]# at now+20min
at> shutdown -h now
at> <EOT>
job 8 at 2013-10-08 21:27

查看等待中的任务:
at -l或者atq
[root@FLyence ~]# at -l
7  2013-10-09 11:00 a root
8  2013-10-08 21:27 a root
[root@FLyence ~]# atq
7  2013-10-09 11:00 a root
8  2013-10-08 21:27 a root

删除等待中的任务
atrm 任务标号 或者 at -d 任务标号
[root@FLyence ~]# atrm 7
[root@FLyence ~]# atq
8  2013-10-08 21:27 a root

当然,指定的计划时间除了上面提到的方式外,还可以使用noon, midnight, teatime(4pm)。

同时可以将任务写到文件中,再使用at -f /PATH/TO/AT_SCRIPT TIME命令来调用。
[root@FLyence ~]# echo -e "ls\ncat /etc/passwd" > at
[root@FLyence ~]# cat at
ls
cat /etc/passwd
[root@FLyence ~]# at -f /root/at now+1min
job 10 at 2013-10-08 21:25
[root@FLyence ~]#
You have new mail in /var/spool/mail/root

更多详情见请继续阅读下一页的精彩内容

Linux中利用crontab创建计划任务

Linux中用crontab例行工作安排

Linux crontab不执行问题排查

Ubuntu使用crontab定时任务

Linux计划任务(at batch crontab anacron)

  • 1
  • 2
  • 下一页

相关内容