Search code examples
shsystemdsystemctl

Systemd configuration to run script as service


If I run /home/pi/update.sh everything works fine; If I run it via systemd service, script will not be executed.

Content of script:

/home/pi/update.sh

#!/bin/sh
script='/opt/server/htdocs/due.py'
/usr/bin/python $script &

Here you are Systemd config: Is there any mistake within code below?

/lib/systemd/system/httpdevice.service
/etc/systemd/system/multi-user.target.wants/httpdevice.service

    [Unit]
    Description=httpdevice-service

    [Service]
    Type=simple
    User=pi
    ExecStart=/home/pi/update.sh

    [Install]
    WantedBy=multi-user.target

Here status:

sudo systemctl status httpdevice.service

● httpdevice.service - httpdevice
   Loaded: loaded (/lib/systemd/system/httpdevice.service; enabled)
   Active: inactive (dead) since Fri 2018-04-27 18:01:52 CEST; 17min ago
  Process: 402 ExecStart=/home/pi/update.sh (code=exited, status=0/SUCCESS)
 Main PID: 402 (code=exited, status=0/SUCCESS)

Apr 27 18:01:51 raspberrypi systemd[1]: Starting httpdevice...
Apr 27 18:01:51 raspberrypi systemd[1]: Started httpdevice.

Solution

  • The problem here is Type=simple. Your service definitely started, and then quit. However, you're probably misled by the child Python process. Child processes are not part of a "simple" service.

    It might be simpler to just have ExecStart=/opt/bin/python opt/server/htdocs/due.py and skip the whole script.