Compare commits

..

3 Commits

View File

@ -0,0 +1,88 @@
---
title: Create a Background Idle Prevention Processs
description:
published: true
date: 2025-06-25T13:53:00.203Z
tags:
editor: markdown
dateCreated: 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:
```bash
nano /usr/local/bin/keep_alive.sh
```
Add the following content:
```bash
#!/bin/bash
while true; do
script -q -c "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:
```bash
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:
```bash
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:
```bash
sudo nano /etc/systemd/system/keep-alive.service
```
Add the following content:
```ini
[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:
```bash
sudo systemctl enable keep-alive.service
sudo systemctl start keep-alive.service
```
3. **Verify the Service Status**:
Check that the service is running:
```bash
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`.