天天看點

Linux伺服器常用shell腳本總結

Linux伺服器常用shell腳本總結

  • ​​一、根據程序名稱,查找并批量kill程序​​
  • ​​1.1 cut 指令和 xargs 參數傳輸​​
  • ​​1.2 awk指令和 xargs 參數傳輸​​
  • ​​1.3 循環周遊,kill,這種要寫在.sh檔案執行​​
  • ​​二、優化linux下,history指令,對所有登陸的IP和時間詳細記錄​​
  • ​​2.1 修改 /etc/profile檔案,在檔案末尾添加​​

一、根據程序名稱,查找并批量kill程序

首先,查出程序

ps -ef | grep classicM      
Linux伺服器常用shell腳本總結

三種方式 kill

1.1 cut 指令和 xargs 參數傳輸

ps -ef | grep classicM | grep -v grep | cut -c 9-15 | xargs kill -9      

1.2 awk指令和 xargs 參數傳輸

ps -ef | grep classicM | grep -v grep | awk '{print $2}' | xargs kill -9      

1.3 循環周遊,kill,這種要寫在.sh檔案執行

ID=` ps -ef | grep classicM | grep -v grep | awk '{print $2}' `
        for id in $ID
        do
        kill -9 $id
        done      

二、優化linux下,history指令,對所有登陸的IP和時間詳細記錄

2.1 修改 /etc/profile檔案,在檔案末尾添加

history
USER=`whoami`
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]; then
USER_IP=`hostname`
fi
if [ ! -d /var/log/history ]; then
mkdir /var/log/history
chmod 777 /var/log/history
fi
if [ ! -d /var/log/history/${LOGNAME} ]; then
mkdir /var/log/history/${LOGNAME}
chmod 300 /var/log/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H:%M:%S"`
export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT"
chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null      

測試前

Linux伺服器常用shell腳本總結

繼續閱讀