Linux控制脚本:信号捕捉,作业控制,调整谦让度,以及计划任务

1、关于信号以及信号捕捉

  (1) $ ps  -au可以用来查看所有作业,包括暂停的和停止的,当然还有正在运行的。

      在STAT这一列表示各个作业的状态,S表示Stop,R表示Run,T表示被追踪的或停止的。当然,你可以用kill -9  pid 的方式来终止任何你想终止的作业。(ctrl+c或者ctrl+z)

  (2)信号捕捉

    trap命令可以拦截你想要观察的linux信号,如果脚本收到了trap命令所捕捉的信号,它会阻止这个信号被shell处理,直接在本地处理这个信号。

    使用格式为:trap  commands  signals。

  (3)移除捕捉:见下例

     例:cat   test.sh

    #!/bin/bash

    trap  "echo  byebye~"  EXIT (追踪退出信号)

    while [ $count   lt  5  ]

    do

      echo "loop  #$count"

      sleep 3

      count=$(($count+1))

    done

    trap   -   EXIT         (取消追踪)

    echo  " I just removed  the trap!"

    当运行该脚本./test.sh时,如果在loop内输入ctrl+c,该结束信号就会被捕捉,然后输出byebye~然后结束。若一直没有输入结束信号,则会从上到下运行,然后退出。

2、后台模式运行脚本

  (1)后台运行脚本的格式为:./test.sh  &,然后系统就会显示出类似下面的数字:   [ 1 ]     1976,其中,[ 1 ]为作业号,1976为PID。这个脚本就会在后台运行,但是关联到这个终端上(pts/0)。该终端结束,则后台运行的关联进程都会结束。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

  (2)在非控制台下运行脚本,nohup命令,格式如下:

    $  nohup  ./test1  &

    [ 1 ]  19876

    $ nohup: ignoring input and appending  output to  'nohup.out'

    由于nohup解除了终端和进程的关联,进程会丢掉STDOUT和STDERR的链接,所以nohup命令会自动将输出的消息重定向到一个名为nohup.out的文件中。如果用nohup命令运行了另外一个程序,结果会追加到nohup.out的文件中。

3、作业控制

  (1)jobs 命令可以用来查看shell当前正在处理的作业 ,jobs命令的输出结果默认会有一个“+”和“-“,其中”+“为当前默认处理的作业,”-“为下一个默认的作业,不管有多少作业,只有一个”+“和一个”-“。想要调整默认的顺序的一个方法就是jobs -l 得到PID之后,将现在前面的作业用kill命令删除即可。

  (2)重启暂停的作业

    在后台继续的命令为bg   2,其中2为作业号,在前台模式重启的命令为  fg  2,2同样为作业号。

4、调整谦让度。

  (1)nice命令

    谦让度丛-20到20,优先级丛高到低,格式为  nice  -n  10   ./test.sh  >test.out  & 

    nice命令只能调整更低的优先级运行,要想调整更高的优先级,需要权限。

  (2)renice命令,见下例

    $   ./test   > test.out  &

    [ 1 ]   29056

    $  renice  10   -p   29056  (调整优先级)

5、定时运行作业

  (1)at命令

    格式:at  -f  filename   time,如:at  -f   test   12:50,在12:50运行test,输出或错误会通过mail发送给用户。

    atq命令用来查看系统的所有作业,而atrm是用来删除作业。如 atrm  59,其中59为作业号。

  (2)cron命令

    cron时间表为:分   时    日    月   星期    command,如:  15  10  *  *  *   command。

    这里有个特殊的例子就是每个月的最后一天怎么办?格式如下:00  12  1  *  *  if [ `date  +%d  -d  tomorrow` =01 ] ;then ; command

    crontab  -l 用来查看所有的cron的作业。

    当不需要精准的时间的时候,用预配置的cron脚本目录会更方便,可以用  ls  /etc/cron.*命令来查看cron的几个基本目录,分别有:hourly、daily、monthly、和weekly,如果有需要每天都运行一次的脚本,只需要将脚本复制到daily的目录下,cron就会每天去执行这个脚本。

    anacron程序会再运行那些错过的作业,anacron程序只会处理位于cron目录下的程序。如/etc/cron.monthly

6、启动时运行

  (1)开机时运行的脚本

    一般开机运行的脚本有些发行版放在了/etc/rc#.d目录中,其中#代表运行级别,其他的版本则放在了/etc/init.d目录或/etc/init.d/rc.d目录中了。这里面是各个版本独立的开机脚本,如果要定义自己的开机脚本的话,每个Linux发行版本有专门的开机文件,可以将脚本的全路径写到这个文件中,达到独特的开机过程。

  (2)新shell启动

    每个用户的主目录都有.bash_profile文件和.bashrc文件。当开始新的登录的时候,bash  shell会运行bash_profile文件,所以可以把登陆时要运行的脚本放到该文件中。当新的shell启动的时候,bash  shell会运行.bashrc的文件,所以当你想在启动新shell的时候启动别的脚本,可以加到.bashrc这个文件中去。

posted @ 2013-07-10 15:13  风情云淡~  阅读(658)  评论(0编辑  收藏  举报