文章目錄
筆記
#Shell工具#記錄檔#編輯#vi datelog.sh#查看檔案內容[root@localhost 0418]# cat datelog.sh #!/bin/bash#datelog.sh#當前的日期current_date=`date "+%Y%m%d"`#今天的記錄檔名todaylog="log/${current_date}.log}#如果記錄檔不存在,建立一個if [ ! -f $todaylog ]then touch $todaylogfi#輸出日誌到記錄檔log_time_format=`date "+%Y-%m-%d %T"`echo "${log_time_format} 命令開始" >>$todaylog## command blockssleep 4##輸出日誌到記錄檔log_time_format=`date "+%Y-%m-%d %T"`echo "${log_time_format} 命令結束" >>$todaylog#執行[root@localhost 0418]# ./datelog.sh #查看當前日期[root@localhost 0418]# dateThu Apr 18 11:35:52 CST 2013You have new mail in /var/spool/mail/root#查看date協助手冊[root@localhost 0418]# man date#編輯vi processlog.sh#改變許可權[root@localhost 0418]# chmod 755 processlog.sh#啟動httpd服務[root@localhost 0418]# service httpd start Starting httpd: perl: warning: Setting locale failed.perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "en_US.en" are supported and installed on your system.perl: warning: Falling back to the standard locale ("C"). [ OK ]#查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -aux|grep "/usr/sbin/httpd"|grep -v " grep "root 19202 1.9 3.3 19656 8624 ? S 12:09 0:00 /usr/sbin/httpd#查看httpd進程詳細資料並去除grep、列印進程號[root@localhost 0418]# ps -aux|grep "/usr/sbin/httpd"|grep -v "grep"|awk '{print $2}'19202#執行[root@localhost 0418]# ./processlog.sh #查看[root@localhost 0418]# cat /ttftpboot tmp #查看tmp目錄,發現19304.txt檔案 [root@localhost 0418]# cat /tmp/.X0-lock.X11-unix.fam_socket.font-unix.gdm_socket.iroha_unix19304.txtOSL_PIPE_0_SingleOfficeIPC_e31798b4554fdedd4fea48cd4e93b7VMwareDnDjd_sockV4orbit-rootorbit-studentssh-XXCs45Kovmware-root#查看19304.txt內容[root@localhost 0418]# cat /tmp/19304.txt 19202#手動刪除[root@localhost 0418]# rm -f /tmp/19304.txt #執行[root@localhost 0418]# ./processlog.sh rm -f /tmp/19421.txt#重新查看,發現沒有內容[root@localhost 0418]# cat /tmp/19421.txtcat: /tmp/19421.txt: No such file or directory#訊號#查看所有訊號[root@localhost 0418]# 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) SIGUSR213) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO30) SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+135) SIGRTMIN+2 36) SIGRTMIN+3 37) SIGRTMIN+4 38) SIGRTMIN+539) SIGRTMIN+6 40) SIGRTMIN+7 41) SIGRTMIN+8 42) SIGRTMIN+943) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+1347) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-14 50) SIGRTMAX-1351) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10 54) SIGRTMAX-955) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-6 58) SIGRTMAX-559) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-2 62) SIGRTMAX-163) SIGRTMAX#查看httpd進程詳細資料[root@localhost 0418]# ps -ef |grep "httpd"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]apache 19210 19202 0 12:09 ? 00:00:00 [httpd]apache 19211 19202 0 12:09 ? 00:00:00 [httpd]apache 19212 19202 0 12:09 ? 00:00:00 [httpd]root 19533 2658 0 12:23 pts/0 00:00:00 grep httpd#查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]apache 19210 19202 0 12:09 ? 00:00:00 [httpd]apache 19211 19202 0 12:09 ? 00:00:00 [httpd]apache 19212 19202 0 12:09 ? 00:00:00 [httpd]#殺死進程19212[root@localhost 0418]# kill -s SIGKILL 19212[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]apache 19210 19202 0 12:09 ? 00:00:00 [httpd]apache 19211 19202 0 12:09 ? 00:00:00 [httpd]#功能同上[root@localhost 0418]# kill -9 19211 #重新查看httpd進程詳細資料並去除grep ,發現沒有該進程,說明被終止 [root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]apache 19210 19202 0 12:09 ? 00:00:00 [httpd]#掛起進程[root@localhost 0418]# kill -s SIGHUP 19210 #查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]#重新查看查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19209 19202 0 12:09 ? 00:00:00 [httpd]#殺死子進程或者父進程下的所有子進程[root@localhost 0418]# kill -1 19209#查看httpd進程詳細資料並去除grep [root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19616 19202 0 12:26 ? 00:00:00 [httpd]#查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdapache 19205 19202 0 12:09 ? 00:00:00 [httpd]apache 19206 19202 0 12:09 ? 00:00:00 [httpd]apache 19207 19202 0 12:09 ? 00:00:00 [httpd]apache 19208 19202 0 12:09 ? 00:00:00 [httpd]apache 19616 19202 0 12:26 ? 00:00:00 [httpd]#結束進程號為19202的進程[root@localhost 0418]# kill -1 19202#查看httpd進程詳細資料並去除grep[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"root 19202 1 0 12:09 ? 00:00:00 /usr/sbin/httpdexit crtl+D退出當前shell#trap捕捉訊號#建立檔案夾0419mkdir 0419#進入該目錄cd 0419#拷貝0418所有檔案到目前的目錄[root@localhost 0419]# cp ../0418/* .#編輯[root@localhost 0419]# vi trap1.sh#查看內容[root@localhost 0419]# cat trap1.sh #!/bin/bash#trap1.sh#注意此處為單引號,不是反引號trap ‘exitprocess’ 2LOOP=0function exitprocess(){ echo "You just hit <CRTL-C>, at number $LOOP" echo "I will now exit" exit 1}while :do LOOP=$[$LOOP+1] echo $LOOPdone#改變許可權[root@localhost 0419]# chmod 755 trap1.sh#執行[root@localhost 0419]# ./trap1.sh#另一個終端查看進程[root@localhost 0419]# ps –efroot 4221 2113 0 15:57 ? 00:00:00 /usr/sbin/sshdroot 4241 4221 0 15:58 pts/1 00:00:00 -bashroot 4834 4180 6 16:19 pts/0 00:00:00 /bin/bash ./trap1.sh#結束進程kill -2 4834#執行終端列印資訊You just hit <CRTL-C>, at number 38383I will now exit#重新編輯[root@localhost 0419]# vi trap1.sh#查看內容,增加休眠調用[root@localhost 0419]# cat trap1.sh #!/bin/bash#trap1.shtrap ‘exitprocess’ 2LOOP=0function exitprocess(){ echo "You just hit <CRTL-C>, at number $LOOP" echo "I will now exit" exit 1}while :do LOOP=$[$LOOP+1] echo $LOOP #增加代碼 sleep 1done#執行[root@localhost 0419]# ./trap1.shroot 5245 4180 0 16:35 pts/0 00:00:00 /bin/bash ./trap1.sh#另一個終端,結束進程kill -4 5245#當前終端,顯示資訊Illegal instruction#不做任何處理kill -3 5305#會做處理kill -2 5305#拷貝檔案[root@localhost 0419]# cp trap1.sh trap2.sh#查看內容[root@localhost 0419]# cat trap2.sh #!/bin/bash#trap2.shLOOP=0trap 'exitprocess' 2HOLD1=/tmp/ho1d1.$$HOLD2=/tmp/hold2.$$function exitprocess(){ echo -e "\nRecived Interrupt ..." echo -n "Do you really wish to exit?(Y?N)" read ANS case $ANS in Y|y) rm_tmp_file ;; N|n) ;; *) exitprocess ;; esac}function rm_tmp_file(){ echo "<CRTL-C> detected .. Now cleaning up ...wait" rm /tmp/*.$$ 2>/dev/null exit 1}while :do LOOP=$[$LOOP+1] echo $LOOP df>>$HOLD1 ps -xa >>$HOLD2 sleep 1done#執行[root@localhost 0419]# ./trap2.sh 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Recived Interrupt ...Do you really wish to exit?(Y?N)N565758596061626364656667686970717273747576Recived Interrupt ...Do you really wish to exit?(Y?N)y<CRTL-C> detected .. Now cleaning up ...wait#查看tmp目錄下的內容ls -al /tmp/-rw-r--r-- 1 root root 3816 Apr 19 16:50 ho1d1.5503-rw-r--r-- 1 root root 3816 Apr 19 16:50 ho1d2.5503#另一個終端查看進程ps –ef#結束進程kill -2 5503#eval#執行[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`-bash: total: command not found#編輯vi myfile#查看內容[root@localhost 0419]# cat myfile ls –al#執行[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`total 32drwxr-xr-x 2 root root 4096 Apr 19 16:54 .drwxr-xr-x 15 root root 4096 Apr 19 15:58 ..-rwxr-xr-x 1 root root 431 Apr 19 15:59 awkif.sh-rwxr-xr-x 1 root root 435 Apr 19 15:59 datelog.sh-rw-r--r-- 1 root root 7 Apr 19 16:54 myfile-rwxr-xr-x 1 root root 391 Apr 19 15:59 processlog.sh-rwxr-xr-x 1 root root 210 Apr 19 16:35 trap1.sh-rwxr-xr-x 1 root root 484 Apr 19 16:45 trap2.sh#重新編輯[root@localhost 0419]# vi myfile #查看內容[root@localhost 0419]# cat myfile ls#重新執行[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`awkif.sh datelog.sh myfile processlog.sh trap1.sh trap2.sh#可以不使用變數[root@localhost 0419]# eval `cat myfile` awkif.sh datelog.sh myfile processlog.sh trap1.sh trap2.sh#logger#查看messages內容[root@localhost 0419]# cat /var/log/messages#向記錄檔增加內容[root@localhost 0419]# logger -i "chinaitlab shenzhen"Apr 19 17:01:09 localhost root[6179]: chinaitlab shenzhen[root@localhost 0419]# logger -p 19 -i "chinaitlab shenzhen"#再次查看[root@localhost 0419]# cat /var/log/messagesApr 19 17:01:09 localhost root[6179]: chinaitlab shenzhenApr 19 17:01:56 localhost root[6199]: chinaitlab shenzhen#向記錄檔增加內容[root@localhost 0419]# logger -p 1 -i "chinaitlab shenzhen"#再次查看[root@localhost 0419]# cat /var/log/messagesApr 19 17:01:09 localhost root[6179]: chinaitlab shenzhenApr 19 17:01:56 localhost root[6199]: chinaitlab shenzhenApr 19 17:02:25 localhost root[6201]: chinaitlab shenzhen
附圖
|
|
@Wentasy 博文僅供參考,歡迎大家來訪。如有錯誤之處,希望批評指正。原創博文如需轉載請註明出處,謝謝 [CSDN部落格] |