Processes and Services
Manage services with systemd
All modern services on Linux are managed by systemd. Basic commands:
# Start a service
systemctl start service-name
# Stop it
systemctl stop service-name
# Restart
systemctl restart service-name
# Reload configuration without downtime
systemctl reload service-name
# Enable automatic startup at boot
systemctl enable service-name
# Disable automatic startup
systemctl disable service-name
# Enable and start together
systemctl enable --now service-name
# Service status
systemctl status service-name
# List all active services
systemctl list-units --type=service --state=running
# List services in error
systemctl --failed
Process management
Find a process
# Search by name
ps aux | grep nginx
pgrep -la nginx # Show PID and name
# Find the PID of a process
pidof nginx
Kill a process
# Terminate with SIGTERM signal (clean, the process can do cleanup)
kill PID
# Force terminate with SIGKILL (immediate)
kill -9 PID
# By process name
pkill nginx # SIGTERM
pkill -9 nginx # SIGKILL
# All processes with a certain name
killall nginx
Process priority (nice)
# Start a process with low priority (nice from -20 to 19, higher = lower priority)
nice -n 10 command
# Change the priority of a running process
renice 10 -p PID
Real-time monitoring
# Complete monitor (recommended)
htop
# Basic monitor
top
# Only processes of a user
ps aux --user www-data
# Top 10 processes by CPU
ps aux --sort=-%cpu | head -11
# Top 10 processes by RAM
ps aux --sort=-%mem | head -11
Zombie processes
A zombie process is a terminated process that has not yet been "removed" from the system. Usually they don't cause problems, but in large numbers they can exhaust available PIDs.
# Find zombie processes
ps aux | grep 'Z'
# How many zombies there are
ps aux | awk '{print $8}' | grep -c Z
To eliminate zombies, usually just restarting the parent process is enough.
Find what uses a port
# Who uses port 80?
ss -tlnp | grep :80
lsof -i :80
# Who uses port 3306 (MySQL)?
ss -tlnp | grep :3306
# All listening ports
ss -tlnp
Processes that won't stop
If a process ignores normal kill:
# 1. Try with SIGTERM
kill PID
# 2. If it doesn't respond after a few seconds, use SIGKILL
kill -9 PID
# 3. Verify it's terminated
ps aux | grep PID
kill -9 doesn't give the process time to do cleanup (save files, close DB connections, etc.). Use it only when the process is completely stuck.
Create a custom systemd service
If you want your script or application to start automatically as a service:
nano /etc/systemd/system/my-app.service
[Unit]
Description=My application
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/my-app
ExecStart=/usr/bin/node /var/www/my-app/app.js
Restart=always
RestartSec=5
Environment=NODE_ENV=production PORT=3000
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now my-app
systemctl status my-app
Services that restart continuously
# See how many times it restarted and why
systemctl status service-name
journalctl -u service-name -n 50
# Reset failure counter
systemctl reset-failed service-name
Articoli correlati
Server Reboot
How to properly restart your VPS or VDS, both via SSH and from the control panel
Resource Monitoring
How to check CPU, RAM, disk and network on your server in real time
Disk Management
How to check disk space, find large files and free up disk space
