shell脚本临时文件生成和使用,信号捕捉

 

Logger工具

logger命令向、var/log/messages文件发送消息

logger命令的一般形式:

Logger -p -i message

1)-p : 为优先级

2)-i : 发送消息

[root@Zabbixserver shell_test]# logger -p 10 -i 'hello!' 
[root@Zabbixserver shell_test]# cat /var/log/messages | tail -2

Apr 15 04:18:26 Zabbixserver root[83456]: hello!
Apr 15 04:18:32 Zabbixserver root[83463]: hello!

 

 

shell脚本临时文件生成和使用,信号捕捉

功能:日志时间动作记录,可用于很多用途

例如:用于数据库备份脚本中,把12和16行分别放在备份的开头和结尾(这里使用sleep休眠5s是为了方便观察)

1 #!/bin/bash 
2 #create_datelog.sh
3 
4 today=`date +'%Y-%m-%d'`
5 
6 filelog="${today}.log"
7 
8 if [ ! -e filelog ];then
9 touch $filelog
10 fi 
11 
12 echo "`date +'%Y-%m-%d %T'` BackupMysql start" >> $filelog
13 
14 sleep 5 
15 
16 echo "`date +'%Y-%m-%d %T'` BackupMysql end" >> $filelog
17 
18 echo ' 日志记录已完毕 '

[root@Zabbixserver shell_test]# sh create_datelog.sh

日志记录已完毕

[root@Zabbixserver shell_test]# cat 2017-04-15.log

2017-04-15 04:09:17 BackupMysql start
2017-04-15 04:09:22 BackupMysql end

 

 

实例:生成临时文件记录apache进程号,杀死apache进程,删除临时文件

  1 #!/bin/bash 
  2 #create_log.sh
  3             
  4 #1.获取到所有httpd应用程序的进程号      
  5 #2.把进程号存入一个临时文件中           
  6 #3.从临时文件中取出所有的apache的进程号 
  7 #4.使用for循环用kill杀掉所有的httpd进程 
  8 #5.删除之前生成的临时文件   
  9 #6.输出关闭进程后的消息     
 10 #$$当前程序运行时候开辟的pid
 11             
 12 tmpfile=$$.txt              
 13             
 14 ps -e | grep httpd | awk '{print $1}' >>$tmpfile
 15             
 16 for pid in `cat $tmpfile`   
 17 do          
 18         echo "apache ${pid} is killed!!!"
 19         kill -9 $pid        
 20 done        
 21             
 22 sleep 1     
 23             
 24 rm -rf $tmpfile             
 25 echo "apache 已经成功被关闭"

 

 

[root@Zabbixserver shell_test]# /etc/init.d/httpd restart

Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]

 

[root@Zabbixserver shell_test]# ps -e | grep httpd

78797 ? 00:00:00 httpd
78800 ? 00:00:00 httpd
78801 ? 00:00:00 httpd
78802 ? 00:00:00 httpd
78803 ? 00:00:00 httpd
78804 ? 00:00:00 httpd
78805 ? 00:00:00 httpd
78806 ? 00:00:00 httpd
78807 ? 00:00:00 httpd

 

[root@Zabbixserver shell_test]# sh create_log.sh

apache 78797 is killed!!!
apache 78800 is killed!!!
apache 78801 is killed!!!
apache 78802 is killed!!!
apache 78803 is killed!!!
apache 78804 is killed!!!
apache 78805 is killed!!!
apache 78806 is killed!!!
apache 78807 is killed!!!
apache 已经成功被关闭

 

[root@Zabbixserver shell_test]# ps -e | grep httpd
[root@Zabbixserver shell_test]#

 

信号类型

linux中一共有64种信号

[root@Zabbixserver shell_test]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

 

SIGHUP :挂起进程【1】  重载配置文件:kill -HUP +进程号 , kill -1 , pkill -HUP + 进程名字

SIGINT :来自键盘的中断ctrl+c【2】

SIGQUIT :从键盘退出ctrl+\【3】

