Processes and Services

01

Manage services with systemd

All modern services on Linux are managed by systemd. Basic commands:

bash
# 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
02

Process management

Find a process

bash
# 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

bash
# 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)

bash
# 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
03

Real-time monitoring

bash
# 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
04

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.

bash
# 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.

05

Find what uses a port

bash
# 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
06

Processes that won't stop

If a process ignores normal kill:

bash
# 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.

07

Create a custom systemd service

If you want your script or application to start automatically as a service:

bash
nano /etc/systemd/system/my-app.service
ini
[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
bash
systemctl daemon-reload
systemctl enable --now my-app
systemctl status my-app
08

Services that restart continuously

bash
# 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

DeluxHost, founded in 2023, offers high-quality hosting solutions for various digital needs. We provide shared hosting, VPS, and dedicated servers with advanced security and global data centers.

© DeluxHost, All rights reserved. | VAT Number : IT17734661006
All Systems Operational