在bash脚本中运行shutdown命令

问题描述 投票:1回答:1

我正在尝试创建一个可执行文件(bash脚本)来向我显示通知,并在找不到进程时关闭我的计算机。

我将脚本作为启动应用程序运行,我在此脚本中使用notify-sendshutdown命令。

问题是: (1)如果我将myfolder/myscript添加到启动应用程序列表中,则无法运行shutdown命令(此时需要root密码) (2)如果我添加脚本sudo myfolder/myscript它无法通过notify-send应用程序显示通知。

我已经在互联网上做了很多搜索并尝试了以下步骤: (1)通过/sbin/shutdown向sudores添加了脚本路径或sudo visudo (2)在su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus命令之前添加了notify-send(我发现一些用户报告root无法发送通知)

所以...他们都没有奏效。我错过了什么?可以做些什么来显示通知和关机?

这是我的代码:

#!/bin/bash

#Search for a specific process and sleep if it is found (removed for space saving)

shut_time=$(date --date='10 minutes' +"%T")
notify-send -t 600000 "WARNING:
Program is not running.
Shutting down in 10 minutes (scheduled for $shut_time)."

#ALREADY TESTED BELLOW LINES (DON'T WORK)
#su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus notify-send -t 600000 'WARNING:
#Program is not running.
#Shutting down in 10 minutes.'"

sudo /sbin/shutdown -h +10 #Tried with our without sudo

我正在运行MX Linux 18(xfce,基于Debian)。

bash root sudo shutdown notify-send
1个回答
0
投票

我认为它没有关闭sudoers的条目。请在/etc/sudoers.d下创建一个文件sudo并进行以下输入:

[YOURUSER] ALL = (ALL) NOPASSWD: /sbin/shutdown

将[YOURUSER]替换为您的用户帐户!

© www.soinside.com 2019 - 2024. All rights reserved.