【问题标题】:Configure systemd to manage a service配置 systemd 来管理服务
【发布时间】:2021-08-19 13:02:39
【问题描述】:

我正在尝试在启动时运行 bash 脚本。

我尝试在 crontab 中使用以下内容:

@reboot bash /home/me/apod.sh

但它似乎不起作用。我发现this post 有类似的问题。我尝试按照已接受答案的说明进行操作。 IE。我制作了一个文件/etc/systemd/system/apod.service,内容如下:

[Unit]
Description=Set APOD as Desktop

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash /home/me/apod.sh
Type=simple
User=me

然后尝试systemctl start apod.service,它按预期工作。

然后我做了systemctl enable apod.service 以确保它在启动时运行。但它仍然没有。我不确定我是否没有运行它的正确权限?

我的/etc/systemd/system/apod.service 文件权限:

-rwxr-xr-x 1 root root 206 Jun  1 10:52 /etc/systemd/system/apod.service

我使用的是 Ubuntu 18.04。谁能看到我做错了什么?

我还找到了this post,这可能是相关的,但似乎没有答案。让脚本在启动时运行似乎非常困难!


当我在启动后运行systemctl status apod

● apod.service - Set APOD as Desktop
   Loaded: loaded (/etc/systemd/system/apod.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2021-06-01 12:02:17 BST; 1min 31s ago
  Process: 817 ExecStart=/bin/bash /home/me/apod.sh (code=exited, status=0/SUCCESS)
 Main PID: 817 (code=exited, status=0/SUCCESS)

Jun 01 12:02:17 me-XPS-15-9500 bash[817]:   File "/usr/lib/python2.7/urllib2.py", line 447, in _open
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:     '_open', req)
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:   File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:     result = func(*args)
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:   File "/usr/lib/python2.7/urllib2.py", line 1248, in https_open
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:     context=self._context)
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:   File "/usr/lib/python2.7/urllib2.py", line 1205, in do_open
Jun 01 12:02:17 me-XPS-15-9500 bash[817]:     raise URLError(err)
Jun 01 12:02:17 me-XPS-15-9500 bash[817]: urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
Jun 01 12:02:17 me-XPS-15-9500 gsettings[1077]: failed to commit changes to dconf: Could not connect: No such file or directory

Here is a previous post我做的是相关的。


我的 shell 脚本(/home/me/apod.sh):

#!/bin/sh                                                                                                 

export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"

python /home/me/apod.py

/usr/bin/gsettings set org.gnome.desktop.background picture-uri "file:///home/me/Downloads/apod.jpg"

以及它调用的 python 脚本 (/home/me/apod.py):

from bs4 import BeautifulSoup as BSHTML
import requests
import subprocess
import urllib2
page = urllib2.urlopen('https://apod.nasa.gov/apod/astropix.html')
soup = BSHTML(page,features="html.parser")
images = soup.findAll('img')

url = 'https://apod.nasa.gov/apod/'+images[0]['src']
r = requests.get(url, allow_redirects=True)
with open('/home/me/Downloads/apod.jpg',"w") as f:
            f.write(r.content)

【问题讨论】:

  • 请告诉我启动后systemctl status apod 的输出是什么...
  • 我将发布我的问题的编辑 :)
  • 如您所见,systemd 尝试启动您的服务,但出现错误。
  • 您是否在apod.sh 脚本中使用了相对路径?
  • 从另一个文件夹调用时也能正常运行吗?

标签: bash cron ubuntu-18.04 systemd


【解决方案1】:

发布我的发现作为答案,因为它太大而无法发表评论。


我们看看systemctl status apod显示的错误开机后

urllib2.URLError:

这意味着requests.get() 调用失败。

这是有道理的,因为您的 systemd 脚本被调用在机器有一个活动的网络连接之前。因此,请求总是会失败,从而导致错误。

我建议添加Wants / After network-online.target 以确保在机器具有活动网络连接时启动 systemd 脚本:

[Unit]
Wants=network-online.target
After=network-online.target

【讨论】:

  • 它曾经工作过,但现在我又遇到了麻烦。当我运行systemctl status apod 时,我得到Jun 03 18:17:01 systemd[1]: Started Set APOD as Desktop. Jun 03 18:17:03 gsettings[1584]: failed to commit changes to dconf: Could not connect: No such file or directory
  • 我是否还需要让 systemd 脚本等到所有文件都可用后再尝试使用其中一个文件设置桌面背景?
  • 该错误听起来更像是 gsettings 特定的错误。我对此并不熟悉。也许this askubuntu answer 可能有用。或者尝试更多地使用google,也许检查systemd启动脚本的用户是否没有正确的权限?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多