Shell脚本编程---脚本控制(六)

本文详细介绍了Linux系统中信号的生成、捕获和处理方法,包括使用trap命令监控和拦截信号,后台运行脚本,作业控制如重启、停止、终止和恢复作业,以及如何使用at和batch命令进行定时任务调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、处理信号

(1)Linux信号

(2)生成信号

中断进程:         Ctrl  + C

暂停进程:         Ctrl  + Z

(3)捕获信号

trap命令可以指定能够通过shell脚本监控和拦截的Linux信号。

trap命令的格式如下:

trap commands signals
[root@ceph01 ctrl-shell]# cat trap-signal.sh 
#!/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"

运行脚本:

[root@ceph01 ctrl-shell]# ./trap-signal.sh 
This is a test program
Loop #1
Haha
Loop #2
Loop #3
Haha
Loop #4
ksks
Loop #5
Loop #6
This is the end of the test program

(4)捕获脚本退出

[root@ceph01 ctrl-shell]# cat trap-exit.sh 
#!/bin/bash
# trapping the script exit
trap "echo byebye" EXIT

count=1
while [ $count -le 5 ]
do
	echo "Loop #$count"
	sleep 3
	count=$[ $count + 1 ]
done

运行脚本:

[root@ceph01 ctrl-shell]# ./trap-exit.sh 
Loop #1
Loop #2
byebye

(5)移除捕获

[root@ceph01 ctrl-shell]# cat trap-remove.sh 
#!/bin/bash
# removing a set trap

trap "echo byebye" EXIT

count=1
while [ $count -le 5 ]
do
	echo "Loop #$count"
	sleep 3
	count=$[ $count + 1 ]
done
trap - EXIT
echo "I just removed the trap"

运行脚本:

[root@ceph01 ctrl-shell]# ./trap-remove.sh 
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
I just removed the trap

二、以后台模式运行脚本

以后台模式运行(&符号)

[root@ceph01 ctrl-shell]# ./trap-remove.sh &
[1] 14394
[root@ceph01 ctrl-shell]# Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
I just removed the trap
[1]+  Done                    ./trap-remove.sh

三、在不使用控制台的情况下运行脚本

      有时需要从终端会话启动shell脚本。然后让脚本在结束之前以后台模式运行,即使退出终端会话也是如此,可以使用nohup命令。

nohup命令的格式如下:

[root@ceph01 ctrl-shell]# nohup: ignoring input and appending output to ‘nohup.out’
[1]+  Done                    nohup ./trap-remove.sh

[root@ceph01 ctrl-shell]# cat nohup.out 
Loop #1
Loop #2
Loop #3
Loop #4
Loop #5
I just removed the trap

 

四、作业控制

重启、停止、终止和恢复作业的操作称为作业控制

(1)查看作业

[root@ceph01 ctrl-shell]# cat view-jobs.sh 
#!/bin/bash
# testing job control

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@ceph01 ctrl-shell]# ./view-jobs.sh 
This is a test program 14505
Loop #1
Loop #2

job命令使用一些不同的命令行参数

(2)作业调度调优

1》nice命令

[root@ceph01 ctrl-shell]# nice -n 10 ./view-jobs.sh > output &
[1] 14616

 

2》renice命令

[root@ceph01 ctrl-shell]# ./view-jobs.sh > output &
[2] 14645
[1]   Done                    nice -n 10 ./view-jobs.sh > output
[root@ceph01 ctrl-shell]# renice 10 -p 14645

五、预定时间运行

(1)使用at命令调度作业

at命令的基本格式:

at [-f filename] time

time参数:
-标准的小时和分钟格式,比如10:15
-AM/PM指示符,比如10:15PM
-具体指定的时间,比如now,noon,midnight,teatime(4PM)

-标准的日期格式,比如MMDDYY、MM/DD/YY或DD.MM.YY
-文本日期格式,比如Jul 4或Dec 25
-还可以指定时间增量:
   Now + 25 minutes
   10:15PM tomorrow
   10:15 + 7 days

获取作业输出

[root@ceph01 ctrl-shell]# cat at.sh 
#!/bin/bash
# testing the at command

time=`date +%T`
echo "This script ran at $time"
echo "This is the end of the script" >&2
[root@ceph01 ctrl-shell]# date 
Sun Jan  6 17:24:14 CST 2019
[root@ceph01 ctrl-shell]# chmod 777 at.sh 
[root@ceph01 ctrl-shell]# at -f at.sh 17:25
job 1 at Sun Jan  6 17:25:00 2019
[root@ceph01 ctrl-shell]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sun Jan  6 17:25  15/534   "Output"
& 
Message  1:
From root@ceph01.localdomain  Sun Jan  6 17:25:00 2019
Return-Path: <root@ceph01.localdomain>
X-Original-To: root
Delivered-To: root@ceph01.localdomain
Subject: Output from your job        1
To: root@ceph01.localdomain
Date: Sun,  6 Jan 2019 17:25:00 +0800 (CST)
From: root@ceph01.localdomain (root)
Status: R

This script ran at 17:25:00
This is the end of the script

& ^CInterrupt
& quit
Held 1 message in /var/spool/mail/root
You have mail in /var/spool/mail/root

(2)使用batch命令

batch命令的格式:

batch [-f filename] [time]

(3)调度定期脚本

corn表格

corn表格的格式如下:

min hour dayofmonth month dayofweek command

构建corn表格

$:crontab -l

anacorn程序

anacron表格的格式:

period delay identifier command

六、开机自启动脚本

(1)在启动时启动脚本

Linux运行级别

(2)定义脚本

(3)随新shell一起启动

每个用户的主目录都包含两个文件,bash shell使用这两个文件自动启动脚本和设置环境变量:

        .bash_profile文件

        .bashrc文件

如果希望为系统的每个用户运行脚本,可以使用大部分linux提供的/etc/bashrc文件。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值