使用 systemd 计时器计划 bash 脚本

使用 systemd 计时器计划 bash 脚本

我想定期从包含图片的文件夹中随机更改壁纸。我使用了程序“feh”并用 bash 编写了一个小脚本,名为壁纸脚本.sh:

#!/bin/bash
files=(/torellino/Dropbox/Dropbox/Sfondi/*)
feh --bg-scale ${files[RANDOM % ${#files[@]}]}

当我在终端上使用该脚本时,它可以工作并且壁纸会随机更改。为了使该过程自动化并让脚本每隔几分钟执行一次,我决定使用 systemd 计时器。所以我创建了2个文件并将它们放在/etc/systemd/system/中:

壁纸.服务:

[Unit]
Description=Call wallpaperscript.sh and change the wallapaper

[Service]
ExecStart="/etc/systemd/system/wallpaperscript.sh"

壁纸.定时器:

[Unit]
Description=Runs wallpaperscript every 10 minutes

[Timer]
# Time to wait after booting before we run first time
OnBootSec=1min
# Time between running each consecutive time
OnUnitActiveSec=10min
Unit=wallpaper.service

[Install]
WantedBy=multi-user.target

事实上,这些并不是我自己创造的,而是我在某个地方找到的。无论如何,我启用并启动了wallpaper.timer,但它不起作用。如果我检查“systemctl status wall.timer”,我实际上可以看到计时器正在工作:

● wallpaper.timer - Runs wallpaperscript every 10 minutes
Loaded: loaded (/etc/systemd/system/wallpaper.timer; enabled; vendor preset: disabled)
Active: active (waiting) since Fri 2018-06-01 19:42:40 CEST; 16min ago
Trigger: Fri 2018-06-01 20:03:47 CEST; 4min 14s left

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

另一边的wallpaper.service给了我一个错误:

● wallpaper.service - Call wallpaperscript.sh and change the wallapaper
Loaded: loaded (/etc/systemd/system/wallpaper.service; static; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2018-06-01 19:53:47 CEST; 7min ago
Process: 919 ExecStart=/etc/systemd/system/wallpaperscript.sh (code=exited, status=2)
Main PID: 919 (code=exited, status=2)

我寻找解决方案,但没有找到任何解决方案。顺便说一句,这是我第一次使用 systemd,所以我自己真的无法找出问题所在。

我正在使用 arch-linux 和 i3 如果这有帮助的话

我还得到了 wall.service 的 sderr ,我得到了这个:

“须藤journalctl -u壁纸.服务”

giu 01 19:43:38 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 19:43:39 placobravo wallpaperscript.sh[452]: feh WARNING: You have no HOME, cannot read configuration
giu 01 19:43:39 placobravo wallpaperscript.sh[452]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 19:43:39 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 19:43:39 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 19:53:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 19:53:47 placobravo wallpaperscript.sh[919]: feh WARNING: You have no HOME, cannot read configuration
giu 01 19:53:47 placobravo wallpaperscript.sh[919]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 19:53:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 19:53:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:04:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:04:47 placobravo wallpaperscript.sh[1108]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:04:47 placobravo wallpaperscript.sh[1108]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:04:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:04:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:15:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: /torellino/Dropbox/Dropbox/Sfondi/the does not exist - skipping
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: witcher3.jpg does not exist - skipping
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh: No loadable images specified.
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: See 'feh --help' or 'man feh' for detailed usage information
giu 01 20:15:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=1/FAILURE
giu 01 20:15:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:26:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:26:47 placobravo wallpaperscript.sh[1388]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:26:47 placobravo wallpaperscript.sh[1388]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:26:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:26:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:37:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:37:47 placobravo wallpaperscript.sh[1579]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:37:47 placobravo wallpaperscript.sh[1579]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:37:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:37:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.

据我所知,正如 Jeff Schaller 指出的那样,feh 似乎无法访问 X。有人知道我该如何解决这个问题吗?

答案1

欢迎来到 StackExchange。

您的输出表明feh无法打开 X 服务器。您应该在中设置几个值wallpaper.server

User=

将其设置为您要为其设置壁纸的用户。

Environment=DISPLAY=:0

需要进入显示部分。 这个帖子解释了为什么两者都需要。

相关内容