3

Noob question, but hopefully someone can help me out here. I'm transitioning from init.d to systemd on my RPi running Linux, as I'm told it would cut my boot time in half.

My init script requires sudo as it needs to access GPIO components.

init.d service

My /etc/init.d/hue command looked like this before:

sudo python /home/pi/hue/app.py

systemd service

[Unit]
Description=Hue Controller

[Service]
Type=simple
ExecStart=sudo /usr/bin/python /home/pi/hue/app.py

[Install]
WantedBy=multi-user.target

When I run $ systemctl start hue.service the following error message:

Failed to issue method call: Access denied

I've been tinkering with things for hours now and I haven't gotten around this issue. I've tried putting the ExecStart script in another bash file and pointing to that, changing my etc/sudoers to allow all for root, but nothing is working.

Any ideas?

3
  • Why not sudo systemctl start hue.service? I've not noticed systemd making any difference to my boot times. I have a fairly standard system and it still takes 45 seconds before my userland stuff starts on the Pi.
    – joan
    Jan 11, 2015 at 9:01
  • Agree w/ joan that systemd will probably not make such a big difference to boot times on a single core system (I haven't noticed one either). It is somewhat nicer to use than the old debian twist on SysV though.
    – goldilocks
    Jan 11, 2015 at 14:04
  • Note that further questions about systemd are more appropriate to the Unix & Linux Exchange.
    – goldilocks
    Jan 11, 2015 at 14:13

2 Answers 2

4

Init services run as root. Do not include sudo in service files run by init, regardless of whether it is SysV or systemd.

I would try [but please see comments, this is a bit personal preference]:

[Unit]
Requires=local-fs.target
After=local-fs.target

[Service]
Type=forking
ExecStart=/home/pi/bin/hue.sh

[Install]
WantedBy=default.target

Then in /home/pi/bin/hue.sh:

#!/bin/bash

export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
python /home/pi/hue/app.py &> /dev/null &

Note the & at the end to ensure this forks, since we have said this is forking. If app.py does this itself (i.e., daemonizes), you could invoke it directly from ExecStart.

You might prefer to replace &> /dev/null w/ &> /tmp/app.py.log for debugging purposes.

5
  • Yes, thank you so much! Just a note, it needs to have [Install] WantedBy=multi-user.target at the end of the service file or it will throw an error. Also, what does local-fs.target mean for "Requires" and "After"?
    – Nick Jonas
    Jan 11, 2015 at 18:29
  • Yeah, I suppose that's not complete -- I meant I'd include at least those fields and try forking instead of simple. WRT to local-fs.target, this is to make sure it's run after the root filesystem is mounted read-write. Most things do, but without that there's no guarantee.
    – goldilocks
    Jan 11, 2015 at 18:51
  • 1
    A lot of the changes you recommended are not necessary. systemd does not require that services fork, in fact it's much easier with systemd to not fork, just keep the service type simple. Also, don't bother redirecting the output to /dev/null or to a file, systemd automatically collects all of that and stores it in the journal so that you can see what's going on with your service. The only change that was really needed was dropping the sudo from the ExecStart.
    – jcollie
    May 3, 2015 at 1:31
  • @jccollie I admit my understanding of systemd is far from perfect and you are certainly correct by the man page -- I think I came to use this model via experimentation and it has been the most foolproof one for me; I've been through some personal frustration getting plain jane services such as this to work under systemd (I still like it more than SysV though)...
    – goldilocks
    May 3, 2015 at 10:32
  • ...However, I don't think "not necessary" amounts to incorrect/inefficient/somehow problematic here though (beyond an extra shell that could be fixed with exec), so I've let it stand but removed the "must fork" bit and added a reference to these comments. Thank you for your answer, BTW.
    – goldilocks
    May 3, 2015 at 10:32
2

The only change that should be necessary is to drop the sudo from the ExecStart line:

[Unit]
Description=Hue Controller

[Service]
Type=simple
ExecStart=/usr/bin/python /home/pi/hue/app.py

[Install]
WantedBy=multi-user.target

Your Answer

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

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