linux 控制脚本-处理信号~作业控制

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4564

2013/12/04 02:06


1               SIGHUP      挂起进程
2                SIGINT      终止进程
3               SIGQUIT     停止进程
9               SIGKILL       无条件终止进程
15             SIGTERM    可能的话终止进程
17             SIGSTOP     无条件停止进程,但不是终止进程
18             SIGSTP       停止或暂停进程,但不终止进程
19              SIGCONT    继续运行停止的进程
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CTRL+C    终止进程组合键 SIGINT
CTRL+Z     停止进程组合键 SIGSTP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
捕捉信号
trap 
vt.诱骗;使受限制;困住;使(水与气体等)分离
n.圈套;(对付人的)计谋;(练习射击用的)抛靶器;(捕捉动物的)夹子
vi.设陷阱;装捕捉机;设圈套
trap command signals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@aoi ~]# cat l
#!/bin/bash
trap "echo ' Sorry! I have 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
[root@aoi ~]# sh l
this is a test program
Loop #1
Loop #2
^C Sorry! I have ctrl-C
Loop #3
^C Sorry! I have ctrl-C
Loop #4
^C Sorry! I have ctrl-C
Loop #5
^C Sorry! I have ctrl-C
Loop #6
Loop #7
Loop #8
^C Sorry! I have ctrl-C
Loop #9
^C Sorry! I have ctrl-C
Loop #10
^C Sorry! I have ctrl-C
this is the end of the test program
每次使用CTRL+C组合键,脚本都会执行trap命令中指定的echo语句,而不是忽略此信号并允许shell停止该脚本。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
捕捉脚本的退出
[root@aoi ~]# cat k
#!/bin/bash
trap " echo 'bye bye'" exit
count=1
while [ $count -le 5 ]
do
echo "Loop #$count"
sleep 3
count=$[ $count+1 ]
done
[root@aoi ~]# sh k
Loop #1
^Cbye bye

[root@aoi ~]# sh k
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
bye bye
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
移除捕捉


[root@aoi ~]# cat i
#!/bin/bash
trap "echo byebye" exit
count=1
while [ $count -le 5 ]
do
echo "Loop #$count"
sleep 2
count=$[ $count+1 ]
done
trap - EXIT
echo "I just removed the trap"
[root@aoi ~]# sh i
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
I just removed the trap

[root@aoi ~]# sh i
Loop #1
^Cbyebye
一旦信号捕捉被移除了,脚本就会忽略该信号。但是如果捕捉被移除前收到信号脚本就会在trap命令中处理它
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
要在命令行界面以后台模式运行shell脚本,只要在命令后加个&就可以,可以在命令行提示符下同时启动任意多个后台作业
[root@aoi ~]# sh i &
[1] 1568
[root@aoi ~]# Loop #1
Loop #2
Loop #3
wangzi
bash: wangzi: command not found
[root@aoi ~]# Loop #4
Loop #5
I just removed the trap
wafiofd
bash: wafiofd: command not found
[1]+  Done                    sh i
[root@aoi ~]#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
退出终端时,捆绑在此终端的后台进程也会推出。
nohup命令运行了另一个命令来阻断所有发给该进程的SIGHUP
[root@aoi ~]# nohup sh i &
[1] 1596
[root@aoi ~]# nohup: ignoring input and appending output to `nohup.out'
cat nohup.out
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
I just removed the trap
[1]+  Done                    nohup sh i
由于nohup命令会从终端解除进程的关联,进程会丢掉到 STDOUT和STDERR的链接。为了保存命令产生的输出,nohup命令会自动将STDOUT和STDERR的消息重定向到一个nohup.out的文件中。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
作业控制
启动,停止,无条件终止,以及恢复作业的这些功能统称为作业控制。
[root@aoi ~]# cat ss
#!/bin/bash
echo "thi 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 test program
[root@aoi ~]# sh ss
thi is a test program 1608
Loop #1
Loop #2
^Z
[1]+  Stopped                 sh ss
[root@aoi ~]#  jobs
[1]+  Stopped                 sh ss
[root@aoi ~]# sh ss > sss &
[2] 1611
[root@aoi ~]# jobs
[1]+  Stopped                 sh ss
[2]-  Running                 sh ss > sss &

$$用来显示linux分配给该脚本的PID,然后进入循环,每次迭代都休眠5秒。
jobs命令允许查看shell当前正在处理的作业。
重启停止的作业
bg 2         2是作业号 bg是以后台形式重启,此时还可以输入其他命令。
fg 2          是前台模式重启,此时命令行提示符不会出现,知道该作业完成
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-20最高优先级  19最低优先级
nice命令允许在启动时调整一个命令的调度优先级。
nice -n 10 sh ss
[root@aoi ~]# nice -n 10 sh ss >ww &
[4] 1656
[root@aoi ~]# ps al
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0  1294     1  20   0   4116   572 n_tty_ Ss+  tty2       0:00 /sbin/ming

4     0  1296     1  20   0   4116   572 n_tty_ Ss+  tty3       0:00 /sbin/ming

4     0  1298     1  20   0   4116   568 n_tty_ Ss+  tty4       0:00 /sbin/ming

4     0  1300     1  20   0   4116   568 n_tty_ Ss+  tty5       0:00 /sbin/ming

4     0  1308     1  20   0   4116   568 n_tty_ Ss+  tty6       0:00 /sbin/ming

4     0  1382  1292  20   0 108352  1928 n_tty_ Ss+  tty1       0:00 -bash
4     0  1402  1398  20   0 108428  1908 wait   Ss   pts/0      0:00 -bash
0     0  1416  1402  20   0 145384  1560 wait   S    pts/0      0:00 su root
4     0  1417  1416  20   0 110524  2052 wait   S    pts/0      0:00 bash
0     0  1608  1417  20   0 108176  1364 signal T    pts/0      0:00 sh ss
0     0  1610  1608  20   0 100960   592 signal T    pts/0      0:00 sleep 5
4     0  1654  1417  30  10 108176  1360 wait   SN   pts/0      0:00 sh ss
0     0  1656  1417  30  10 108176  1360 wait   SN   pts/0      0:00 sh ss
0     0  1661  1656  30  10 100960   596 hrtime SN   pts/0      0:00 sleep 5
0     0  1662  1654  30  10 100960   596 hrtime SN   pts/0      0:00 sleep 5
4     0  1663  1417  20   0 108152   964 -      R+   pts/0      0:00 ps al

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
renice 命令可以改变系统上已经运行的命令优先级。
你只能对属于你的进程执行renice命令
你只能通过renice命令降低进程的优先级
root用户可以通过renice调整任何进程的优先级

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1