User avatar
Elliott B
Posts: 92
Joined: Thu Jan 09, 2014 11:58 pm

systemd service won't run at startup

Wed Jul 18, 2018 8:52 am

I've created a startup service to stream a camera, and it's not loading at startup, but it does work if I start it manually After a reboot, the service is dead:

Code: Select all

$ sudo systemctl status stream
● stream.service - Stream raspicam
   Loaded: loaded (/etc/systemd/system/stream.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
If I start it manually, the command stalls and I have to hit Ctrl-C. After doing this, it says "activating" forever, but it works.

Code: Select all

$ systemctl status stream
● stream.service - Stream raspicam
   Loaded: loaded (/etc/systemd/system/stream.service; disabled; vendor preset: enabled)
   Active: activating (start) since Wed 2018-07-18 01:47:53 PDT; 1min 19s ago
Cntrl PID: 423 (sh)
   CGroup: /system.slice/stream.service
           ├─423 /bin/sh -c /usr/bin/raspivid -n -ih -t 0 -rot 180 -w 1280 -h 720 -b 1000000 -fps 30 -o - | nc -lkv4 
           ├─424 /usr/bin/raspivid -n -ih -t 0 -rot 180 -w 1280 -h 720 -b 1000000 -fps 30 -o -
           └─425 nc -lkv4 8080

Jul 18 01:47:53 raspberrypi systemd[1]: Starting Stream raspicam...
Jul 18 01:47:53 raspberrypi sh[423]: Listening on [0.0.0.0] (family 2, port 8080)
Here is my service:

Code: Select all

[Unit]
Description=Stream raspicam

[Service]
Type=forking
ExecStart=/bin/sh -c "/usr/bin/raspivid -n -ih -t 0 -rot 180 -w 1280 -h 720 -b 1000000 -fps 30 -o - | nc -lkv4 8080"
Restart=on-abnormal
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

epoch1970
Posts: 1838
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: systemd service won't run at startup

Wed Jul 18, 2018 9:06 am

With an absolute path to "nc"?
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
DougieLawson
Posts: 33314
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: systemd service won't run at startup

Wed Jul 18, 2018 9:46 am

Try creating these three services

/etc/systemd/system/raspivid.service

Code: Select all

[Unit]
Description=raspivid process
Requires=netcat.socket
After=netcat.socket
Wants=netcat.service

[Service]
TimeoutStartSec=infinity
Sockets=netcat.socket
StandardOutput=fd:netcat.socket
StandardError=journal
ExecStart=/usr/bin/raspivid -n -ih -t 0 -rot 180 -w 1280 -h 720 -b 1000000 -fps 30 -o -

Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
/etc/systemd/system/netcat.service

Code: Select all

[Unit]
Description=netcat process
Requires=netcat.socket
After=netcat.socket

[Service]
TimeoutStartSec=infinity
Sockets=netcat.socket
StandardInput=fd:netcat.socket
StandardError=journal
ExecStart=/bin/nc -lkv4 8080
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
/etc/systemd/system/netcat.socket

Code: Select all

[Unit]
Description=netcat.service reads from this

[Socket]
ListenFIFO=/run/netcat.sock
SocketMode=0600
RemoveOnStop=false

[Install]
WantedBy=multi-user.target
Note: I've not tested that stuff.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
Elliott B
Posts: 92
Joined: Thu Jan 09, 2014 11:58 pm

Re: systemd service won't run at startup

Thu Jul 19, 2018 6:50 am

Thanks Dougie! That works. I'll have to read up on all those options. I kind of feel weird about using three services instead of one, do you think this is a recommended practice?
I realized I just made a mistake of not enabling my service the first time. So if I do it in one forking service as I wrote before, it actually does work, the only "problem" is that systemctl status stream.service always shows "activating" forever.

User avatar
DougieLawson
Posts: 33314
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: systemd service won't run at startup

Thu Jul 19, 2018 7:43 am

It's the systemd way of doing plumbing. You're sending the standard output (stdout) from the raspivid process through the socket process into the standard input (stdin) of the netcat process. It's ugly but you can stop and restart the three processes without breaking things.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Raspbian”

Who is online

Users browsing this forum: No registered users and 28 guests