写在前面:案例、常用、归类、解释说明。(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. win7 32/64bit VS2010 OpenCV 2.4.9 环境配置

    作者 : 卿笃军 因为近期人脸识别须要用到OpenCV,让我開始了OpenCV学习. OpenCV 2.4.9下载地址:http://sourceforge.net/projects/opencvli ...

  2. STL——前闭后开区间表示法和function call 操作符

    前开后闭开区间表示法[) 任何一个STL算法,都需要获得由一对迭代器(泛型指针)所标示的区间,用以表示操作范围,这一对迭代器所标示的是个所谓的前闭后开区间,以[first,last)表示,也就是说,整 ...

  3. ActiveNotifications

    The NotificationManager can tell you how many notifications your application is currently showing. T ...

  4. Composite 组合模式

      简介         <大话设计模式>一书中组合模式的定义为:将对象组合成[树]形结构以表示[部分-整体]的层次结构,组合模式使得用户对[单个对象]和对[组合对象]的使用具有一致性. ...

  5. 剪切板 复制文本 ClipboardManager

    代码 public class MainActivity extends ListActivity {     private EditText tv_info;     private Clipbo ...

  6. 【开源java游戏框架libgdx专题】-09-动画的使用

    1.Animation类介绍   Api定义:动画是由多个帧,在设定的时间间隔序列显示.比如,一个跑步的人一个动画可以通过运行时播放这些图像无限拍照他了. 功能用法:管理动画,设置随即播放模式和播放顺 ...

  7. OD: Kernel Vulnerabilities

    内核漏洞概述 内核漏洞的分类 运行在 Ring0 上的操作系统内核.设备驱动.第三方驱动能共享同一个虚拟地址空间,可以完全访问系统空间的所有内存,而不像用户态进程那样拥有独立私有的内存空间.由于内核程 ...

  8. C#中的线程二(BeginInvoke和Invoke)

    近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资料,整理如下.感谢这篇文章对我的理解Invoke和BeginInvoke的真正含义 . (一)Control的In ...

  9. VI文件编辑操作说明

    vi Hello.c (回车后就进入,按i或a键开始编辑.要退出按ESC,进入中间模式,按冒号 :后面跟命令):wq (保存并退出):q!(退出不保存)

  10. oracle批量转库工作,比较快捷的方式

    select 'create table '  || t.TABLE_NAME ||  ' as select * from ODS_ZMGLXT.'|| t.TABLE_NAME ||'; ' fr ...