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?

5
  • 2
    Given that these are two different services, why not just use two different unit files? Commented Sep 3, 2018 at 10:12
  • 1
    How would I go about executing them at a specific order on startup ? Commented Sep 3, 2018 at 10:17
  • 1
    You can specify dependencies in systemd units. Commented Sep 3, 2018 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 in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Commented Sep 3, 2018 at 10:47
  • Thanks @jww, I will keep this in mind for future cases. Commented Sep 3, 2018 at 10:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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. fedoramagazine.org/systemd-unit-dependencies-and-order
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.
0

You can try this in a single unit

ExecStart=sh -c "python script1.py & python script2.py" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.