Menu
User

DropVPS Team

Writer: Cooper Reagan

How to Run a Script as a Service on Ubuntu

How to Run a Script as a Service on Ubuntu

Publication Date

05/25/2026

Category

Articles

Reading Time

3 Min

Table of Contents

Running a script as a service on Ubuntu allows the script to start automatically, restart after failures, and be managed with standard systemctl commands. This is useful for background scripts, bots, monitoring tasks, automation jobs, and custom applications.

On a Linux VPS server, systemd is commonly used to keep custom scripts running reliably after reboot or unexpected process failure.

Step 1: Connect to the Ubuntu Server

Access the server using SSH:

ssh root@your_server_ip

Step 2: Create a Script File

Create a new script inside /usr/local/bin:

nano /usr/local/bin/my-script.sh

Add a simple test script:

#!/bin/bash

while true
do
    echo "Script is running at $(date)" >> /var/log/my-script.log
    sleep 60
done

This script writes a log entry every 60 seconds.

Step 3: Make the Script Executable

Set executable permission on the script:

chmod +x /usr/local/bin/my-script.sh

Test the script manually:

/usr/local/bin/my-script.sh

Stop it with:

CTRL + C

Step 4: Create a systemd Service File

Create a new service unit file:

nano /etc/systemd/system/my-script.service

Add the following configuration:

[Unit]
Description=My Custom Script Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/my-script.sh
Restart=on-failure
RestartSec=5
User=root

[Install]
WantedBy=multi-user.target

The ExecStart line defines the script that systemd runs as the main service process. The Restart=on-failure option tells systemd to restart the service if it exits with an error.

Step 5: Reload systemd

Reload systemd so it detects the new service file:

systemctl daemon-reload

This step is required after creating or editing a systemd unit file.

Step 6: Start the Script Service

Start the service:

systemctl start my-script

Check the service status:

systemctl status my-script

If the service is running correctly, the status should show:

active (running)

Step 7: Enable the Service at Boot

Enable the script service to start automatically after reboot:

systemctl enable my-script

You can also start and enable it in one command:

systemctl enable --now my-script

Step 8: View Script Logs

Check logs written by the script:

cat /var/log/my-script.log

View systemd service logs:

journalctl -u my-script

Follow logs in real time:

journalctl -u my-script -f

Step 9: Restart or Stop the Service

Restart the service:

systemctl restart my-script

Stop the service:

systemctl stop my-script

Disable automatic startup:

systemctl disable my-script

Step 10: Run the Service as a Non-Root User

For better security, create a dedicated user:

useradd --system --no-create-home --shell /usr/sbin/nologin myscript

Update file ownership if the script needs access to its own files:

chown myscript:myscript /usr/local/bin/my-script.sh

Then edit the service file:

nano /etc/systemd/system/my-script.service

Change the user line:

User=myscript
Group=myscript

Reload systemd and restart the service:

systemctl daemon-reload
systemctl restart my-script

Step 11: Remove the Script Service

Stop and disable the service:

systemctl stop my-script
systemctl disable my-script

Remove the service file:

rm /etc/systemd/system/my-script.service

Reload systemd:

systemctl daemon-reload

Running a script as a service on Ubuntu Server 24.04 makes background tasks easier to manage, monitor, restart, and run automatically after reboot. For VPS environments, systemd provides a reliable way to keep custom automation scripts and lightweight applications running continuously.

Linux VPS
๐ŸงLinux VPS

Need a Linux Server for This?

Run Debian, Ubuntu, or any Linux distro on DropVPS โ€” fast NVMe SSD, full root access, and 24/7 support. Perfect for everything you just read.

  • Full Root Access
  • Debian & Ubuntu Ready
  • 99.99% Uptime
  • 24/7 Support
Get Linux VPS โ†’

No commitment ยท Cancel anytime

U
Loading...

Related Posts