Ubuntu:
Source: Notion | Last edited: 2024-10-31 | ID: d8246535-ebc...
2024-10-30 Forget about screen on this Notion page.
Section titled “2024-10-30 Forget about screen on this Notion page.”Highly suggest using the Tmux : Tmux persistent sessions even after Cursor / VSCode IDE SSH disconnected (~7min video)
Section titled “Highly suggest using the Tmux : Tmux persistent sessions even after Cursor / VSCode IDE SSH disconnected (~7min video)”(Using screen, Random Ping Interval, and Loginctl Configuration)
Section titled “(Using screen, Random Ping Interval, and Loginctl Configuration)”This tutorial will guide you through the steps to keep a process running even after logging out of your Ubuntu session using the screen utility. Additionally, you will learn how to ping www.google.com at random intervals between 3 and 10 seconds without creating a script, and how to set up the system to automatically enable lingering with loginctl upon startup.
Prerequisites
Section titled “Prerequisites”- Ubuntu or any Linux distribution.
- Basic command-line knowledge.
Step 1: Install screen
Section titled “Step 1: Install screen”To ensure that your processes continue running after you log out, you’ll need to install the screen utility. screen allows you to start a terminal session and then detach it, leaving the process running in the background.
- Open a terminal.
- Install
screenby running the following command:
sudo apt-get updatesudo apt-get install screenStep 2: Start and Detach a screen Session
Section titled “Step 2: Start and Detach a screen Session”- To start a
screensession, simply type:
screen- Run your desired command inside the
screensession. For example, you can start a process or command that you want to keep running. - Detach the
screensession by pressingCtrl + A, followed byD.
Step 3: Reattach to a screen Session
Section titled “Step 3: Reattach to a screen Session”If you want to return to your detached session, you can reattach it using:
screen -rIf there are multiple sessions, you may need to specify the session ID. You can list all screen sessions by running:
screen -lsThis command will show a list of available screen sessions. To reattach to a specific session, use:
screen -r session_idReplace session_id with the appropriate ID shown in the screen -ls output.
Example Output of screen -ls:
screen -listThere are screens on: 24273.pts-4.2080ti (2024-08-15 01:37:05 AM) (Detached) 19376.pts-4.2080ti (2024-08-15 01:33:08 AM) (Attached)2 Sockets in /run/screen/S-terrylica.In this example, two sessions are running. One is detached, and the other is attached. You can reattach to a detached session using the session ID.
Step 4: Prevent Process Termination After Logout Using loginctl
Section titled “Step 4: Prevent Process Termination After Logout Using loginctl”To prevent processes from being killed after logging out, you can enable lingering for your user account. This will allow user processes to continue running even after you log out.
- Run the following command:
sudo loginctl enable-linger $(whoami)Note: This step is for reference only for the Little Black (Fractal Design) Ubuntu workstation since the lingering setup has already been automated in Step 5.
Step 5: Automate Lingering at Startup
Section titled “Step 5: Automate Lingering at Startup”To ensure that lingering is always enabled when your system starts up, you can add this command to your startup applications.
This setup has already been completed on the Little Black (Fractal Design) Ubuntu workstation, but the instructions are retained here for reference.
- Open Startup Applications Preferences from your system settings.
- Click Add to create a new startup entry.
- In the Name field, enter something descriptive like “Process Termination After Logout Using loginctl.”
- In the Command field, enter the following:
sudo loginctl enable-linger $(whoami)- In the Comment field, you can add “Work with
screen.” - Click Save to finalize the entry. This setup ensures that lingering is enabled automatically every time you start your system, so your processes will not be terminated after you log out.
Step 6: Ping at Random Intervals (Simplified Demo)
Section titled “Step 6: Ping at Random Intervals (Simplified Demo)”If you want to ping www.google.com at random intervals between 3 and 10 seconds without creating a script, you can do so directly in the terminal:
- Open a terminal.
- Run the following command:
while true; do echo "$(date '+%Y-%m-%d %H:%M:%S') - $(ping -c 1 www.google.com | grep 'bytes from')"; sleep $((RANDOM % 8 + 3)); doneThis command will continuously ping www.google.com, wait for a random interval between 3 and 10 seconds, and then ping again.
- If you want this to run in the background and continue after logout, start the command inside a
screensession:
screen
while true; do echo "$(date '+%Y-%m-%d %H:%M:%S') - $(ping -c 1 www.google.com | grep 'bytes from')"; sleep $((RANDOM % 8 + 3)); done- Detach the
screensession usingCtrl + A, followed byD.
Conclusion
Section titled “Conclusion”By following these steps, you can ensure that your processes will continue running even after you log out of your Ubuntu session. Using screen, enabling lingering with loginctl, and automating it at startup are powerful tools for managing background processes. Additionally, the simplified random interval ping command provides a quick way to test network connectivity.