注意:系统维护完毕后,记得把/etc/nologin文件删除,否则普通用户无法登入系统
#!/bin/bash
#
#
#===================================================================
#
# author: 9528
# mail: scsc4212@163.com
# date: 2019-7-24
#
#-------------------------------------------------------------------
#
# 简述:
# 脚本实现提示普通用户系统即将开始维护作业,禁止用户新登入,已
# 登入的普通用户强制下线。
#
#===================================================================
#
#引入系统函数,调用函数实现执行成功或失败时给信息着色的功能
#
. /etc/init.d/functions
#
#创建/etc/nologin文件,此文件存在时,普通用户将不能登入,普通用户登陆时
#会显示文件里的内容
#
#注意:系统维护完成后要记得把此文件删除,否则会导致普通用户不能登入系统
#
echo "The system is about to be maintained. Do not log in!" > /etc/nologin
#
#给系统上的全部已登入的用户发提示信息
#
wall -n << EOF
===========================================================================
Peleae note:
After 10 minutes, the system is about to be maintained.
Please log off after you have saved your data, mandatory
logging off after 10 minutes!
===========================================================================
EOF
#
#10分钟倒计时
#
sleep 10m
#
#查看此时有没有非root用户登入系统
#
who | grep -v "^root\>" > /dev/null
#
#如果有非root用户登入系统,则强制下线这些用户
#
#整理已登入的普通用户,去除重复项
#为了防止误下线root用户,加强判断,只有普通用户才强制下线
#
if [ $? -eq 0 ];then
who | grep -v "^root\>" | awk '{print $1}' > users1
sort -u users1 > users2
while read line; do
if [ $line != 'root' ];then
pkill -kill -u $line
if [ $? -eq 0 ];then
action "pkill $line:" /bin/true
else
action "pkill $line:" /bin/false
fi
fi
done < users2
rm -f users1 users2
fi
#
#作业完成
#提示用户可以再次执行who命令核实是否普通用户都已下线
#提示用户系统维护完毕后,要记得删除/etc/nologin文件
#
echo -e "\n"
echo "-------------------------------finish!----------------------------------------"
echo "Please confirm again by execute the 'who' command."
echo -e "Remember to delete the /etc/nologin file when system maintenance is complete.\n"
执行结果演示
[root@localhost ~/test]$bash pkill.sh
Remote broadcast message (Thu Aug 1 12:55:26 2019):
===========================================================================
pleae note:
After 10 minutes, the system is about to be maintained.
Please log off after you have saved your data, mandatory
logging off after 10 minutes!
===========================================================================
pkill sc: [ OK ]
pkill tom: [ OK ]
-------------------------------finish!----------------------------------------
Please confirm again by execute the 'who' command.
Remember to delete the /etc/nologin file when system maintenance is complete.
[root@localhost ~/test]$