cron 系统调度进程,可使其在每天非高峰负荷时间段或每周、每月中不同时间段运行
at 特定时间段运行特殊的作业,或晚一些的负荷高峰时间段运行
& 在后台运行一个占用时间不长的作业
nohup 在后台运行一个命令,即时用户退出时也不受影响
1、cron和crontab
cron 调度进程,无需人工干预下自动运行
crontab 允许用户提交、编辑、删除作业
每个用户有一个crontab文件用来保存调度信息,但管理员通常会禁用这些文件,通过cron.deny、cron.allow文件来决定是否禁用
1-1 crontab的域
列1 分钟 1~59
列2 小时1~23
列3 日1~31
列4 月1~12
列5 星期0~6
列6 要运行的命令
格式: 分<>小时<>日<>月<>星期<>命令(- 表示范围 ,表示单个 *表示连续的时间段)
1-2 crontab 举例
30 21 * * * /apps/bin/cleanup.sh
45 4 1,10,22 * * /app/bin/backup.sh
59 23 * * 5 /bin/find -name "core" -exec rm {} \;
1-3 crontab命令选项
crontab -u 用户名 -e 编辑crontab文件 -l 显示内容 -r 删除crontab文件
1-4 创建crontab文件
确定EDITOR变量
编辑$HOME目录下的.profile文件,加入一行 EDITOR=vi; export EDITOR
/var/spool/cron/tabs目录中可查看所有用户的任务
2、at命令
想cron守护进程提交作业,使其在稍后的时间运行
在/etc/at.denny、at.allow文件中
格式:at [-f script] [-m -l -r] [time] [date]
-f 所要提交的脚本或命令
-l 列出当前所有等待运行的作业 或atq
-m 作业完成后给用户发mail
-r 清除作业,某些unix只接收atrm命令
time H、HH.HHMM、HH:MM或H:M (H小时 M分钟)
date 可以是月份数或日期数,而且还能识别today、tomorrow
2-1 at提交脚本
test@linux_NGIN:~> at 11:30
warning: commands will be executed using /bin/sh
at> find / -name "passwd" -print
at> <EOT> ---------- <EOT>就是ctrl+d
job 14 at 2012-01-12 11:30
若提交的是shell脚本,如下:
test@linux_NGIN:~/script> at 3.00pm tomorrow -f ./who_is
warning: commands will be executed using /bin/sh
job 16 at 2012-01-13 15:00
还可使用echo向at提交作业
test@linux_NGIN:~/script> echo find /etc -name "passwd" -print | at now + 1 minute
warning: commands will be executed using /bin/sh
job 17 at 2012-01-12 05:09
2-2 列出提交的脚本
test@linux_NGIN:~> at -l 命令atq也可实现该功能
14 2012-01-12 11:30 a test
17 2012-01-12 05:09 a test
13 2012-01-12 11:30 a test
16 2012-01-13 15:00 a test
第1行为作业标识,a 表示at,test为用户
提交作业后,都被拷贝到/var/spool/atjobs目录中,等到要求的时间运行
2-3 清除作业
atrm job 13
或 at -r [job no]
3 &命令
后台运行作业而不占据终端
格式:命令 &
3-1 向后台提交命令
linux_NGIN:/var/spool/atjobs # find /etc -name "srm.conf" -print >find.dt 2>&1 &
[1] 13106
3-2 用ps命令查看进程
linux_NGIN:/var/spool/atjobs # ps x|grep 28305
13621 pts/1 S+ 0:00 grep 28305
或
linux_NGIN:/var/spool/atjobs # ps -ef|grep 28305
root 13625 4982 0 11:09 pts/1 00:00:00 grep 28305
3-3 杀死后台进程
kill 进程id
kill -9 进程id
4 nohup命令
运行一个作业的同时,当用户退出时该作业不会结束
格式 nohup command &
同时提交多个作业(将命令写入到一个文件中,然后使用nohup进行提交),如下
test@linux_NGIN:~> cat > quarterend
cat /home/test/script/grep | /apps/bin/trials.awk |sort|lp
test@linux_NGIN:~> chmod 777 quarterend
test@linux_NGIN:~> nohup ./quarterend > qtr.out 2>&1 &
[1] 20243