写在前面:案例、常用、归类、解释说明。(By Jim)

Ctrl+C组合键可以生产SIGINT信号
Ctrl+Z组合键生产SIGTSTP信号,停止进程后程序仍然留在内存中,能够从停止的地方继续运行。

捕获信号

#!/bin/bash
# testing output in a background job

trap "echo Haha" SIGINT SIGTERM
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
  echo "Loop #$count"
  sleep 10
  count=$[ $count + 1 ]
done
echo "This is the end of the test program"
(捕获到信息之后,就会输出一段文字Haha,脚本不会被终止)

捕获脚本退出
除了在shell脚本中捕获信号之外,还可以在shell脚本退出时捕获它们。
要捕获shell脚本退出,只需要向trap命令添加EXIT信号:
#!/bin/bash
# testing the script exit

trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 5 ]
do
  echo "Loop #$count"
  sleep 3
  count=$[ $count + 1 ]
done
echo "This is the end of the test program"
结果:
This is a test program
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
This is the end of the test program
byebye
(在执行结束之后,捕获到,并输出byebye字样)

移除捕获
要移除捕获,使用破折号作为命令和想要恢复正常行为的信号列表:
#!/bin/bash
# testing the script

trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 5 ]
do
  echo "Loop #$count"
  sleep 3
  count=$[ $count + 1 ]
done
trap - EXIT
echo "This is the end of the test program"
(信号捕获移除后,脚本将忽略信号。但是,如果在移除捕获之前收到信号,将继续执行捕获)

test1 &
(以后台模式运行)

在不使用控制台的情况下运行脚本
有时需要从终端会话启动shell脚本,然后让脚本在结束之前以后台模式运行,即使退出终端会话也是如此。
nohup命令,使用nohup命令时,关闭会话后脚本将忽略任何终端会话发送的SIGHUP信号。

作业控制
重启、停止、终止和恢复作业的操作称为作业控制(job control)。使用作业控制可以完全控制进程在shell环境中运行的方式。

参看作业
#!/bin/bash
# testing the script

echo "This is a test program $$"
count=1
while [ $count -le 10 ]
do
  echo "Loop #$count"
  sleep 10
  count=$[ $count + 1 ]
done
echo "This is the end of the test program"

运行中进行中断和一系列操作
[root@localhost shellscript]# test1
This is a test program 30016
Loop #1
Loop #2
Loop #3
Loop #4
^Z
[1]+  Stopped                 test1
[root@localhost shellscript]# ./test1 >testout &
[2] 30026
[root@localhost shellscript]# jobs
[1]+  Stopped                 test1
[2]-  Running                 ./test1 > testout &
(通过jobs指令进行捕获)

nice命令可以在启动命令时设置它的调度优先级。

准确无误地运行
at命令
batch命令
cron表格
at -f test1 16:22(test1脚本将与16:22运行)
列出排队的作业
at -f test1 5pm
atq(将列出排队的作业)
移除作业
atrm 8(移除作业8)

batch命令不是安排脚本在预设的时间运行,而是安排脚本在系统使用率低时运行。
如果需要脚本在每天、每周或每月在同一时间运行,该怎么办呢?

Linux&shell之如何控制脚本的更多相关文章

  1. Linux shell编写端口扫描脚本

    Linux shell编写端口扫描脚本 需求: 扫描特定主机 扫描特定主机的特定端口 扫描特定网段 扫描特定网段中哪些主机开放了特定的端口 源码如下: #/bin/bash #该脚本用于对特定目标主机 ...

  2. linux shell 写swoole重启脚本

    linux shell 写swoole重启脚本 代码如下<pre>#!/bin/shkill `lsof -t -i:9501`sleep 2php /data/web/mircoweb/ ...

  3. Linux shell批量执行scp脚本工具

    转载: linux shell + expect:批量scp脚本工具             2011-09-13 15:51:06 分类: Python/Ruby 最近在准备一个部署的任务,其中有一 ...

  4. Linux shell简单创建用户脚本

    前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本    1.创建脚本文件    2.根据需求,编写脚本    3.测试执行脚本 ...

  5. LINUX SHELL 笔记 01: 脚本

    root@iZwz:~/labs# vim myfirst root@iZwz:~/labs# cat myfirst #!/bin/bash clear echo "this is my ...

  6. linux shell 之流程控制 if if else while

    (1)流程控制不可以为空: (2)if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi 条件 ...

  7. linux shell:mysql bin_log定期清理脚本

    需求: 1.自动处理mysql bin日志脚本 2.输出可读log 3.保留1周的日志 4.对所有数据库统一处理.   实现过程描述:   思路:两种方式实现 1.mysql目录通过ls获取bin日志 ...

  8. linux shell中判断bash脚本输入的参数个数

    看下面的一段程序. #!/bin/bash ]; then echo "参数个数为$#个" else echo "没有参数" fi

  9. Linux shell 批量运行jmeter脚本

    第一版,这些代码有点问题,需要继续更改 #!/bin/bash jmxpath= reportpath= timestamp=$(date +%Y%m%d_%H%M%S) echo timestamp ...

随机推荐

  1. [AngularJS] Use ng-model-options to limit $digest

    Refer: http://toddmotto.com/super-fast-angular-ng-model-options-limit-digest-cycles/ Use: <input ...

  2. 查看Linux下网卡状态或 是否连接(转)

      1) 通过mii-tool指令       [root@localhost root]# mii-tool        eth0: negotiated 100baseTx-FD, link o ...

  3. 转载:IntelliJ Idea 常用快捷键列表

    IntelliJ Idea 常用快捷键列表 (http://www.open-open.com/lib/view/open1396578860887.html) Ctrl+Shift + Enter, ...

  4. 读写Excel

    有读Excel,也有生成相同格式的Excel.需要引用Microsoft.Office.Interop.Excel.dll public string ShiPin() { //获取项目下的目录 st ...

  5. ICOMOON!强悍的WEB字体图标制造器/Web字体使用实例

    IcoMoon!一个可以通过个性化设置来创建自定义图标(字体)的生成器!也可以将SVG图片,转换成web字体 IcoMoon是一项免费的服务,通过使用不同设置使我们能够创建自定义的ICON图或ICON ...

  6. 四、分离T4引擎

         在前几篇文章中,我使用大量的篇幅来介绍T4在VisualStudio中如何使用.虽然在一定程度上可以提高我们的工作效率,但并没有实质上的改变.不过从另一方面来说,我们确实了解到了T4的强大. ...

  7. Python报错:SyntaxError: Non-ASCII character '\xe5' in file

    运行Python脚本总是报一下的错误: SyntaxError: Non-ASCII character '\xe5' in file 原因:Python默认是以ASCII作为编码方式的,如果在自己的 ...

  8. Linux svn一次增加多个文件并批量上传

    命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多个文件怎么办?下面的命令可以帮助你解决这个问题 一次 ...

  9. Linux和windows下清除svn保存的账号密码信息

    linux是什么用户登录就是什么用户的home下,如root用户就是/root,如果xiangdong就是/home/xiangdong 用Svn时会有一种需求是需要换个帐号测试一下什么的,但往往有缓 ...

  10. 认识<hr>标签,添加水平横线

    在信息展示时,有时会需要加一些用于分隔的横线,这样会使文章看起来整齐些.如下图所示: 语法: html4.01版本 <hr> xhtml1.0版本 <hr /> 注意: 1.  ...