【发布时间】: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