Linux服务器常用shell脚本总结

桃扇骨 2023-08-17 16:48 263阅读 0赞

Linux服务器常用shell脚本总结

    • 一、根据进程名称,查找并批量kill进程
      • 1.1 cut 命令和 xargs 参数传输
      • 1.2 awk命令和 xargs 参数传输
      • 1.3 循环遍历,kill,这种要写在.sh文件执行
    • 二、优化linux下,history命令,对所有登陆的IP和时间详细记录
      • 2.1 修改 /etc/profile文件,在文件末尾添加

一、根据进程名称,查找并批量kill进程

首先,查出进程

  1. ps -ef | grep classicM

在这里插入图片描述
三种方式 kill

1.1 cut 命令和 xargs 参数传输

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

1.2 awk命令和 xargs 参数传输

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

1.3 循环遍历,kill,这种要写在.sh文件执行

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

二、优化linux下,history命令,对所有登陆的IP和时间详细记录

2.1 修改 /etc/profile文件,在文件末尾添加

  1. history
  2. USER=`whoami`
  3. USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
  4. if [ "$USER_IP" = "" ]; then
  5. USER_IP=`hostname`
  6. fi
  7. if [ ! -d /var/log/history ]; then
  8. mkdir /var/log/history
  9. chmod 777 /var/log/history
  10. fi
  11. if [ ! -d /var/log/history/${LOGNAME} ]; then
  12. mkdir /var/log/history/${LOGNAME}
  13. chmod 300 /var/log/history/${LOGNAME}
  14. fi
  15. export HISTSIZE=4096
  16. DT=`date +"%Y%m%d_%H:%M:%S"`
  17. export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT"
  18. chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null

测试前
在这里插入图片描述

添加用户 ls03并登陆后,
useradd ls03
passwd ls03, 会提示输入密码
在ls03简单输入几个命令,在退出来,进入 /var/log/history目录会看到如下的显示

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,263人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux shell脚本命令

    1、脚本文件内容的第一行必须要有下面的内容 !/bin/bash 其作用是表明脚本是在什么shell下面运行的,如上是在bash中运行 2、变量无需定义,但需要注

    相关 Linux Shell 脚本

    在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本。如

    相关 shell脚本命令

    一.diff命令 diff 命令:是用来比较两个文件或目录的不同,并且是以行为单位来比对的。一般是用在 ASCII 纯文本文件的比对上。由于是以行为比对的单位,因此 di