WikiJS/home/homelab/idle-process.md

2.3 KiB

title description published date tags editor dateCreated
Create a Background Idle Prevention Processs true 2025-06-25T13:43:53.845Z markdown 2025-06-25T11:49:23.320Z

Setting Up a Lightweight Keep-Alive Script on Ubuntu Server 24.04 LTS

This guide describes how to create a keep_alive.sh script to prevent a headless Ubuntu Server from being considered idle and how to configure it as a systemd service for persistence.

Creating the Keep-Alive Script

  1. Create the Script: Open a terminal and create the script file:

    nano /usr/local/bin/keep_alive.sh
    

    Add the following content:

    #!/bin/bash
    while true; do
        echo "Keep alive" > /dev/null
        sleep 60
    done
    

    Save and exit by pressing Ctrl+O, Enter, then Ctrl+X.

  2. Make the Script Executable: Set execute permissions for the script:

    chmod +x /usr/local/bin/keep_alive.sh
    
  3. Run the Script in the Background: Start the script in the background, ensuring it persists after logout:

    nohup /usr/local/bin/keep_alive.sh &
    

Configuring the Script as a Systemd Service

To ensure the script runs automatically on boot and is managed by the system, configure it as a systemd service.

  1. Create the Systemd Service File: Create a service file:

    sudo nano /etc/systemd/system/keep-alive.service
    

    Add the following content:

     [Unit]
     Description=Keep Alive Script to Prevent Idle
     After=network.target
    
     [Service]
     Type=simple
     ExecStart=/bin/bash /usr/local/bin/keep_alive.sh
     Restart=always
     StandardOutput=null
     StandardError=null
    
     [Install]
     WantedBy=multi-user.target
    

    Save and exit (Ctrl+O, Enter, Ctrl+X).

  2. Enable and Start the Service: Enable the service to start on boot and run it immediately:

    sudo systemctl enable keep-alive.service
    sudo systemctl start keep-alive.service
    
  3. Verify the Service Status: Check that the service is running:

    systemctl status keep-alive.service
    

Notes

  • Resource Usage: The script uses near 0% CPU and minimal memory (a few KB).
  • Interval Adjustment: Modify the sleep 60 value in the script to change the interval (e.g., sleep 30 for 30 seconds).
  • Monitoring: Confirm the script is running with ps aux | grep keep_alive.