如何监控关机序列的状态

如何监控关机序列的状态

这是我编写的在 30 分钟后锁定并关闭系统的脚本。

#!/bin/bash
# Read Password
read -s -p "Password: " password
echo
# shutdown and screensaver Command
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s 

该脚本的问题是,有时 30 分钟后它也不会关闭。

这就是为什么我read在最后添加了一个命令 - 为了监控关机序列的状态以及进度窗口。但这个脚本只显示启动分钟的状态

例如:

Broadcast message from root@eka-PC
    (unknown) at 10:31 ...

The system is going down for halt in 30 minutes!

它没有显示关机的进度。

編輯:

我还在上面的脚本中发现了一个错误,如果输入了错误的密码,系统将锁定而不发出任何警告。是否可以在关机命令中给出条件语句。我试过了

SHUTDOWN=$(echo "$password" | sudo -kS shutdown -h +30 &);
if [[ $SHUTDOWN -eq 0]]
then
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s 
fi

它只激活关机但不激活屏幕保护程序


工作脚本

未找到验证关机的解决方案(echo "$password" | sudo -kS shutdown -h +30 &),因此我尝试通过两次确认密码来解决这个问题。这是工作脚本

#!/bin/bash

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

echo "Activate screensaver and shutdowns the system in 30 minutes";
# Read Password
shut()
{
read -s -p "Password: " password1
read -s -p "Confirm Password: " password
echo
# Run Command
if [ "$password1" == "$password" ]
then
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "`echo $'\n> \n>'` ${green} To cancel shutdown, press C ${reset}`echo $'\n> \n>'`" prompt

    if [[ $prompt =~ [cC](es)* ]]
    then
    echo "$password" | sudo -kS shutdown -c
    read -p "`echo $'\n> \n>'`${red} Shutdown Cancelled ${reset}" -n1 -s
    fi
else
echo -e "\n${red} Wrong Password ${reset}, Re enter the password"
shut 
fi
}
shut

还向上述脚本添加了额外的文本格式和关机取消代码。

如果你还想激活音乐播放器(例如 Rhytmbox),那么请gnome-screensaver-command -a &添加此代码rhythmbox-client --play &

任何建议都值得赞赏

答案1

sleep该脚本使用循环控制关机计时器。

#!/bin/bash
# Read Password
read -s -p "Password: " password
echo
# Send messages
for i in {0..5}
do
    echo "Shutdown in" $((30- i * 5)) "minutes"
    sleep 300
done
echo "Shutdown now" | wall
# shutdown and screensaver Command
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s

答案2

相反,您可以使用at命令。使用以下命令安装

sudo apt-get install at

然后运行

sudo echo "shutdown -h now"|at  now + 30 min

相关内容