0

I am trying to make systemd unit that starts a script inside GNU screen headlessly. I did base my work here on a couple of questions some had that were related. I also tested the ExecStart line in a shell as the user it should start for, and it works.

Here is the redacted systemd unit file I created and placed in /usr/lib/systemd/system/devredacted.service...

[Unit]
Description=redacted development site inside screen
After=network-online.target

[Service]
Type=forking
Restart=on-failure
RestartSec=3
User=dev
Group=dev
ExecStart=/usr/bin/screen -L -dmS redacted-dev /home/dev/run.sh

When this is started the process does not appear when I look for it using ps aux | grep screen and when I check the status, I see this...

Oct 16 05:45:30 funbox systemd[1]: Starting devredacted.service - redacted development site inside screen...
Oct 16 05:45:30 funbox systemd[1]: Started devredacted.service - redacted development site inside screen.
Oct 16 05:45:30 funbox systemd[1]: devredacted.service: Deactivated successfully.

I tested this as the user this systemd unit user, which has an interactive shell, and the command executed by the unit file clearly works...

$ /usr/bin/screen -L -dmS redacted-dev /home/dev/run.sh
$ screen -ls
There is a screen on:
        31790.redacted-dev        (Detached)
1 Socket in /run/screen/S-dev.
Kaizushi
  • 1
  • 1
  • 1
    `Type=oneshot` might be a better option for these "session manager"-like programs (screen, tmux etc.) – iBug Oct 16 '22 at 06:55

1 Answers1

0

I almost wanted to delete this but I thought leaving it here would help people looking for help in a similar situation.

The problem was actually in my script run.sh which uses a path to an executable inside the home directory for the user dev, which it runs as. When testing this I was already in /home/dev but when using systemd it was not. This meant my script would exit and GNU screen would close.

The solution was as simple as setting the working directory...

[Service]
WorkingDirectory=/home/dev

This has fixed the problem and everything is fine now.

Kaizushi
  • 1
  • 1