Screen. Run a script on startup in a detached screen

The screen utility provides a way to run a command on a Linux system and then detach from it. This is a particularly useful command if you have a long-running process that you want to keep running even after you log out and still have the ability to reattach to it later and/or from another location.

 

How to use ‘screen’ command on Linux systems

There are many good tutorials available on the web describing the use of screen, check the references to find some good ones.

 

I want to show you here only a basic usage of screen, only to run a script on a machine like a server or a Raspberry Pi.

 

The first step is to install screen on the machine:

$ sudo apt install screen # ubuntu based distro

$ sudo dnf install screen # fedora distro

Here the case of the script workscript.sh. So, edit the startup rc.local file:

$ sudo vi /etc/rc.local

And add the following line before the ‘exit 0′ command.

# Run a command as `username` from the home folder
su - username -c "screen -dm -S ~/workscript.sh"

Remember that the script must be set to executable:

$ chmod +x ~/workscript.sh

Reboot the machine and check the script running with:

$ screen -DR

If the machine is a remote machine, you need to ssh to.

 

References

Screen User’s Manual – https://www.gnu.org/software/screen/manual/screen.html

In Unix, what is screen, and how do I use it? – https://kb.iu.edu/d/acuy

How do I use screen on the Linux systems? – https://uisapp2.iu.edu/confluence-prd/pages/viewpage.action?pageId=115540034

Leave a Reply

Your email address will not be published.