控制脚本
来源:互联网 发布:dnf数据异常 编辑:程序博客网 时间:2024/06/07 02:02
处理信号
中断进程
Ctrl+C组合键会生成SIGINT信号,并将其发送给当前在shell中运行的所有进程。暂停进程
Ctrl+Z组合键会生成一个SIGSIGTSTP信号,停止shell中运行的进程。停止进程会让程序继续保留在内存中,并能从上次停止的位置继续运行。
可以用ps命令来查看已停止的作业 :
ps -l
捕获信号
trap commands signals
如果脚本受到了trap命令中列出的信号,该信号不再由shell处理,而是交由本地处理。
#!/bin/bash # testing signal trap trap "echo 'Sorry, I have trapped Ctrl+C'" SIGINT echo this is a test script count=1 while [ $count -le 10 ] do echo "Loop #$count" sleep 1 count=$[ $count + 1 ] done echo "this is the end of the test script"
执行这段脚本:
[root@cenos scripts2]# ./signal this is a test scriptLoop #1Loop #2Loop #3^CSorry, I have trapped Ctrl+CLoop #4Loop #5^CSorry, I have trapped Ctrl+CLoop #6Loop #7Loop #8^CSorry, I have trapped Ctrl+CLoop #9Loop #10this is the end of the test script
捕获脚本退出
适用于shell完成任务时执行命令
在trap命令后加上EXIT信号就行
trap “echo Goodbye” EXIT修改或移除捕获
修改捕获:只需重新使用带有新命令的trap命令。
删除设置的捕获: 在trap命令与希望恢复默认行为的信号列表之间加上两个波折号就可以了
trap – SIGINT#!/bin/bash # tesing signal trapping trap "echo 'Sorry, I have trapped Ctrl+C'" SIGINT echo this is a test script count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$[ $count + 1 ] done # modify the trap trap "echo 'I modified the trap'" SIGINT count=1 while [ $count -le 5 ] do echo "Second loop #$count" sleep 1 count=$[ $count + 1 ] done # remove the trap trap -- SIGINT echo "I just removed the trap" count=1 while [ $count -le 5 ] do echo "Third loop #$count" sleep 1 count=$[ $count + 1 ] done echo "this is the end of the test script"
执行这段脚本:
[root@cenos scripts2]# ./signal this is a test scriptLoop #1Loop #2Loop #3^CSorry, I have trapped Ctrl+CLoop #4Loop #5^CSorry, I have trapped Ctrl+CSecond loop #1Second loop #2^CI modified the trapSecond loop #3Second loop #4^CI modified the trapSecond loop #5I just removed the trapThird loop #1Third loop #2^C
以后台模式运行脚本 ##
./test &
#在命令后面加&符号
在终端会话中使用后台进程时,每个后台程序都绑定到了该终端回话的终端上了(pts/0)。如果进程会话退出了,后台进程也会退出。
在非控制台下运行脚本
nohup命令运行了另外一个命令来阻断所有发送给该进程的SIGHUP信号。在退出终端会话时阻止进程退出。
nohup命令会将stdout, stderr的消息重定向到一个名为nohup.out文件中。
作业控制
包括:启动,停止,终止,恢复作业
jobs命令:查看shell当前正在处理的作业。(-l 参数:列出进程的PID以及作业号)
$ jobs -l
[1] 1950 Running ./test10.sh > test10a.out &
[2]- 1952 Running ./test10.sh > test10b.out &
[3]+ 1955 Running ./test10.sh > test10c.out &
+:默认作业
-:下一个默认作业,
重启停止的作业:
以后台模式重启一个作业: bg命令 加上作业号(如果有多个已停止的作业时)
以前台模式重启一个作业:fg命令 加上作业号(如果有多个已停止的作业时)
调整谦让度
调度优先级:内核分配给进程的CPU时间。Linux中,由shell启动的所有进程的调度优先级(-20:最高 到+19:最低)默认都是相同的(0)
nice命令:设置命令启动时的调度优先级
必须要将nice命令和要启动的命令放在同一行中。
$ nice -n 10 ./test1.sh > output.txt &
renice命令: 改变系统上已运行命令的优先级。
指定进程的PID来改变进程优先级。
$ renice -n 10 -p 5055
nice和renice命令的限制:
1. 只能对属于你的进程执行renice/nice
2. 只能通过renice降低进程的优先级
3. root用户可以通过renice来任意调整进程的优先级
定时运行作业
方法:at命令和cron表
用 at 命令来计划执行作业
at的守护进程atd会以后台模式运行,检查作业队列来运行作业。atd守护进程会检查一个特殊目录(usually /var/spool/at)来获取用
at命令提交的作业。usually 每60s 检查一下这个目录。if has job
and 作业设置运行的时间与current时间匹配, atd守护进程就会运行
此作业。格式: at [-f filename] time
time:指定Linux系统何时运行该作业。获取作业的输出:
作业运行时,Linux系统会将提交改作业的用户的邮件地址作为stdout
和 stderr。列出等待的作业:
atq命令[root@DDAN root]# atq
1 Sun Oct 15 18:12:00 2017 a root
作业号 系统运行该作业的日期及时间 所在的队列 用户删除作业:
atrm命令atrm 作业号
[root@DDAN root]# atrm 1
使用cron时间表存放需要定期执行的脚本
min hour dayofmonth month dayofweek command
构建cron时间表:
使用crontab命令来处理cron时间表
crontab -l :列出已有的cron时间表
crontab -e:为cron时间表添加条目浏览cron目录:
4个预配置的cron脚本目录:
hourly,daily,monthly,weeklyanacron程序:
如果某个作业在cron时间表中安排运行的时间已到,但这时候Linux系统处于关机状态,
那么这个作业就不会被运行。当系统开机时,cron程序不会再去运行那些错过的作业。
要解决这个问题,许多Linux发行版还包含了anacron程序。如果anacron知道某个作业错过了执行时间,它会尽快运行该作业。这意味着如果Linux系统
关机了几天,当它再次开机时,原定在关机期间运行的作业会自动运行。anacron程序只会处理位于cron目录的程序,比如/etc/cron.monthly。它用时间戳来决定作业
是否在正确的计划间隔内运行了。每个cron目录都有个时间戳文件,该文件位于/var/spool/anacron。
anacron程序使用自己的时间表(/etc/anacrontab)来检查作业目录。
$: vim /etc/anacrontab
…..
period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily >/dev/null 2>&1
7 25 cron.weekly nice run-parts /etc/cron.weekly >/dev/null 2>&1
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly >/dev/null 2>&1
period in days:作业多久运行一次
delay in minutes:系统启动后anacron程序需要等待多少min在开始运行错过的脚本
commands:包含run-parts程序和一个cron脚本目录名。run-parts程序负责运行目录中传给它的任何脚本。
- 控制脚本
- 引擎控制命令脚本
- perl脚本进程控制
- 脚本控制资源列表
- linux脚本流程控制
- 边际服务控制脚本
- shell脚本超时控制
- shell脚本超时控制
- nginx服务控制脚本
- mysql bat脚本控制
- wpa_supplicant控制脚本
- shell脚本超时控制
- F5设备控制脚本
- LInux_场景控制脚本
- shell脚本逻辑控制
- shell 脚本并发控制
- 虚拟机控制脚本
- Shell脚本控制语句
- 表空间-只读、读写设置
- 数字信封工作原理
- ARM9 #define GPACON ((volatile unsigned *)0x56000000)解析
- Oracle表空间-大小设置
- HDU 6194 string string string (SAM)
- 控制脚本
- Oracle表空间-移动
- Oracle表空间-删除
- 数据缓存
- HashMap、HashSet和HashTable的区别与联系
- Oracle表空间-OMF管理表空间
- 表单验证
- 0001_程序员戒手淫之放纵情欲对命运和身心的影响
- Angularjs的增删改查,排序,查询年龄范围