0

I am running Raspbian GNU Linux (9) Stretch, on my Raspberry Pi 3.

Project Overview

  1. Python script which makes use of 2 inputs to the Pi. Calculations are performed based on these inputs, and values are stored in a database.
  2. A separate Django website which reads values from this database and updates the content of the web page.

Django Server run using: /home/pi/mysite/manage.py runserver 127.0.0.1:8000

Python Script located at: /home/pi/Desktop/myscript.py

On startup I would like to first make sure that my Django server is up and running and then start my python script.

What I have done so far

Initially I started by trying to get the python script to run on startup. To do this I am using a simple systemd service as follows.

[Unit]
Description=My script

[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/myscript.py

[Install]
WantedBy=multi-user.target

This runs without error and initiates the python script on startup.

However, I cannot get my Django server up and running inside the same systemd service since only one ExecStart is allowed. I need both to be run in parallel, but the Django up to be started first.

I think I am looking for something like Wants.

I came across the following question, but I have not managed to implement a working solution. However most of the information is relevant.

if Type=simple in your unit file, you can only specify one ExecStart, but you can add as many ExecStartPre,ExecStartPost, but none of this is suited for long running commands, because they are executed serially and everything one start is killed before starting the next one. If Type=oneshot you can specify multiple ExecStart, they run serially not in parallel.

I tried creating another unit as follows:

sudo systemctl edit --force mysite.service


[Unit]
Description=my site

[Service]
ExecStart=/usr/bin/python /home/pi/mysite/manage.py runserver 127.0.0.1:8000

[Install]
WantedBy=multi-user.target

This works on it's own.

However I need both to be run in parallel, but the Django up to be started first.

That's why I edited myscript.serviceas follows:

[Unit]
Description=My script

[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/Scripts/oee_calc.py
Wants=mysite.service

[Install]
WantedBy=multi-user.target

The python script is initiated, but the django server is not.

Any suggestions on how this can be done?

rrz0
  • 2,182
  • 5
  • 30
  • 65
  • 2
    Given that these are two different services, why not just use two different unit files? – Kendas Sep 03 '18 at 10:12
  • 1
    How would I go about executing them at a specific order on startup ? – rrz0 Sep 03 '18 at 10:17
  • 1
    You can specify dependencies in systemd units. – syntonym Sep 03 '18 at 10:19
  • 2
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Sep 03 '18 at 10:47
  • Thanks @jww, I will keep this in mind for future cases. – rrz0 Sep 03 '18 at 10:53

2 Answers2

2

What you need are 2 different systemd unit and define the dependency using requires

[Unit]
Description=My script

[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/myscript.py
Requires=dhangioserver.service 

[Install]
WantedBy=multi-user.target

It will also be good to specify RequiredBy in DjangoService unit

There is a related spec called Wants which differs only in whether service should continue if dependendcy fails or not. Looking at your requirement , it looks like you need Requires and not Wants

rajeshnair
  • 1,587
  • 16
  • 32
  • Ah, thanks I had just edited my question, thinking that I needed something like `Wants`. I will try this and get back. According to the following site I may need to use `Wants` since I would like my python script to run even when there is an issue with Django. https://fedoramagazine.org/systemd-unit-dependencies-and-order/ – rrz0 Sep 03 '18 at 10:37
  • Ok , if your python script is not dependent on successful start of django service, you need `Wants` and not `Requires`. I would suggest be to give them a useful name service name. – rajeshnair Sep 03 '18 at 10:47
0

You can try this in a single unit

ExecStart=sh -c "python script1.py & python script2.py" 
LiberiFatali
  • 185
  • 11