This question already has an answer here:

I'm getting very annoyed here right now. I've been trying to get something very easy done for way too long now.

I am running a python script on my Pi Zero W (in CLI) to control my smart lights, this means, of course, that the script is using network access right off the bat. I want to be able to control and monitor this script directly on the pi (via keyboard and monitor) and via an external pc via ssh. I've made this work using screen. Now I want the Pi to automatically create the screen socket and execute the python script.

To do this manually I'm successfully using the following command:

screen -S startup /home/pi/launcher

I'm using the name startup to connect to the correct screen on the separate pc.

/home/pi/launcher is a separate file that simply calls the python script.

I can't find any way to execute this single line of code automatically in such a way that things work. I think I've tried every method on Google, none with any success.

marked as duplicate by Community Nov 10 '17 at 16:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Thanks to Aurora0001 I was able to make it work using /etc/profile. This is a file that runs everytime a user is logged in. I added the following code to the end of that file:

LIST="$(screen -list | grep $NAME)"

if [ -z "${LIST}" ]
screen -S $NAME /home/pi/launcher
screen -x $NAME

First I get a LIST of all active screen elements using screen -list. Then I use grep to remove everything from that list except lines containing the screen NAME I need.

If the script doesn't find an active screen with the desired NAME in LIST then I start a screen with that name running home/pi/launcher inside it using screen -S $NAME /home/pi/launcher

If the script does find an active screen with the desired NAME in LIST then I'll enter it in multiscreen mode using screen -x $NAME

Not the answer you're looking for? Browse other questions tagged or ask your own question.