Instance: Generate temporary file record Apache process number, kill Apache process, Delete temp file
1#!/bin/Bash2#create_log. Sh3 4#1. Get the process number to all httpd applications5#2. Deposit the process number in a temporary file6#3Remove all Apache process numbers from the temp file7#4. Kill all httpd processes with kill using a For loop8#5. Delete the temporary files that were generated earlier9#6. Output message after closing a processTen#$$ The current program run time to open up the PID One Atmpfile=$$.txt - -Ps-e | grep httpd | Awk'{print $}'>>$tmpfile the - forPidinch' Cat $tmpfile ' - Do -Echo"Apache ${PID} is killed!!!" +Kill-9$pid - Done + ASleep1 at -RM-RF $tmpfile -Echo"Apache has been successfully shut down"
[Email protected] 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]
[Email protected] shell_test]# PS-E | grep httpd
78797?xx:xx:xxhttpd78800?xx:xx:xxhttpd78801?xx:xx:xxhttpd78802?xx:xx:xxhttpd78803?xx:xx:xxhttpd78804?xx:xx:xxhttpd78805?xx:xx:xxhttpd78806?xx:xx:xxhttpd78807?xx:xx:xxhttpd
[Email protected] shell_test]# sh create_log.sh
Apache78797 isKilled!!!Apache78800 isKilled!!!Apache78801 isKilled!!!Apache78802 isKilled!!!Apache78803 isKilled!!!Apache78804 isKilled!!!Apache78805 isKilled!!!Apache78806 isKilled!!!Apache78807 isKilled!!!Apache has been successfully shut down
[Email protected] shell_test]# PS-E | grep httpd
[Email protected] shell_test]#
Signal type
There are 64 types of signals in Linux
[Email protected] shell_test]# kill-l
1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP
6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1
One) (SIGSEGV) ( SIGUSR2) sigpipe) sigalrm) SIGTERM
Sigstkflt) (SIGCHLD) Sigcont SIGSTOP) SIGTSTP
(Sigttin) Sigttou () Sigurg) sigxcpu) Sigxfsz
( sigvtalrm) sigprof) sigwinch SIGIO) SIGPWR
Sigsys) (sigrtmin) sigrtmin+1) sigrtmin+2 Notoginseng) sigrtmin+3
sigrtmin+4) sigrtmin+5 (sigrtmin+6) sigrtmin+7) sigrtmin+8
sigrtmin+9) (sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
(sigrtmin+14) sigrtmin+15 () SIGRTMAX-14) SIGRTMAX-13) SIGRTMAX-12
SIGRTMAX-11) SIGRTMAX-10 SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7
(SIGRTMAX-6) ( SIGRTMAX-5) SIGRTMAX-4) SIGRTMAX-3) SIGRTMAX-2
SIGHUP: Suspend process "1" Reload Profile: Kill-hup + process number, KILL-1, Pkill-hup + process name
SIGINT: Interrupt from keyboard CTRL + C "2"
Sigquit: Exit ctrl+\ "3" from the keyboard
SIGKILL: Unconditional termination of "9"
Trap Signal Capture
After capturing a signal, it may take one of the following three actions:
1. Do not take any action by the system to deal with
2. Capture the signal, but ignore it
3. Capture the signal, but take the appropriate action
The Trap command uses:
Trap "function" signal
1) signal for the received signal
2) function as an executive (action)
Common actions:
1) Clear Temporary files
2) Ignore the signal
3) Ask the user whether to terminate the script execution
Trap Signal Capture
1#!/bin/Bash2#signal. Sh3#信号捕捉4 5Trap'MyFunc' 26 7function MyFunc () {8Echo'you are pressing CTRL + C key, the program cannot terminate'9 }Ten OneI=0 A - while : - Do theLet i++ -Echo $i -Sleep1 -Done
[Email protected] shell_test]# sh signal.sh
1
2
3
4
5
6
7
^c you are pressing CTRL + C key, the program cannot terminate
8
9
^c you are pressing CTRL + C key, the program cannot terminate
10
11
12
^c you are pressing CTRL + C key, the program cannot terminate
13
14
^c you are pressing CTRL + C key, the program cannot terminate
15
^c you are pressing CTRL + C key, the program cannot terminate
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Killed
↑
After opening a window, kill the script process to terminate
Last Login:sat Apr 02:27:19 from 172.30.1.21
[Email protected] ~]# 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
[Email protected] ~]# kill-9 80834
Trap Signal Capture Upgrade version
1#!/bin/Bash2 #signal. Sh3 #信号捕捉4 #与用户进行交互5 6Trap'MyFunc' 27 8 function MyFunc () {9Read-p'are you sure you want to terminate the process? Yes or no:'sTen One Case$sinch A 'Yes') - Exit - ;; the 'No') - ;; -*) - MyFunc + ;; - + Esac A } at -I=0 - - while : - Do -Let i++ in Echo $i -Sleep1 toDone
Select Yes to terminate directly
[Email protected] shell_test]# sh signal.sh
1
2
3
^c Are you sure you want to terminate the process? Yes or No:yes
Select No to continue down, select Other, invoke function, continue to prompt to terminate the process.
[Email protected] shell_test]# sh signal.sh
1
2
^c Are you sure you want to terminate the process? Yes or No:no
3
4
5
6
^c Are you sure you want to terminate the process? Yes or no:1
Are you sure you want to terminate the process? Yes or No:2
Are you sure you want to terminate the process? Yes or No:3
Are you sure you want to terminate the process? Yes or No:yes
[Email protected] shell_test]#
Shell script temporary file generation and use, signal capture