0

I want to start a service with a screen command in detached more. The backgound is, that it shall read a serial interface and with reconnecting to the screen I want to be able to interact in case of errors or so to send commands via the serial interface.

Currently I have

systemd service file

[Unit]
Description=read serial interface

[Service]
User=someUser
Group=someUser
Type=forking
Restart=on-failure
RestartSec=3
WorkingDirectory=/home/someUser/dev
ExecStart=/usr/bin/screen -DmS serial-magic /home/someUser/dev/run-serial-magic.sh

[Install]
WantedBy=multi-user.target

Script under /home/someUser/dev/run-serial-magic.sh

#!/bin/bash

pio device monitor -b 115200 -p /dev/ttyUSB0 | tee test.log

I already tried

  • moving the screen inside the script.
  • using Type=simple with screen option -d instead of -D

The script is working if I call it manually from the command line. I can return to the screen and detach it again. But when I start the service sudo journalctl -f -u someServoce.service just gives me

Okt 23 18:13:28 someHost systemd[1]: Starting read serial interface...

But screen -list says

No Sockets found in /run/screen/S-someUser.
1
  • forking should be the correct option and can be used along with /usr/bin/screen -dmS, see: superuser.com/a/1276822/645522. Silly question, but are you logging as someUser to check that your service is running ? You will only see screen sessions that belong to you. – Anonymous Oct 23 '20 at 22:21
0

In this case, working on the idea that you've verified the screen is actually running, I'm making the assumption that "someUser" in your service file is not infact the same someUser in your screen -list error. In this case you should enable multiuser support within your screen.

Since you intend to use this like a service, you should

    echo "multiuser on" >> /home/<service user>/.screenrc
    echo "acladd <user who needs access to screen>" >> /home/<service user>/.screenrc

This will add it as a default setting for the service user's screen. If you only use this user for the service it would be more advisable to create a seperate config file for screen and invoke it using

    /usr/bin/screen -c /path/to/config.conf -DmS serial-magic /home/someUser/dev/run-serial-magic.sh
    

In your service file's "ExecStart="

You will then be able to access the screen using screen -x <service user>/serial-magic

I've verified this in my test server which is RHEL 7.

4
  • Thanks for the reply, but this does not solve my issue. someUser is me. It is the same user that is defined in the service and that runs the command screen -list. There is no screen process running at all. It is not that some user cant see it. It is not running. I can switch the user to e.g. root with the same result. – SolvedForHome Oct 23 '20 at 22:19
  • The (deleted) hint with the script content was helpful. The env is apparently different when screen is triggered, so my application pio was not found but gave no error. Absolute path to the binary did the trick. Thanks for the help. ;) – SolvedForHome Oct 23 '20 at 22:31
  • Lol I was going to go home and grab platformio once I got off work to test it. I was pretty sure there was an environment difference since the script died lol. Glad you got it working :P – ReedGhost Oct 23 '20 at 23:38
  • I will examine how the environment is loaded differently from bash and from systemd calling with the same user and write an update to my post. I guess .bashrc is not sourced on the systemd call but from screen it is. – SolvedForHome Oct 24 '20 at 12:34

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.