2

I'm desperate. I'm making a camera project with a Raspberry Pi, I'm running Raspbian Buster there, and for the love of God, I cannot make my scripts run after reboot by using systemd. I can start them just fine, but they will be always inactive (dead) after the reboot.

My units are in the /home/pi/.config/systemd/user/ . And they are:

camera-control.service

[Install] WantedBy=multi-user.target [Unit] Description=Camera position controlling server [Service] ExecStart=python3 "${HOME}/scripts/start.camera_control.server.py" 

And:

camera-stream.service

[Install] WantedBy=multi-user.target [Unit] Description=Camera streaming [Service] ExecStart=bash "${HOME}/scripts/start.stream.sh" 

The python script basically is running the TCPServer with server.serve_forever(), and bash script is this:

mkdir -p "${HOME}/storage" raspivid --nopreview -ih -hf -vf --width 800 --height 600 --framerate 15 --bitrate 2000000 --profile main --timeout 0 -g 4 -o - | gst-launch-1.0 -v fdsrc do-timestamp=true ! h264parse ! tee name=t \ t. ! queue ! rtph264pay config-interval=1 pt=96 ! udpsink host=dom.zaroda.pl port=8004 \ t. ! queue ! splitmuxsink location="${HOME}/storage/%02d.mkv" max-size-time=30000000000 muxer=matroskamux 

Both services correctly run with systemctl --user start camera-control. But they never, ever will run after the reboot.

Now I tried A LOT of things. With every change I'm doing systemctl --user daemon-reload and I'm re-enabling them - I'm making sure that they are always enabled.

  • I tried to add exec before the raspivid.

  • I tried to add

this:

After=network-online.target 

To the Unit section.

and this:

After=network.target 

and this:

Before=network-online.target 

And this:

Wants=network-online.target 

In every configuration I could find on the internet.

I also tried all of this with setting the type of service to: forking and oneshot.

I enabled systemd-networkd service and added to the Unit section things like:

After=systemd-networkd-wait-online.service Requires=systemd-networkd-wait-online.service 

All of the above in countless configurations. It just won't work. Everytime I will reboot with these two services enabled, I'm getting the same states with systemctl --user status camera-stream and systemctl --user status camera-control :

● camera-stream.service - Camera streaming Loaded: loaded (/home/pi/.config/systemd/user/camera-stream.service; enabled; vendor preset: enabled) Active: inactive (dead) ● camera-control.service - Camera position controlling server Loaded: loaded (/home/pi/.config/systemd/user/camera-control.service; enabled; vendor preset: enabled) Active: inactive (dead) 

There is nothing in journalctl.

I must be missing something embarrassingly simple. Do you have any suggestions what else I can try to make it work? I feel like I brute forced every configuration of the above mentioned settings imaginable.

Added #1:

One more thing, RPi is being connected to the network by wifi (wlan0). It might or may not be important here.

Added #2:

Services actually work as expected when added to the /etc/systemd/system/ and run "globally". But they refuse to start on boot when run as user, and that's what I'm after.

Output of sudo loginctl show-user pi:

UID=1000 GID=1000 Name=pi Timestamp=Wed 2020-01-01 14:10:16 CET TimestampMonotonic=8249110 RuntimePath=/run/user/1000 [email protected] Slice=user-1000.slice Display=c1 State=active Sessions=c1 IdleHint=no IdleSinceHint=1577884640207702 IdleSinceHintMonotonic=421820778 Linger=yes 

1 Answer 1

4

The reason behind this is because this is a service that runs underneath the pi user. As noted in the path of the service: /home/pi/.config/systemd/. This will only trigger the service up-on login.

In order for the a user service to work you need to enable linger for user pi by running the following command:

sudo loginctl enable-linger pi 

For more information, see man pages for loginctl.

Also, note that multi-user.target is not a valid service for user service, you need to change WantedBy to default.target so that it becomes:

WantedBy=default.target 

Now the service will boot on startup for the pi-user without login.

4
  • Thanks for the suggestion, I didn't know about that, but unfortunately enabling linger didn't change anything. I just tried with linger enabled multiple variations of service/unit configuration to the same effect. Commented Jan 1, 2020 at 12:41
  • I just checked that these services work correctly when added to the /etc/systemd/system/. They just don't work when run as user :/ . Commented Jan 1, 2020 at 13:04
  • 1
    Actually, thanks to your suggestion I figured out what is wrong, and explanation is here: github.com/systemd/systemd/issues/2690#issuecomment-186973730 . So it looks like both things are required: enabled linger and WantedBy=default.target You can add it to your answer and I will gladly accept it, thanks! :) Commented Jan 1, 2020 at 14:22
  • @ŁukaszZaroda I've updated the solution. Thanks for the clarification! Commented Jan 2, 2020 at 0:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.