Screen is a powerful tool that allows you to create and manage multiple terminal sessions within a single window. It is commonly used by system administrators and developers to keep processes running even after disconnecting from a remote server. In this article, we will guide you through the process of starting a screen session upon reboot and executing a script in it.
Step 1: Install Screen
If you don't have Screen installed on your system, you can easily install it using the package manager of your operating system. Here are the commands for some popular distributions:
| Distribution | Package Manager | Command |
|---|---|---|
| Ubuntu/Debian | apt | sudo apt install screen |
| CentOS/Fedora | dnf/yum | sudo dnf install screen or sudo yum install screen |
| Arch Linux | pacman | sudo pacman -S screen |
Step 2: Create a Script
In order to execute a script within a screen session, you need to create a script file. This script file will contain the commands you want to run within the screen session. Here's an example script that prints "Hello, World!" to the terminal:
#!/bin/bash
echo "Hello, World!"
Save this script file with a meaningful name, such as hello.sh.
Step 3: Configure the System to Start a Screen Session on Reboot
In order to start a screen session automatically upon reboot, we need to add a command to the system's startup configuration. The exact method varies depending on your operating system.
For Ubuntu/Debian:
Open a terminal and run the following command:
sudo nano /etc/rc.local
This will open the rc.local file in the nano text editor. Add the following line before the exit 0 line:
su -c "screen -dmS session_name /path/to/script.sh" username
Replace session_name with a name for your screen session and /path/to/script.sh with the actual path to your script file. Also, replace username with your username. Save the file and exit the editor.
For CentOS/Fedora:
Open a terminal and run the following command:
sudo nano /etc/rc.d/rc.local
This will open the rc.local file in the nano text editor. Add the following line before the exit 0 line:
su -c "screen -dmS session_name /path/to/script.sh" username
Replace session_name with a name for your screen session and /path/to/script.sh with the actual path to your script file. Also, replace username with your username. Save the file and exit the editor.
For Arch Linux:
Open a terminal and run the following command:
sudo nano /etc/rc.local
This will open the rc.local file in the nano text editor. Add the following line before the exit 0 line:
su -c "screen -dmS session_name /path/to/script.sh" username
Replace session_name with a name for your screen session and /path/to/script.sh with the actual path to your script file. Also, replace username with your username. Save the file and exit the editor.
Step 4: Reboot the System
Now that you have configured your system to start a screen session and execute a script upon reboot, it's time to reboot your system. After the reboot, the screen session will be automatically started and your script will be executed within it.
You can check if the screen session is running by executing the following command:
screen -ls
This will list all the running screen sessions on your system.
Conclusion
Starting a screen session upon reboot and executing a script within it can be a useful technique for automating tasks on your system. By following the steps outlined in this article, you should now be able to configure your system to start a screen session and run a script within it. Happy scripting!
References
| Source | Link |
|---|---|
| Screen Manual | https://www.gnu.org/software/screen/manual/screen.html |
| Ubuntu Documentation | https://help.ubuntu.com/community/UbuntuBootupHowto |
| CentOS Wiki | https://wiki.centos.org/HowTos/Network/SecuringSSH |
| Arch Linux Wiki | https://wiki.archlinux.org/index.php/Arch_User_Repository |