8

Im currently running a python script inside a Screen session through this script in rc.local:

screen -L -dmS pi python /home/pi/truck/main2.py &

But I would like it to run through systemd so I can restart it if the Python script crashes.

I tried creating a service in /systemd/system/myscript.service

[Unit]
Description=myscript
After=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=3
ExecStart=/usr/bin/screen -L -dmS pi  python /home/pi/truck/main2.py

[Install]
WantedBy=multi-user.target

But it wont run my script inside the Screen session?

If I get a list of running screens it shows that the screen session "pi" is dead.

3
  • See me on unix.stackexchange.com, I would be glad to help you there! Dec 13, 2017 at 14:24
  • Is there a particular reason to use Screen at all?
    – user1686
    Dec 13, 2017 at 15:50
  • I'm running a Raspberry Pi headless and would like to SSH into my script. That's why I'm using Screen.
    – TwinToe
    Dec 15, 2017 at 11:35

1 Answer 1

15

Type=simple expects the service's main process to continue running forever. Your actual command, however, is telling Screen to do the opposite – start a new session and fork to background.

When Screen does that, systemd assumes the service has exited, and cleans up all leftover processes – including the "backgrounded" Screen process. (This doesn't even count as a "failure".)

To avoid that, either change type to forking, or the Screen -dm option to -Dm.

1
  • Changing the Type to forking did the trick. Thanks!
    – TwinToe
    Dec 15, 2017 at 12:16

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.