我的项目要求笔记本电脑的电源必须开关。
当外部电源丢失时,是否有办法使Ubuntu正确关闭?它将在内部电池上运行关机。
发布于 2014-10-10 15:53:16
尝试检查on_ac_power
命令。来自man on_ac_power
:
NAME
on_ac_power - test whether computer is running on AC power
SYNOPSIS
on_ac_power
DESCRIPTION
on_ac_power checks whether the system is running on AC power (i.e., mains power) as opposed to battery
power.
OPTIONS
None.
EXIT STATUS
0 (true) System is on mains power
1 (false) System is not on mains power
255 (false) Power status could not be determined
if on_ac_power; then
echo "You are on AC-Power" # System is on mains power
else
echo "Power Lost" # System is not on mains power
fi
你需要在每隔一段时间内检查交流电状态.在后台运行它是最简单的方法,在while循环中运行:
while true
do
if on_ac_power; then
echo "You are on AC-Power" # System is on main power
else
echo "Power Lost" # System is not on main power
fi
sleep [Seconds]
done
保存脚本(ChechMainPWR.sh
),如果您想在启动时运行脚本,那么在/etc/rc.local
中添加一行以调用脚本(ChechMainPWR.sh
) + "&“以使脚本退出。像这样
sh /home/USERNAME/ChechMainPWR.sh $
重新启动并查看更改。
如果您希望在桌面通知中丢失或未连接到警报时查看该警报,则可以使用notify-send
程序。
notify-send "Main Power Lot!" "System is now shutting down"
有关更多信息,请参见man notify-send
。
NAME
notify-send - a program to send desktop notifications
SYNOPSIS
notify-send [OPTIONS] <summary> [body]
DESCRIPTION
With notify-send you can sends desktop notifications to the user via a notification daemon from the command
line. These notifications can be used to inform the user about an event or display some form of information
without getting in the user's way.
最后的脚本应该是这样的:
while true
do
if ! on_ac_power; then # System is not on main power
notify-send "Main Power Lot!" "System is going down after 10 seconds"
sleep 10
echo YOUR_PASSWORD | sudo -kS shutdown -h now
#####^^^^^^^^^^^^^ VERY VERY VERY insecure! ##########
fi
sleep 300 # check main power status every 5 minutes
done
警告:echo YOUR_PASSWORD | sudo -kS shutdown -h now
可以工作,但它非常不安全,因为您的密码是在您的历史记录中编写的,并且可能通过进程列表对其他用户可见。
然后,另一种解决方案是:您可以配置您的系统,使sudo someCommand
不需要密码(在我的例子中是sudo shutdown
)。为此,运行sudo visudo
并在打开的文件末尾添加以下行:
Your_USERNAME ALL=NOPASSWD: /sbin/shutdown
然后退出编辑器并保存它(CTRL+x)。
现在,您可以在命令行或脚本中使用shutdown
命令,而无需密码。
所以剧本应该是这样的:
while true
do
if ! on_ac_power; then #your system is not on AC-Power
notify-send "Main Power Lost!" "System is going down after 10 seconds"
sleep 10
sudo shutdown -h now # System is not on mains power
fi
sleep 300 # check main power status every 5 minutes
done
外部链接:
发布于 2014-10-10 12:05:55
在“电源管理”首选项中,您可以设置电池功率极低时发生的情况。
把它放在那里关闭:
发布于 2014-10-10 12:27:45
点击桌面右上角的设置并选择“系统设置.”进入系统设置。在系统设置中,pannel打开" power“,在那里您可以看到一个选项:”当电源非常低时,默认情况下它是黑色的。请选择它,然后在下拉菜单中选择“关机”。
https://askubuntu.com/questions/534453
复制相似问题