You are not logged in.
Hi all,
Sorry if I'm asking a stupid question, I'm new to Arch Linux, Pi, and Python. I have a Python script I'd like to start automatically after boot so I created a myapp.service file:
[Unit]
Description=MyApp daemon
Requires=network.target[Service]
Type=forking
ExecStart=/root/bin/myapp/initmyapp.py
ExecStop=/bin/kill $MAINPID[Install]
WantedBy=multi-user.target
After initmyapp.py runs it calls "myapp.py &" that's why I tried type=forking. I tested "systemctl start myapp" and it looks like it's working; I can see the process running. So, I did "systemctl enable myapp" which ran without errors and then I rebooted. After reboot, however, the process is not running and "systemctl status myapp" tells me this:
myapp.service - MyApp daemon
Loaded: loaded (/etc/systemd/system/myapp.service; enabled)
Active: failed (Result: exit-code) since Wed, 1969-12-31 19:00:10 EST; 43 years and 0 months ago
Process: 102 ExecStart=/root/bin/myapp/initmyapp.py (code=exited, status=1/FAILURE)
CGroup: name=systemd:/system/myapp.service
Dec 31 19:00:09 mybox initmyapp.py[102]: '_open', req)
Dec 31 19:00:09 mybox initmyapp.py[102]: File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
Dec 31 19:00:09 mybox initmyapp.py[102]: result = func(*args)
Dec 31 19:00:09 mybox initmyapp.py[102]: File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
Dec 31 19:00:09 mybox initmyapp.py[102]: return self.do_open(httplib.HTTPConnection, req)
Dec 31 19:00:09 mybox initmyapp.py[102]: File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
Dec 31 19:00:10 mybox initmyapp.py[102]: raise URLError(err)
Dec 31 19:00:10 mybox initmyapp.py[102]: urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
Dec 31 19:00:10 mybox systemd[1]: Failed to start MyApp daemon.
Dec 31 19:00:10 mybox systemd[1]: Unit myapp.service entered failed state
Does this mean that networking is not ready yet? Instead of Requires=network.target I also tried Requires=multi-user.target but didn't work. I have no idea what I'm doing so any suggestion would be appreciated.
Offline
Read the "Requires" section of `man systemd.unit` and http://www.freedesktop.org/wiki/Softwar … workTarget. Also, you don't need that ExecStop line (killing is the default action) and, if possible, call myapp.py directly and don't fork.
Steven [ web : git ]
GPG: 327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com
Offline
Thank you for the quick response. I removed the kill part and "stop" still works. I also removed all the network includes to test it out and the background call too and after hours of trial and error I was finally able to make it work. Now I just have to figure out how to tell when the network is up. That article covers it for C (I think) but not for Python. It also seems very technical.
Anyway, thanks again for your help.
Offline
The article simply explains why 'After=network.target' doesn't work as expected. If you are using NetworkManager, you can enable NetworkManager-wait-online.service (as stated in the article). A more general solution is to modify your script to handle intermittent network connectivity. The simplest way to do this is to wrap your urllib call in a loop that keeps retrying every n seconds.
Steven [ web : git ]
GPG: 327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com
Offline
Thanks. I decided to go with the latter option and try to make each network related call be aware that the network might not be available. Is there any network library for Python? Right now I'm executing Linux commands and parse their output, e.g. I run "ifconfig wlan0" and use regex to get the MAC Address, or "iwlist wlan0 scan" to get the list of available wireless connections. I'm probably doing this horribly wrong.
Offline