配置 systemd 以管理服务
Configure systemd to manage a service
我正在尝试 运行 启动时的 bash 脚本。
我尝试在 crontab 中使用以下内容:
@reboot bash /home/me/apod.sh
但是好像不行。我发现 有类似的问题。我试图按照已接受答案的说明进行操作。 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
我说的是相关的。
我的 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
开机后显示的错误:
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
这意味着 requests.get()
调用失败。
这是有道理的,因为您的 systemd 脚本在计算机具有活动网络连接之前被调用。所以,请求总会失败,导致报错。
我建议添加 Wants
/ After
network-online.target
以确保当机器有活动网络连接时启动 systemd 脚本:
[Unit]
Wants=network-online.target
After=network-online.target
我正在尝试 运行 启动时的 bash 脚本。
我尝试在 crontab 中使用以下内容:
@reboot bash /home/me/apod.sh
但是好像不行。我发现 /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
我的 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
开机后显示的错误:
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
这意味着 requests.get()
调用失败。
这是有道理的,因为您的 systemd 脚本在计算机具有活动网络连接之前被调用。所以,请求总会失败,导致报错。
我建议添加 Wants
/ After
network-online.target
以确保当机器有活动网络连接时启动 systemd 脚本:
[Unit]
Wants=network-online.target
After=network-online.target