首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当停电时如何使Ubuntu关机?

当停电时如何使Ubuntu关机?
EN

Ask Ubuntu用户
提问于 2014-10-10 12:01:12
回答 3查看 3K关注 0票数 5

我的项目要求笔记本电脑的电源必须开关。

当外部电源丢失时,是否有办法使Ubuntu正确关闭?它将在内部电池上运行关机。

EN

回答 3

Ask Ubuntu用户

发布于 2014-10-10 15:53:16

尝试检查on_ac_power命令。来自man on_ac_power

代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
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循环中运行:

代码语言:javascript
运行
复制
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) + "&“以使脚本退出。像这样

代码语言:javascript
运行
复制
sh /home/USERNAME/ChechMainPWR.sh $

重新启动并查看更改。

奖金

如果您希望在桌面通知中丢失或未连接到警报时查看该警报,则可以使用notify-send程序。

代码语言:javascript
运行
复制
notify-send "Main Power Lot!" "System is now shutting down"

有关更多信息,请参见man notify-send

代码语言:javascript
运行
复制
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.

最后的脚本应该是这样的:

最终脚本

代码语言:javascript
运行
复制
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并在打开的文件末尾添加以下行:

代码语言:javascript
运行
复制
Your_USERNAME  ALL=NOPASSWD: /sbin/shutdown

然后退出编辑器并保存它(CTRL+x)。

现在,您可以在命令行或脚本中使用shutdown命令,而无需密码。

所以剧本应该是这样的:

代码语言:javascript
运行
复制
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

外部链接:

如何在没有密码的情况下运行特定的sudo命令?

如何从终端重新启动/关机?

票数 6
EN

Ask Ubuntu用户

发布于 2014-10-10 12:05:55

在“电源管理”首选项中,您可以设置电池功率极低时发生的情况。

把它放在那里关闭:

票数 0
EN

Ask Ubuntu用户

发布于 2014-10-10 12:27:45

点击桌面右上角的设置并选择“系统设置.”进入系统设置。在系统设置中,pannel打开" power“,在那里您可以看到一个选项:”当电源非常低时,默认情况下它是黑色的。请选择它,然后在下拉菜单中选择“关机”。

票数 -1
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/534453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档