2

I created a service to run my python program at Raspberry Startup. But the program doesn't execute.

My Service File:

[Unit]
Description =Serviço para iniciar o Gateway
After= multi-user.target

[Service]
Type=idle
User=pi
Group=pi
WorkingDirectory = /home/pi/WebSite
ExecStart=/home/pi/WebSite/env/bin/python gateway.py > gateway.log 2>&1

[Install]
WantedBy = multi-user.target

If a type sudo systemctl restart gateway.service ,the program runs normally, but at the startup it doesn'work.

Any ideas? Thanks a lot!

  • Have you enabled it to start at startup via sudo systemctl enable gateway.service? – tobyd Nov 25 '17 at 15:24
  • Yes! I do this. – FelipeFonsecabh Nov 25 '17 at 15:25
  • Anything in sudo journalctl -xe -u gateway.service directly after boot before you've started the application manually? – tobyd Nov 25 '17 at 15:31
  • 1
    Read COMMAND LINES in man systemd.service, specifically the paragraph which begins "This syntax is...", includes the phrase "Specifically, redirection using..." and ends "...are not supported.". – goldilocks Nov 25 '17 at 15:34
  • 1
    I think @goldilocks is on the money here, drop the log redirection, output should end up in the journal anyway. Just have the execution as this ExecStart=/home/pi/WebSite/env/bin/python gateway.py and update the systemd files with sudo systemctl daemon-reload – tobyd Nov 25 '17 at 15:41
5

Systemd services aren't executed by a shell, and although man systemd.service notes the syntax for the various Exec___ directives is "inspired by shell syntax", it also notes:

redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

So you can see there is a problem there. I don't know if that will necessarily mean the service wasn't run, but it will mean it won't log to where you want it to log (see the StandardOutput and StandardError options in man systemd.exec for that).

Of course, this implies it can still be run manually:

If a type sudo systemctl restart gateway.service ,the program runs normally

So likely the spurious redirection is just being ignored, and not the main problem.

This just seems wrong:

[Unit]
After= multi-user.target

[Install]
WantedBy = multi-user.target

If you have reason to believe this should work, fair enough. If it is just something you decided upon, I would use something other than multi-user.target in After, e.g., local-fs.target or networking.target (check to make sure those are in use with systemctl status ____ first).

By the way, you do not mention doing this:

sudo systemctl enable [my.service]

Which you must do if you want it run at boot. You can also check after boot to see what happened:

systemctl status [my.service]

There should be some clue there. Also, every time you change the service file you will need to use sudo systemctl daemon-reload.

Finally, you may want to change the type of the service to either simple or oneshot, depending on whether this is a persistent, long-running program (simple) or something that just configures something quickly then exits (oneshot).

  • I try all options in After parameter and change Type to simple. output of systemctl status gateway.service: gateway.service - Serviço para iniciar o Gateway no boot do Raspberry Loaded: loaded (/etc/systemd/system/gateway.service; enabled; vendor preset: Active: inactive (dead) Curiously, if a type "sudo systemctl start gateway.service" the program works fine. I can't understand. So, the service not starting, and i don't know why. – FelipeFonsecabh Nov 25 '17 at 19:22
  • It might help if you went into more detail (in the question) about what the python script does. – goldilocks Nov 26 '17 at 23:21

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.