π How to Create a Service in Ubuntu Using systemd
π― Why Do We Create a Service in Ubuntu?β
A service is a background process (daemon) that runs continuously, even after logging out or rebooting the system.
β Key Benefits of Creating a Service:β
- π Automation & Startup Execution β Ensures apps start automatically on boot.
- π Process Management & Stability β Monitors and restarts failed applications.
- π Security & User Permissions β Runs services with restricted users.
- βοΈ Standardized Control β Uses
systemctl
for easy management.
π Step-by-Step Guide to Creating a Service in Ubuntuβ
π Step 1: Prepare Your Script or Applicationβ
Before creating a service, ensure you have a script or application ready.
For example, a simple bash script /opt/myapp/start.sh
:
#!/bin/bash
echo "My service is running..."
while true; do
sleep 60
done
Give it execution permissions:
chmod +x /opt/myapp/start.sh
π Step 2: Create a systemd Service Fileβ
Create a new service file in /etc/systemd/system/
:
sudo nano /etc/systemd/system/myapp.service
πΉ Add the following content:
[Unit]
Description=My Custom Application Service
After=network.target
[Service]
ExecStart=/opt/myapp/start.sh
WorkingDirectory=/opt/myapp
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
π Explanation:
[Unit]
β Defines metadata & dependencies.[Service]
β Configures execution & restart behavior.User=root
β Runs the service as the root user. This means the service has full system privileges, which is not recommended for security-sensitive applications. Consider running under a non-root user if possible.Group=root
β Defines the group under which the service runs. Keeping itroot
means the service has administrative group access.
[Install]
β Enables auto-start at boot.
π Step 3: Reload systemd & Start the Serviceβ
After creating the service file, reload systemd:
sudo systemctl daemon-reload
Start your service:
sudo systemctl start myapp
Check if it's running:
sudo systemctl status myapp
π Step 4: Enable the Service to Start on Bootβ
To ensure the service starts automatically:
sudo systemctl enable myapp
Disable auto-start (if needed):
sudo systemctl disable myapp
π Step 5: Managing the Serviceβ
Action | Command |
---|---|
β Start | sudo systemctl start myapp |
β Stop | sudo systemctl stop myapp |
π Restart | sudo systemctl restart myapp |
π Check Status | sudo systemctl status myapp |
π₯ Check if Active | sudo systemctl is-active myapp |
π Check if Enabled at Boot | sudo systemctl is-enabled myapp |
π Step 6: View Logs of the Serviceβ
View historical logs:
sudo journalctl -u myapp --no-pager
π‘ Real-time logs:
sudo journalctl -u myapp -f
π Step 7: Remove the Serviceβ
To remove the service: 1οΈβ£ Stop & disable it:
sudo systemctl stop myapp
sudo systemctl disable myapp
2οΈβ£ Delete the service file:
sudo rm /etc/systemd/system/myapp.service
3οΈβ£ Reload systemd:
sudo systemctl daemon-reload
4οΈβ£ (Optional) Remove script:
sudo rm -r /opt/myapp
π Alternative Methods to Run Applications Without Creating a Service
1οΈβ£ Running in the Foreground (Basic Execution)β
node /home/ubuntu/app.js
β Stops when the terminal is closed.
2οΈβ£ Using nohup
(Ignore Hangups)β
nohup node /home/ubuntu/app.js > output.log 2>&1 &
β Keeps running after logout. β No auto-restart if crashed.
3οΈβ£ Using screen
(Persistent Sessions)β
screen -S myapp
node /home/ubuntu/app.js
πΉ Press Ctrl + A
, then D
to detach.
πΉ Reattach with:
screen -r myapp
β Stops on reboot.
4οΈβ£ Using tmux
(Better Alternative to Screen)β
tmux new -s myapp
node /home/ubuntu/app.js
πΉ Detach with Ctrl + B
, then D
.
πΉ Reattach with:
tmux attach -t myapp
β Doesnβt persist after reboot.
5οΈβ£ Using supervisor
(Process Monitoring & Auto-Restart)β
πΉ Install supervisor
:
sudo apt update && sudo apt install supervisor -y
πΉ Create a configuration file:
sudo nano /etc/supervisor/conf.d/myapp.conf
πΉ Add the following content:
[program:myapp]
command=/usr/bin/node /home/ubuntu/app.js
autostart=true
autorestart=true
stderr_logfile=/var/log/myapp.err.log
stdout_logfile=/var/log/myapp.out.log
πΉ Apply changes and start the process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start myapp
β Auto-restarts if the process crashes!
π Comparison: systemd vs. Other Methods
Method | Pros | Cons |
---|---|---|
π systemd service | Best for production, auto-start, logging | Requires root access |
π nohup & & | Simple, keeps running | No auto-restart, hard to track |
π screen/tmux | Keeps running in a session | Stops after reboot |
π Supervisor | Auto-restart, logging | Extra installation required |
π― Final Thoughtsβ
β
If you need a long-running, auto-managed process, use systemd services.
β
If you need something temporary, nohup
or screen/tmux
are fine.
β
If you need process monitoring, supervisor
is a great option.
For DevOps & production environments, systemd services are the best choice! ππ₯
π‘ **Let me know if you need further clarifications!**π