Skip to content

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.”

(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.

  • Ubuntu or any Linux distribution.
  • Basic command-line knowledge.

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.

  1. Open a terminal.
  2. Install screen by running the following command:
Terminal window
sudo apt-get update
sudo apt-get install screen
  1. To start a screen session, simply type:
Terminal window
screen
  1. Run your desired command inside the screen session. For example, you can start a process or command that you want to keep running.
  2. Detach the screen session by pressing Ctrl + A, followed by D.

If you want to return to your detached session, you can reattach it using:

Terminal window
screen -r

If there are multiple sessions, you may need to specify the session ID. You can list all screen sessions by running:

Terminal window
screen -ls

This command will show a list of available screen sessions. To reattach to a specific session, use:

Terminal window
screen -r session_id

Replace session_id with the appropriate ID shown in the screen -ls output.

Example Output of screen -ls:

Terminal window
screen -list
There 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.

  1. Run the following command:
Terminal window
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.

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.

  1. Open Startup Applications Preferences from your system settings.
  2. Click Add to create a new startup entry.
  3. In the Name field, enter something descriptive like “Process Termination After Logout Using loginctl.”
  4. In the Command field, enter the following:
Terminal window
sudo loginctl enable-linger $(whoami)
  1. In the Comment field, you can add “Work with screen.”
  2. 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:

  1. Open a terminal.
  2. Run the following command:
Terminal window
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

This command will continuously ping www.google.com, wait for a random interval between 3 and 10 seconds, and then ping again.

  1. If you want this to run in the background and continue after logout, start the command inside a screen session:
Terminal window
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
  1. Detach the screen session using Ctrl + A, followed by D.

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.