Systemd 服务用于在启动时从网络获取数据

Systemd 服务用于在启动时从网络获取数据

我想在/usr/share/wallpapers每次重新启动笔记本电脑时从 unsplash 中获取一张随机图片。

我第一次尝试编写了此服务:

[Unit]
Description=Fetch random picture from unsplash
After=network-online.target

[Service]
User=root
Type=idle
ExecStart=/home/nicolas/.local/bin/fetch_unsplash_1920x1080

[Install]
WantedBy=network-online.target

我启用了它,重新启动,我发现它失败了,因为:

déc. 19 13:21:24 localhost.localdomain systemd[1]: Started Fetch random picture from unsplash.
déc. 19 13:21:24 localhost.localdomain systemd[1728]: fetch_unsplash.service: Failed to execute command: Permission denied
déc. 19 13:21:24 localhost.localdomain systemd[1728]: fetch_unsplash.service: Failed at step EXEC spawning /home/nicolas/.local/bin/fetch_unsplash_1920x1080: Permission denied
déc. 19 13:21:24 localhost.localdomain systemd[1]: fetch_unsplash.service: Main process exited, code=exited, status=203/EXEC
déc. 19 13:21:24 localhost.localdomain systemd[1]: fetch_unsplash.service: Failed with result 'exit-code'.
~

脚本路径正确且有执行权限。

这是代码(也是未来改进的基础):

#!/usr/bin/bash

location="$1"
if [ -z "$location" ]
then
    location="/usr/share/wallpapers"
fi
name="random_unsplash_1920x1080.jpg"
wget -q "https://picsum.photos/1920/1080/?random" --output-document "$location/$name"

那么什么不起作用呢?

这也给我带来了两个问题:如何授予服务写入权限/usr/share/wallpapers?如何仅在笔记本电脑真正连接到互联网时才启动服务(我在这项服务中做得对吗?)?

相关内容