linux任务工作页,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>

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>

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>

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

2.周期性的例行性任务

周期性的例行性工作调度是由cron(crond)这个系统服务来控制的。

首先来看看crond的配置文件

1). 系统cron:/etc/crontabSHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# For details see man 4 crontabs

# Example of job definition:

# .---------------- minute (0 - 59)

# | .------------- hour (0 - 23)

# | | .---------- day of month (1 - 31)

# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...

# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * user-name command to be executed

从这个配置文件中可以看出系统cron中语法分为7段,分钟 小时 日期 月份 周几 用户 命令,各个字段的范围都有说明。

例如:3 4 * * * root cat /etc/fstab

每天的4点零3分,以root身份运行cat /etc/fstab,最后结果结果以邮件方式发送到root用户的邮箱。

下面介绍下通配符:

*:相应时间点的所有有效取值

,: 离散取值, 3,18,33,48 * * * * root cat /etc/fstab

/: 定义指定取值范围内的频率, 例如:*/3 * * * * root cat /etc/fstab 17 */3 * * * root cat /etc/fstab

-: 连续取值:17 8-20/3 * * * root cat /etc/fstab

例如:每周二和五的18点零3分,将/etc下面的文件备份到/backup/下3 18 * * 2,5 root tar -Jcf /backup/etc-`date '+%F'`.tar.xz /etc/*

2). 用户级的例行性任务

用户cron的配置文件定义在/var/spool/cron/USERNAME中。[root@FLyence ~]# crontab -e

命令:crontab -e,会自动进入该用户的cron配置文件

例如:6月3日14点20分,执行ls的命令20 14 3 6 * ls

执行结果将会以邮件的形式放松到该用户的邮箱中。

这里的语法只有6段,少了一个用户字段。

用户cron的定义:

crontab

-e: 打开cron的编辑页面同vim,会显示语法错误,保存后自动在/var/spoon/cron

-r: 移除配置文件,会删除所有任务;

-l: 列出周期性任务

-u USERNAME,管理员可以替其他用户编写cron:crontab -e -u hadoop

在配置文件中使用的命令的路径是/bin:/sbin:/usr/bin:/usr/sbin,当你用到的命令不在这个路径下的,就无法执行。此时就应该使用绝对路径。如果是在脚本中的时候,自定PATH变量。

由于例行性任务的执行结果默认是发送到用户的邮箱的。要不发邮箱的话,可以定义

MAILTO=‘’

以秒单位执行任务计划:

方法1:以30s为周期执行某任务

* * * * * /usr/bin/unison webdata

* * * * * sleep 30 && /usr/bin/unison webdata

方法2:以15s为周期执行某任务

* * * * * for i in 0 1 2; do some_job && sleep 15; done; some_job

注意:使用%时要加反斜线。例如:

1 2 3 * * touch ~/file_$(date +\%Y\%m\%d).txt

但使用单引号后也可以不再使用反斜线,例如:

1 2 3 * * touch ~/file_$(date '+%Y%m%d').txt

补充:batch

batch 命令被用来在系统平均负载达到 0.8% 以下时执行一次性的任务,用法与at一样[root@FLyence ~]# batch midnight

//在00:00之后系统平均负载达到0.8%以下的时候执行

at> sync

at> sync

at> shutdown -h now

at>

job 3 at 2012-12-28 13:20

[root@FLyence ~]# atq

3 2012-12-28 13:20 b root

[root@FLyence ~]# atrm 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值