SIGKILL :无条件终止【9】

 

Trap信号捕捉

捕捉到一个信号后,它可能会采取下面三种操作之一:

1.不采取任何行动,由系统来处理

2.捕捉信号,但忽略它

3.捕捉该信号,但采取相应的行动

Trap命令使用:

trap "function" signal

1)signal为接受到的信号

2)function为执行函数(动作)

 

常见行动:

  1)清除临时文件

  2)忽略该信号

  3)询问用户是否终止脚本执行

 

trap信号捕捉

1 #!/bin/bash
2 #signal.sh
3 #信号捕捉
4 
5 trap 'myfunc' 2
6 
7 function myfunc(){
8    echo '你正在按ctrl+c键,程序无法终止'
9 }
10 
11 i=0
12 
13 while :
14 do
15    let i++
16    echo $i
17   sleep 1
18 done

 

[root@Zabbixserver shell_test]# sh signal.sh
1
2
3
4
5
6
7
^C你正在按ctrl+c键,程序无法终止
8
9
^C你正在按ctrl+c键,程序无法终止
10
11
12
^C你正在按ctrl+c键,程序无法终止
13
14
^C你正在按ctrl+c键,程序无法终止
15
^C你正在按ctrl+c键,程序无法终止
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Killed

在开启一个窗口,杀死脚本进程,以终止

Last login: Sat Apr 15 02:27:19 2017 from 172.30.1.21
[root@Zabbixserver ~]# ps -ef | grep signal
root 260 2 0 Apr14 ? 00:00:00 [scsi_eh_0]
root 261 2 0 Apr14 ? 00:00:00 [scsi_eh_1]
root 284 2 0 Apr14 ? 00:00:00 [scsi_eh_2]
root 1472 1 0 Apr14 ? 00:00:01 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
root 2849 2767 0 Apr14 ? 00:00:00 /usr/libexec/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1
gdm 2958 1 0 Apr14 ? 00:00:00 /usr/bin/dbus-launch --exit-with-session
root 3131 3021 0 Apr14 ? 00:00:00 gnome-session
root 3139 1 0 Apr14 ? 00:00:00 dbus-launch --sh-syntax --exit-with-session
root 3140 1 0 Apr14 ? 00:00:00 /bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
zabbix 72407 72387 0 01:25 ? 00:00:00 /usr/local/zabbix/sbin/zabbix_server: housekeeper [deleted 0 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 0.002925 sec, idle for 1 hour(s)]
root 80834 4904 0 03:30 pts/0 00:00:00 sh signal.sh
root 80872 80807 0 03:30 pts/2 00:00:00 grep signal


[root@Zabbixserver ~]# kill -9 80834

 

trap信号捕捉升级版

 1 #!/bin/bash
 2 #signal.sh
 3 #信号捕捉
 4 #与用户进行交互
 5 
 6 trap 'myfunc' 2
 7 
 8 function myfunc(){
 9   read -p '您确定终止该进程吗? yes or no:' s
10 
11   case $s in
12     'yes')
13       exit
14       ;;
15     'no')
16       ;;
17     *)
18       myfunc
19       ;;
20 
21   esac
22 }
23 
24 i=0
25 
26 while :
27 do
28   let i++
29   echo $i
30   sleep 1
31 done

 

 

选择yes直接终止

[root@Zabbixserver shell_test]# sh signal.sh
1
2
3
^C您确定终止该进程吗? yes or no:yes

 

选择no继续往下走,选择其他,调用函数,继续提示是否终止进程

[root@Zabbixserver shell_test]# sh signal.sh
1
2
^C您确定终止该进程吗? yes or no:no
3
4
5
6
^C您确定终止该进程吗? yes or no:1
您确定终止该进程吗? yes or no:2
您确定终止该进程吗? yes or no:3
您确定终止该进程吗? yes or no:yes
[root@Zabbixserver shell_test]#

 

posted @ 2017-04-15 18:50  沈剑宇  阅读(3046)  评论(0编辑  收藏  举报