linux脚本控制,linux控制脚本

1.处理信号

1 SIGUP 挂起进程

2 SIGINT 终止进程 (CTRL + C)

3 SIGQUIT 停止进程

9 SIGKILL 无条件终止进程

15 SIGTERM 可能的话终止进程

17 SIGSTOP 无条件停止进程,但不是终止

18 SIGTSTP 停止或暂停进程,但不终止进程(CTRL+Z)

19 SIGCONT 继续运行停止的进程

bash shell 会忽略3 和15 ,因为这样shell才不可能意外的终止

ps -au 查看运行的进程 ,jobs 查看作业

master@master:~/shell/command$ ./test1

this is the begin

^Z

[2]+ 已停止 ./test1

master@master:~/shell/command$ jobs

[1]- 已停止 ./test1

[2]+ 已停止 ./test1

2.捕捉信息

trap command signals

master@master:~/shell/command$ ./test2

this is a test program

loop # 1

loop # 2

^Csorry I have trapped CTRL-C

loop # 3

^Csorry I have trapped CTRL-C

loop # 4

^Csorry I have trapped CTRL-C

loop # 5

^Csorry I have trapped CTRL-C

loop # 6

^Csorry I have trapped CTRL-C

loop # 7

loop # 8

loop # 9

loop # 10

this is the end of the test program

master@master:~/shell/command$ cat test2

#!/bin/bash

trap " echo 'sorry I have trapped CTRL-C'" SIGINT SIGTERM

echo this is a test program

count=1

while [ $count -le 10 ]

do

echo "loop # $count"

sleep 5

count=$[ $count + 1 ]

done

echo this is the end of the test program

捕捉脚本的退出

aster@master:~/shell/command$ ./test3

this is a test program

loop # 1

loop # 2

loop # 3

this is the end of the test program

bye

master@master:~/shell/command$ cat test3

#!/bin/bash

trap " echo bye" EXIT

echo this is a test program

count=1

while [ $count -le 3 ]

do

echo "loop # $count"

sleep 5

count=$[ $count + 1 ]

done

echo this is the end of the test program

移除捕捉

trap - 信号(EXIT)

3.在后台模式运行脚本

脚本后加 & 但是程序会绑定到终端进程中,如果终端退出了,进程也会退出。

用nohup 命令可以让进程一直在后台运行,直到结束。

4.查看作业 jobs

master@master:~/shell/command$ jobs -l

[1]- 2499 停止 ./test1

[2]+ 2512 停止 ./test1

-l 列出进程的PID以及作业号

-n 只列出上次shell发出的通知后改变了状态的作业。

-p 只列出作业的PID

-r 只列出运行中的作业

-s 只列出已停止的作业

注意:带加号的作业会被当成默认的作业,减号作业表示下一个默认作业。

5.重新启动停止的进程bg(后台) fg(前台)

master@master:~/shell/command$ bg 2

[2]+ ./test1 &

master@master:~/shell/command$ jobs

[2]+ 运行中 ./test1 &

6.调整谦让度

优先级从-20 到 +20(低优先级)

nice -n 10 ./test4 > testout &

注意; linux 会阻止普通用户调整更高的优先级

重新调整优先级

renice 10 -p 29801

7.定时运行作业 at cron

(1)at 在某个时间运行脚本

at [ -f filename ] time

at -f test5 11:39

没有屏幕会关联到该作业,默认会发送到邮件中

列出等待的作业 atq

删除等待的作业 atrm 59

(2)计划定时执行脚本 cron

时间格式:

min hour dayofmonth month dayof week command

如每个月的第一天执行

00 12 1 * * command

每周一4:15执行

15 16 * * 1 command

每个月的最后一天

00 12 1 * * * if [ `date +d -d tomorrow` = 01 ] ; then ;command

1.『*』 匹配任意时间。(1 * * * * * command 代表每月,每周,每日,每时的第一分钟都执行command)

2.『,』 列举时间点。(1,2,3 * * * * command 代表每月,每周,每日,每时的第1,2,3分钟执行command)

3.『-』 列举时间段。( 1-4 * * * * command 代表每月,每周,每日,每时的从第一分钟到第四分钟都执行command)

4.『/n』 每隔n个单位间隔。(*/5 * * * * command 代表每隔5分钟执行一次command。注意是*/5,不是/5)

crontab基本命令

crontab -e  编辑该用户的crontab,当指定crontab 不存在时新建。

crontab -l  列出该用户的crontab。

crontab -r  删除该用户的crontab。

crontab -u  指定要设定crontab的用户名称。

crontab –v 显示上一次编辑的时间(只在某些操作系统上可用)

实战

编辑crontab模式

1.使用crontab -e 命令。这种是针对一般用户指定自己的命令。

2.直接编辑/etc/crontab ,指定系统的例行性任务。

查看

master@master:/tmp$ cat test.txt

good

master@master:/tmp$ cat test.txt

crontab日志

(centos默认情况下,crontab中执行的日志写在/var/log下#ls /var/log/cron*/var/log/cron /var/log/cron.1 /var/log/cron.2 /var/log/cron.3 /var/log/cron.4)

crontab的日志默认是关闭的。因为crontab所有的错误和输出都会发送给使用用户的mail里去。这个mail不是真正的email而是指/var/spool/mail,如果你是用的root来执行命令,那么就在/var/spool/mail/root下。

1.找到/etc/rsyslog.d/50-default.conf文件;

2.找到有#cron那行,去掉#;

3.重启rsyslog

root@ubuntu:~# vi /etc/rsyslog.d/50-default.conf

# Default rules for rsyslog.

#

# For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#

# First some standard log files. Log by facility.

#

auth,authpriv.* /var/log/auth.log

*.*;auth,authpriv.none -/var/log/syslog

#cron.* /var/log/cron.log

daemon.* -/var/log/daemon.log

kern.* -/var/log/kern.log

lpr.* -/var/log/lpr.log

mail.* -/var/log/mail.log

user.* -/var/log/user.log

#

# Logging for the mail system. Split it up so that

# it is easy to write scripts to parse these files.

#

root@ubuntu:~# /etc/init.d/rsyslog restart

查看

root@ubuntu:~# cat /var/log/cron.log

Mar 6 22:07:01 ubuntu CRON[5793]: (root) CMD (php /var/www/nginx-default/test/crontab.php)

Mar 6 22:07:01 ubuntu CRON[5794]: (root) CMD (echo "good">>/tmp/test.txt)

Mar 6 22:08:01 ubuntu CRON[5814]: (root) CMD (echo "good">>/tmp/test.txt)

Mar 6 22:08:01 ubuntu CRON[5815]: (root) CMD (php /var/www/nginx-default/test/crontab.php)

Mar 6 22:08:30 ubuntu cron[5827]: (CRON) DEATH (can't lock /var/run/crond.pid, otherpid may be 4414: Resource temporarily unavailable)

Mar 6 22:09:01 ubuntu CRON[5843]: (root) CMD (php /var/www/nginx-default/test/crontab.php)

Mar 6 22:09:01 ubuntu CRON[5844]: (root) CMD (echo "good">>/tmp/test.txt)

Mar 6 22:09:01 ubuntu CRON[5845]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值