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

I came across the following question, but this is not what I am looking for. However most of the information is highly 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.

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.

Any suggestions on how this can be done?-

  • The same question you link to has an answer that mentions target dependencies. I have to say the example is not very good / clear, but I do think that is one way of solving your issue. – Dirk Sep 3 '18 at 9:53
  • I have been trying to interpret the answer and implement something similar but I have to admit that I'm struggling to get something to work on my end. – Rrz0 Sep 3 '18 at 9:55
1

Solved

The simplest solution I found was to create another Unit:

[Unit]
Description=Mysite

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

[Install]
WantedBy=multi-user.target

We can enable it using:

rpi ~$ sudo systemctl enable mysite.service

Next we need to edit the myscript.service, such that this runs before mysite.service.

[Unit]
Description=My script

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

[Install]
WantedBy=multi-user.target

The difference between Wants and Requires can be found here.

  • 1
    Hi @Rrz0, well done with making a second Unit. This is the solution. But there is a mistake in myscript.service. For section [Service] is no option Wants= defined. This is only available for section [Unit]. Look at man systemd.unit and at man systemd.service. Add this two options to section [Unit]: After=mysite.service and Wants=mysite.service. That would do the thing. – Ingo Sep 3 '18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.