I have a raspberry pi on which I'm running some services. I need to be able to ssh into the machine and monitor those services. Currently, what I do is I log in, run screen, then create three windows and in each window run the script that runs the relevant service. Two of these are actually running as root (one monitors the network, another monitors some hardware connected to the pi). The third is running the actual service, and runs in my user. (The second is basically a proxy for the hardware to talk to the third.)

In any case, this works great, except that when the pi gets rebooted for whatever reason (e.g. power failure), I have to set all this up again, which is tedious.

What I would like is for the system to run screen in my user or startup automatically, with three windows created, two running processes as root and one in my user, such that when I log in, I can connect to that screen session and see everything running live.

How would I do that?

  • 1
    I feel you should really make these 3 processes into services (systemd?) instead of running them in screen. Sep 5 '19 at 4:45

As @DavidDai mentions in his comment, it would be best to run these as system services (e.g. by creating systemd service files or sysvinit startup scripts for them). That can be easier said than done, though.

It is possible to keep doing it with screen. You just need to create a config file for screen, and use screen -c /path/to/screen/config (to override the default ~/.screenrc configfile).

screen -t monitor1   0 sudo /path/to/monitor1.sh args 
screen -t monitor1   1 sudo /path/to/monitor2.sh args 
screen -t server     2 /path/to/service/script.sh

If you want it to start with a particular window active, you can add a line like the following:

select 1

See man screen for details.

You'll also need to configure sudo to allow your user to run both script1.sh and script2.sh as root.

This will not be started automatically when your rpi reboots. You'd have to login and start screen manually. For automatic startup under your uid, you could run it from an @reboot crontab entry (see man 5 crontab for details).

You can attach to this screen session as your user.

Alternatively, you could run a detached screen (e.g. screen -d -m -c /path/to/config) as root (e.g. from /etc/rc.local or cron as above) and dropping sudo from the first two windows, and adding it to the last:

screen -t monitor1   0 /path/to/monitor1.sh args 
screen -t monitor2   1 /path/to/monitor2.sh args 
screen -t server     2 sudo -u username /path/to/service/script.sh

This would be started up automatically whenever the rpi reboots. You would have to be root to attach to this screen session.

Finally, if your monitoring scripts don't need to be run as root, you should run them as a less privileged user. Maybe create a new user and add them to a group with read access to whatever it is they need to monitor.

  • Thanks, that's very helpful. Running as root is needed for the hardware stuff, but the bulk of the logic is not running as root (the root process just proxies to the unprivileged one using a socket). The reason for desiring that this run in screen is so that the processes can be monitored interactively. I agree that in a production environment one would prefer to do this as a true service but it's impractical during development. Sep 6 '19 at 6:45

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.