1. 程式人生 > >Linux Shell筆記之指令碼控制

Linux Shell筆記之指令碼控制

1.處理訊號
訊號   值               描述
1       SIGHUP      掛起程序
2       SIGINT         終止程序
3       SIGQUIT      停止程序
9       SIGKILL       無條件終止程序
15     SIGTERM    可能的話終止程序
17     SIGSTOP    無條件停止程序,但不是終止程序
18     SIGTSTP     停止或暫停程序,但不終止程序
19     SIGCONT    繼續執行停止的程序

bash shell預設會忽略任何3和15的訊號,但會處理收到的1和2訊號

2.產生訊號

1)終止程序
Ctrl+C會產生訊號2終止程序
2)暫停程序
Ctrl+Z會產生訊號18停止程序
# sleep 100
[1]+  Stopped                 sleep 100

# ps au
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     25186  0.0  0.0   3680   468 pts/1    T    04:16   0:00 sleep 100
root     25240  0.0  0.0   4216   940 pts/1    R+   04:19   0:00 ps au
root     26781  0.0  0.1   4620  1424 pts/2    Ss+  Nov09   0:00 bash
shell中執行的每個程序稱為作業,已停止的作業狀態為T

3.捕捉訊號

1)捕捉訊號
#!/bin/bash
trap "echo 'I have trapped Ctrl+C'" SIGINT SIGTERM         trap後為""空則不顯示資訊,指令碼不會被Ctrl+C終止
count=1
while [ $count -le 10 ]
do
   echo "loop #$count"
   sleep 2
   count=$[ $count + 1 ]
done

# ./trap 
loop #1
loop #2
loop #3
loop #4
I have trapped Ctrl+C
loop #5

2)捕捉指令碼退出時的訊號
#!/bin/bash
trap "echo 'goodbye'" EXIT                                捕捉EXIT訊號

3)移除捕捉
#!/bin/bash
trap "echo 'goodbye'" EXIT
count=1
while [ $count -le 2 ]
do
   echo "loop #$count"
   sleep 2
   count=$[ $count + 1 ]
done
trap - EXIT    
                                           trap後加-號移除訊號捕捉

4.後臺執行指令碼
# ./trap &  
                 加&符號後臺執行,程序會話退出,後臺程序也會退出
[5] 30618                    方括號內為作業號,之後為PID

# nohup ./trap &            加nohup命令,即使退出終端,指令碼也不會終止
[5] 30725
# nohup: appending output to `nohup.out'    指令碼結束後生成nohup.out檔案,包含標準輸出資訊

5.作業控制
1)檢視作業
# jobs                       -l選項顯示PID及作業號
[1]   Stopped                  sleep 100  (wd: ~)
[2]   Stopped                   ./trap
[3]-  Stopped                  ./trap vi trap                 帶-號為下一個預設作業
[4]+  Stopped                 ./trap                         帶+號會被當作預設作業

2)重啟停止的作業
# bg 2                        後臺模式重啟一個作業,作業號為2
# fg 2                        前臺模式重啟一個作業

6.調整謙讓度
預設情況下,Shell啟動的所有程序擁有相同的高度優先順序
優先順序從-20(最高優先順序)到20(最低優先順序)
1)nice
# nice -n 10 ./trap > out.txt &        nice命令在啟動時調整優先順序,普通使用者只能降低優先順序

2) renice
# ps al
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0 22854 22850  15   0   4620  1452 wait   Ss   pts/1      0:00 -bash
0     0 26781 26770  16   0   4620  1424 -      Ss+  pts/2      0:00 bash
0     0 30927 22854  15   0   4440  1040 finish T    pts/1      0:00 /bin/bash ./trap
# renice 10 -p 26781
renice只能對屬於自己的程序執行renice,只能降低程序的優先順序,root使用者可以調整任何優先順序

7.定時任務
1)cron時間表
min hour dayofmonth month dayofweek command

2)檢視cron表
$ crontab -l
$ crontab -e                    清加條目

3)cron目錄
/etc目錄下cron目錄,指令碼放於這些目錄下就可定時執行
cron.hourly
cron.daily
cron.weekly
cron.monthly
cron表中的作業當系統關機後,期間的作業再啟動系統後不會執行

4)anacron程式
系統關機了,當再次啟動後,anacron程式會盡快執行錯過的計劃好的作業
anacron程式只處理cron目錄中的程式,但不會處理/etc/cron.hourly的指令碼
anacron格式:period delay identifier command
period定義多久執行一次,單位天
delay定義系統啟動後多少分鐘後開始執行錯過的指令碼
$ cat anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

1       65      cron.daily                  run-parts /etc/cron.daily
7       70      cron.weekly              run-parts /etc/cron.weekly
30      75      cron.monthly            run-parts /etc/cron.monthly

8.開機自啟動
開機讀取/etc/inittab檔案
開機指令碼一般放於/etc/rc#.d目錄,其中#為執行級別
或者/etc/init.d目錄

本地開機檔案
Ubuntu         /etc/rc.local
debian         /etc/init.d/rc.local
Fedora         /etc/rc.d/rc.local

為系統中的每個使用者執行一個指令碼:/etc/bashrc

inittab程式:格式 label:runlevel:action:process
1)label
id              定義預設init程式執行級別
ca              按下“Ctrl+Alt+Delete”時執行的程序
si              指定系統初始化程序
pf              UPS監控軟體通知電源被切斷時執行的程序
pr              真正關閉系統前,UPS發出電源恢復訊號時執行的程序

2)runlevel     執行級別,可不指定
3)action
wait            init程式執行此程式一次,並等待執行結束才進入下一步
ctrlaltdel      按下“Ctrl+Alt+Delete”時執行Process欄位指定的值
respawn         保證一直處於執行狀態的程序,若程序終止,則重啟程序

4)process      執行的命令

#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, <[email protected]>
#               Modified for RHS Linux by Marc Ewing and Donnie Barnes
#

# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)

id:5:initdefault:

# System initialization.   執行rc.sysinit進行系統初始化
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# Trap CTRL-ALT-DELETE     按下“Ctrl+Alt+Delete”時關機
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# When our UPS tells us power has failed, assume we have a few minutes
# of power left.  Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.  
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.   取消關機
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"

# Run gettys in standard runlevels          始終開啟6個終端控制檯
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon