The Ultimate Guide to Linux System Services! From Configuration to Autostart, All in One Place
The Ultimate Guide to Linux System Services! From Configuration to Autostart, All in One Place
Friends, still tired of manually starting services every time your server reboots? Today I bring you the ultimate solution for Linux system services! From creating a service to enabling autostart, this one post covers it all, so your programs run as steadily as proper system services!
🚀 Why Use System Services?
- Autostart on boot: services run automatically after a server reboot, no manual intervention needed
- Process supervision: programs restart automatically after crashes, keeping your service highly available
- Unified management: manage all your service states through the systemctl command
- Log tracking: integrated with the system journal, which makes troubleshooting much easier
🎯 Hands-On: Deploying a Node.js App as a System Service
Step 1: Prepare Your Application
Say we have a Node.js app whose start command is:
node /opt/myapp/app.js
Step 2: Create the systemd Service File
Create a service file under the /etc/systemd/system/ directory:
sudo vim /etc/systemd/system/myapp.service
Step 3: Write the Service Configuration
[Unit]
Description=My Node.js Application
After=network.target
Wants=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/app.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
🔧 Configuration Deep Dive: Every Parameter Explained
The [Unit] Section
| Parameter | Description | Example |
|---|---|---|
| Description | Service description | "My Node.js Application" |
| After | Specifies startup ordering | network.target |
| Wants | Weak dependency | network.target |
| Requires | Hard dependency | mysql.service |
The [Service] Section
| Parameter | Description | Recommended value |
|---|---|---|
| Type | Startup type | simple/forking/oneshot |
| User/Group | User the service runs as | www-data/nobody |
| WorkingDirectory | Working directory | /opt/myapp |
| ExecStart | Start command | /usr/bin/node app.js |
| Restart | Restart policy | always/on-failure/no |
| RestartSec | Restart interval | 10s |
| Environment | Environment variables | NODE_ENV=production |
The [Install] Section
| Parameter | Description |
|---|---|
| WantedBy | Target run level |
| RequiredBy | Forced dependency |
⚡️ Service Management Command Cheat Sheet
Basic Operations
# Reload the systemd configuration
sudo systemctl daemon-reload
# Start the service
sudo systemctl start myapp
# Stop the service
sudo systemctl stop myapp
# Restart the service
sudo systemctl restart myapp
# Check the status
sudo systemctl status myapp
Autostart Settings
# Enable autostart
sudo systemctl enable myapp
# Disable autostart
sudo systemctl disable myapp
# Check whether autostart is enabled
sudo systemctl is-enabled myapp
Viewing Logs
# Follow the logs in real time
sudo journalctl -u myapp -f
# Show the last 100 log lines
sudo journalctl -u myapp -n 100
# Show today's logs
sudo journalctl -u myapp --since today
# Show error logs
sudo journalctl -u myapp --priority=err
🎭 Advanced Configuration Tips
1. Multi-Instance Services
Create a template service file called myapp@.service:
[Unit]
Description=My App Instance %i
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/app.js --port=%i
Restart=always
[Install]
WantedBy=multi-user.target
Start multiple instances:
sudo systemctl start myapp@3000