11

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! Commented Dec 13, 2017 at 14:24
  • Is there a particular reason to use Screen at all? Commented 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
    Commented Dec 15, 2017 at 11:35

2 Answers 2

20

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.

0
0

It will work with Type=simple

Use capital D detach.

screen -Dm

man screen

-d -m   Start screen in detached mode. This creates a new session but doesn't attach  to
               it. This is useful for system startup scripts.

-D -m   This  also  starts  screen in detached mode, but doesn't fork a new process. The
               command exits if the session terminates.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .