2.4 KiB
| title | description | published | date | tags | editor | dateCreated |
|---|---|---|---|---|---|---|
| Create a Background Idle Prevention Processs | true | 2025-06-25T13:53:00.203Z | 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
-
Create the Script: Open a terminal and create the script file:
nano /usr/local/bin/keep_alive.shAdd the following content:
#!/bin/bash while true; do script -q -c "echo 'Keep alive'" /dev/null sleep 60 doneSave and exit by pressing
Ctrl+O,Enter, thenCtrl+X. -
Make the Script Executable: Set execute permissions for the script:
chmod +x /usr/local/bin/keep_alive.sh -
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.
-
Create the Systemd Service File: Create a service file:
sudo nano /etc/systemd/system/keep-alive.serviceAdd 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.targetSave and exit (
Ctrl+O,Enter,Ctrl+X). -
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 -
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 60value in the script to change the interval (e.g.,sleep 30for 30 seconds). - Monitoring: Confirm the script is running with
ps aux | grep keep_alive.