Bash 服务已启用,但失败。我怎样才能让它运行?

Bash 服务已启用,但失败。我怎样才能让它运行?,bash,service,scripting,systemd,linux-mint,Bash,Service,Scripting,Systemd,Linux Mint,我使用LinuxMint19。我创建了一个简单的脚本“After_suspension”,它将运行三个命令。我试图让它运行时,薄荷从暂停醒来 matthew@matthew-pc:~$ cat /usr/local/bin/After_suspension #!/bin/bash pon dsl-provider sudo service fancontrol start /usr/bin/mailnag matthew@matthew-pc:~$ file /usr/local/bin/Af

我使用LinuxMint19。我创建了一个简单的脚本“After_suspension”,它将运行三个命令。我试图让它运行时,薄荷从暂停醒来

matthew@matthew-pc:~$ cat /usr/local/bin/After_suspension
#!/bin/bash
pon dsl-provider
sudo service fancontrol start
/usr/bin/mailnag

matthew@matthew-pc:~$ file /usr/local/bin/After_suspension
/usr/local/bin/After_suspension: Bourne-Again shell script, ASCII text executable
“mailnag”是
(text/x-python)
。我创建了以下服务文件,该文件已启用但失败。我怎样才能让它运行?我应该使用三个单独的服务文件来运行这三个命令吗

matthew@matthew-pc:~$ cat /etc/systemd/system/After_suspension.service
[Unit]
After=suspend.target

[Service]
ExecStart=/usr/local/bin/After_suspension

[Install]
WantedBy=suspend.target
matthew@matthew-pc:~$ systemctl is-enabled After_suspension.service
enabled
matthew@matthew-pc:~$ systemctl is-active After_suspension.service
failed

matthew@matthew-pc:~$ systemctl status After_suspension.service
● After_suspension.service
Loaded: loaded (/etc/systemd/system/After_suspension.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2019-04-11 01:19:42 HKT; 3min 46s ago
Process: 11655 ExecStart=/usr/local/bin/After_suspension (code=exited, status=1/FAILURE)
Main PID: 11655 (code=exited, status=1/FAILURE)

Apr 11 01:19:11 matthew-pc After_suspension[11655]:   File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 122, in __new__
Apr 11 01:19:11 matthew-pc After_suspension[11655]:     bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
Apr 11 01:19:11 matthew-pc After_suspension[11655]: dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbu
Apr 11 01:19:07 matthew-pc systemd[1]: Started 
After_suspension.service.
Apr 11 01:19:07 matthew-pc systemd[1]: After_suspension.service: Main process exited, code=exited, status=1/FAILURE
Apr 11 01:19:42 matthew-pc pppd[11657]: Timeout waiting for PADO packets
Apr 11 01:19:42 matthew-pc pppd[11657]: Unable to complete PPPoE Discovery
Apr 11 01:19:42 matthew-pc pppd[11657]: Terminating on signal 15
Apr 11 01:19:42 matthew-pc pppd[11657]: Exit.
Apr 11 01:19:42 matthew-pc systemd[1]: After_suspension.service: Failed with result 'exit-code'.
以下是当前的“fancontrol.service”


Systemd以root用户身份运行系统脚本,因此您试图以root用户身份启动GNOME磁盘,在没有X服务器处于活动状态的终端会话中,这就是您可能收到连接被拒绝消息的原因


您可能希望将该服务配置为用户服务(您可能还需要传递
DISPLAY
变量),或者首先尝试在脚本或systemd服务中设置
DISPLAY
变量。

systemd以root身份运行系统脚本,因此您尝试以root身份启动GNOME磁盘,在没有X服务器处于活动状态的终端会话中,这就是您可能收到连接被拒绝消息的原因


您可能希望将该服务配置为用户服务(您可能还需要传递
显示
变量),或者首先尝试在脚本或systemd服务中设置
显示
变量。

@icwebndev,我已经编辑了我的问题。请再读一遍。实际上,我的目标是运行三个命令,而不是启动
GNOME磁盘
。我应该如何修改脚本文件和/或服务文件?@MatthewWai我会避免从其他服务启动风扇服务,因为您可以进行覆盖。对于其他两个,它们需要作为root用户还是用户运行?如果它们必须以用户身份运行,那么最好为您的用户启用后暂停服务,并查看它是否以这种方式运行。
sudo服务fancontrol start
在没有
sudo
的情况下无法运行,因此我认为它需要以root用户身份运行
/usr/bin/mailnag
可以在没有
sudo
的情况下运行,因此我认为它可以作为用户运行。我该怎么办?如何修改服务文件/脚本文件?您可以覆盖fancontrol服务(您可以通过
systemctl edit fancontrol.service
)编辑fancontrol服务,以将其调整为在
suspend.target
之后运行。对于您的服务,将其作为用户服务启用:
systemctl--user enable After_suspension.service
。要测试它是否工作,
systemctl——用户在挂起后启动。服务
。当然,您必须先禁用系统服务。我已经添加了有关“fancontrol.service”的详细信息。请再读一遍我的问题。我希望fancontrol在Linux启动时以及Linux从暂停状态唤醒时启动。我应该为fancontrol创建另一个服务文件吗?@icwebndev,我已经编辑了我的问题。请再读一遍。实际上,我的目标是运行三个命令,而不是启动
GNOME磁盘
。我应该如何修改脚本文件和/或服务文件?@MatthewWai我会避免从其他服务启动风扇服务,因为您可以进行覆盖。对于其他两个,它们需要作为root用户还是用户运行?如果它们必须以用户身份运行,那么最好为您的用户启用后暂停服务,并查看它是否以这种方式运行。
sudo服务fancontrol start
在没有
sudo
的情况下无法运行,因此我认为它需要以root用户身份运行
/usr/bin/mailnag
可以在没有
sudo
的情况下运行,因此我认为它可以作为用户运行。我该怎么办?如何修改服务文件/脚本文件?您可以覆盖fancontrol服务(您可以通过
systemctl edit fancontrol.service
)编辑fancontrol服务,以将其调整为在
suspend.target
之后运行。对于您的服务,将其作为用户服务启用:
systemctl--user enable After_suspension.service
。要测试它是否工作,
systemctl——用户在挂起后启动。服务
。当然,您必须先禁用系统服务。我已经添加了有关“fancontrol.service”的详细信息。请再读一遍我的问题。我希望fancontrol在Linux启动时以及Linux从暂停状态唤醒时启动。我应该为fancontrol创建另一个服务文件吗?
matthew@matthew-pc:~$ cat /lib/systemd/system/fancontrol.service
[Unit]
Description=fan speed regulator
# Run pwmconfig to create this file.
ConditionPathExists=/etc/fancontrol
After=lm-sensors.service
Documentation=man:fancontrol(8) man:pwmconfig(8)

[Service]
ExecStartPre=/usr/sbin/fancontrol --check
ExecStart=/usr/sbin/fancontrol
PIDFile=/var/run/fancontrol.pid

[Install]
WantedBy=multi-user.target