The Ultimate Guide to Linux System Services! From Configuration to Autostart, All in One Place

Linux

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?

  1. Autostart on boot: services run automatically after a server reboot, no manual intervention needed
  2. Process supervision: programs restart automatically after crashes, keeping your service highly available
  3. Unified management: manage all your service states through the systemctl command
  4. 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

ParameterDescriptionExample
DescriptionService description"My Node.js Application"
AfterSpecifies startup orderingnetwork.target
WantsWeak dependencynetwork.target
RequiresHard dependencymysql.service

The [Service] Section

ParameterDescriptionRecommended value
TypeStartup typesimple/forking/oneshot
User/GroupUser the service runs aswww-data/nobody
WorkingDirectoryWorking directory/opt/myapp
ExecStartStart command/usr/bin/node app.js
RestartRestart policyalways/on-failure/no
RestartSecRestart interval10s
EnvironmentEnvironment variablesNODE_ENV=production

The [Install] Section

ParameterDescription
WantedByTarget run level
RequiredByForced 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