Linux&shell之如何控制脚本
写在前面:案例、常用、归类、解释说明。(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之如何控制脚本的更多相关文章
- Linux shell编写端口扫描脚本
Linux shell编写端口扫描脚本 需求: 扫描特定主机 扫描特定主机的特定端口 扫描特定网段 扫描特定网段中哪些主机开放了特定的端口 源码如下: #/bin/bash #该脚本用于对特定目标主机 ...
- linux shell 写swoole重启脚本
linux shell 写swoole重启脚本 代码如下<pre>#!/bin/shkill `lsof -t -i:9501`sleep 2php /data/web/mircoweb/ ...
- Linux shell批量执行scp脚本工具
转载: linux shell + expect:批量scp脚本工具 2011-09-13 15:51:06 分类: Python/Ruby 最近在准备一个部署的任务,其中有一 ...
- Linux shell简单创建用户脚本
前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本 1.创建脚本文件 2.根据需求,编写脚本 3.测试执行脚本 ...
- LINUX SHELL 笔记 01: 脚本
root@iZwz:~/labs# vim myfirst root@iZwz:~/labs# cat myfirst #!/bin/bash clear echo "this is my ...
- linux shell 之流程控制 if if else while
(1)流程控制不可以为空: (2)if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi 条件 ...
- linux shell:mysql bin_log定期清理脚本
需求: 1.自动处理mysql bin日志脚本 2.输出可读log 3.保留1周的日志 4.对所有数据库统一处理. 实现过程描述: 思路:两种方式实现 1.mysql目录通过ls获取bin日志 ...
- linux shell中判断bash脚本输入的参数个数
看下面的一段程序. #!/bin/bash ]; then echo "参数个数为$#个" else echo "没有参数" fi
- Linux shell 批量运行jmeter脚本
第一版,这些代码有点问题,需要继续更改 #!/bin/bash jmxpath= reportpath= timestamp=$(date +%Y%m%d_%H%M%S) echo timestamp ...
随机推荐
- .Net Framework 4.0安装cmd命令
在安装系统以后和.Net FrameWork 后,通过cmd编译编写的程序时总是提示编译错误.可以通过cmd命令安装相应的.net framework版本. 具体步骤如下: 1.以管理员身份打开cmd ...
- bzoj 3225: [Sdoi2008] 立方体覆盖 题解
[原题] 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 51 Solved: 36 [Submit][S ...
- Swift: 继承
为了在属性值改变的时候获得通知,类可以为继承的属性添加属性观察者.属性观察者可以添加到任何属性上,不管这个属性原来是存储属性还是计算属性. Swift中的类没有一个统一的基类. 为了讲明白继承,我们先 ...
- Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command
错误如下: Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibEx ...
- C#反射—解决类的实例化问题
利用简单工厂模式来改进抽象工厂使用的复杂性(抽象工厂详见 设计模式之—抽象工厂模式) 数据表(User)业务处理接口(IUser) namespace FactoryMethodPatternDB.C ...
- My.Ioc 代码示例——使用观察者机制捕获注册项状态的变化
在 My.Ioc 中,要想在服务注销/注册时获得通知,可以通过订阅 ObjectBuilderRegistered 和 ObjectBuilderUnregistering 这两个事件来实现.但是,使 ...
- java中对象的转型
1.对象的向上转型——将子类的对象赋值给父类的引用 Student s=new Student(); Person p=s; 一个引用能够调哪些成员(变量和函数),取决于这个引用的类型 也就是Pe ...
- protocol buffer使用简介
之前在工作中用到了protocol buffer(此处简称PB)(主要对数据进行序列化与反序列化,方便网络传输中的编解码),之后发现这是一个好东西,在此稍微记录下该工具如何使用,方便以后查阅 官网地址 ...
- iOS开发中EXC_BAD_ACCESS的另类原因
今天偶然学习iOS开发的时候碰到一个EXC_BAD_ACCESS的异常,经查资料得到的解释是由于访问了已经被回收了堆内存对象导致的,参考: http://code.tutsplus.com/tutor ...
- .net中XML的创建02(linqToXml)
linqToXml比较的灵活和方便,它是基于函数式编程具体的使用如下:引用程序集using System.Xml.Linq; 1.创建XDocument并设置文档头 XDocument XDoc = ...