Systemd 服务不工作

Systemd 服务不工作

有人能弄清楚为什么这个 systemd 单元无法启动吗?

我已将服务置于/etc/systemd/system/startup_actions.service

[Unit]  
Description=Startup actions

[Service]
Type=oneshot 
ExecStart=/usr/local/bin/disable_pgupdw.sh

[Install]
WantedBy=multi-user.target

为了运行它我

sudo systemctl start startup_actions.service 

该单元简单地使用 xmodmap 禁用 pgup 和 down 键,并自行工作:

#!/bin/bash
xmodmap -e 'keycode 112 = NoSymbol'
xmodmap -e 'keycode 117 = NoSymbol'

脚本位于

/usr/local/bin

这是 systemctl status startup_actions 的输出

startup_actions.service - Startup actions
Loaded: loaded (/etc/systemd/system/startup_actions.service; enabled; vendor preset: enable
Active: failed (Result: exit-code) since Sat 2017-11-04 14:15:18 GMT; 1h 21min ago
Process: 2360 ExecStart=/usr/local/bin/disable_pgupdw.sh (code=exited, status=1/FAILURE)
Main PID: 2360 (code=exited, status=1/FAILURE)

当我尝试启动它时,它显示:

Job for startup_actions.service failed because the control process exited with error code.
See "systemctl  status startup_actions.service" and "journalctl  -xe" for details.

答案1

以下单元有效,尽管它不是最佳解决方案

[Unit]  
Description=Startup actions

[Service]
Type=simple
User=USERNAME
Environment=DISPLAY=:0 
ExecStart=/usr/local/bin/disable_pgupdw.sh

[Install]
WantedBy=multi-user.target

事实上,需要在脚本开头添加 sleep 10,这不是很好

#!/bin/bash
sleep 10
xmodmap -e 'keycode 112 = NoSymbol'
xmodmap -e 'keycode 117 = NoSymbol'

正如 Ignacio 指出的那样,xmodmap 需要访问 X 服务器。

Type=simple
User=USERNAME
Environment=DISPLAY=:0

sleep 10

脚本中的操作似乎达到了要求。

答案2

~user/.config/systemd/user/当我将它放入(然后systemctl --user daemon-reload)并使用启动它时,相同的单元对我来说可以工作systemctl --user start startup_actions.service
这样环境变量就可以正确设置。

相关内容