I'm trying to run a bash script on boot up.
I tried using the following in crontab:
@reboot bash /home/me/apod.sh
But it doesn't seem to work. I found this post with a similar issue. I tried to follow the instructions of the accepted answer. I.e. I made a file /etc/systemd/system/apod.service
which has the contents:
[Unit]
Description=Set APOD as Desktop
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/bin/bash /home/me/apod.sh
Type=simple
User=me
and then tried systemctl start apod.service
and it worked as expected.
I then did systemctl enable apod.service
to make sure that it runs at boot. But it still doesn't. I'm not sure if I don't have the right permissions for it to run?
My /etc/systemd/system/apod.service
file permissions:
-rwxr-xr-x 1 root root 206 Jun 1 10:52 /etc/systemd/system/apod.service
I'm using Ubuntu 18.04. Can anyone see what I'm doing wrong?
I have also found this post which might be relevant but doesn't seem to have an answer. It's seems surprisingly difficult to get a script to run on boot!
When I run systemctl status apod
after boot:
● 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 I made that is relevant.
My shell script (/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"
and the python script it calls (/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)
Posting my findings as an answer since it's too large for a comment.
Lets take a look at the error shown by systemctl status apod
after boot:
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
This implies that the requests.get()
call fails.
This makes sense, since your systemd script is called before the machine has an active network connection. Therefore, the request will always fail, resulting in an error.
I'd recommend adding an Wants
/ After
network-online.target
to ensure the systemd script is started when the machine has an active network connection:
[Unit]
Wants=network-online.target
After=network-online.target