What I Wanted to Do
I wanted to restart nginx or Docker and configure them to start automatically on boot. On Linux, systemd handles starting, stopping, and enabling services.
Basic Commands
# Start a service
sudo systemctl start nginx
# Stop a service
sudo systemctl stop nginx
# Restart a service
sudo systemctl restart nginx
# Reload config without stopping
sudo systemctl reload nginx
Checking Service Status
sudo systemctl status nginx
Output looks like this:
● nginx.service - A high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: active (running) since ...
active (running)→ service is runninginactive (dead)→ service is stoppedfailed→ service failed to start
Enabling Auto-Start on Boot
Use enable to start a service automatically when the OS boots.
# Enable auto-start
sudo systemctl enable nginx
# Disable auto-start
sudo systemctl disable nginx
# Check current auto-start status
sudo systemctl is-enabled nginx
If it shows enabled, auto-start is configured.
Checking Logs with journalctl
# View service logs
sudo journalctl -u nginx
# Show only the latest 50 lines
sudo journalctl -u nginx -n 50
# Follow logs in real time
sudo journalctl -u nginx -f
# Show only today's logs
sudo journalctl -u nginx --since today
Creating a Custom Service
You can register your own script as a systemd service.
sudo vim /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
ExecStart=/home/user/myapp/start.sh
Restart=always
User=user
WorkingDirectory=/home/user/myapp
[Install]
WantedBy=multi-user.target
After creating the file, reload the daemon and start the service.
# Reload systemd to pick up the new config
sudo systemctl daemon-reload
# Enable and start the service in one step
sudo systemctl enable --now myapp
Useful Commands Summary
# List all services and their status
sudo systemctl list-units --type=service
# Show only failed services
sudo systemctl --failed
# View the service definition file
sudo systemctl cat nginx
Common Pitfalls
enablealone does not start the service — also runstart, or useenable --now- Always run
daemon-reloadafter adding or modifying a custom service file - Use full paths in
ExecStart(e.g./usr/bin/python3) Restart=alwaysmakes the service restart automatically after a crash- If logs fill up disk space, clean them with
journalctl --vacuum-time=7d
Related Articles
- Linux Basic Commands (ls/cd/mkdir/rm)
- Linux Process Management (ps/kill/top)
- How to Set Up Cron Jobs on Linux
- nginx Basic Config File Setup
Recommended Cloud Hosting
Looking for reliable cloud infrastructure? Check out these developer-friendly services.
- Cherry Servers - High-performance VPS and dedicated servers
- Cloudways - Managed cloud hosting for developers