天天看点

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脚本总结

继续阅读