Processes and systemd
Introduction
Every running program is a process with a PID. On modern Linux, systemd starts services at boot, restarts failed daemons, and collects logs. This chapter shows how to inspect processes, run background jobs, manage units like Nginx or MySQL, and schedule tasks with cron.
Prerequisites
- Package management
sudoaccess
View Processes
# Snapshot of all processes
ps aux | head
# Filter by name
ps aux | grep nginx
# PID of a program
pgrep nginx
pidof nginxInteractive monitor:
# Install if needed
sudo apt install -y htop
htopPress q to quit top/htop.
Key columns in ps aux:
| Column | Meaning |
|---|---|
| USER | Owner |
| PID | Process ID |
| %CPU / %MEM | Resource use |
| COMMAND | Program |
Foreground, Background, nohup
# Run in background
sleep 300 &
# List jobs in this shell
jobs
# Bring job 1 to foreground
fg %1Survive SSH disconnect with nohup:
# Output to nohup.out in current directory
nohup java -jar app.jar > app.log 2>&1 &
# Note PID
echo $!Production prefers systemd over raw nohup—reliable restart and logging.
Signals and kill
# Graceful stop (SIGTERM)
kill 12345
# Force kill (SIGKILL) — last resort
kill -9 12345
# Kill by name
pkill nginxWarning
kill -9 does not allow cleanup—databases and JVMs may corrupt data. Try graceful stop first.
systemd Basics
systemd manages units. Common type: service (.service).
# List running services
systemctl list-units --type=service --state=running | head
# Status of ssh
systemctl status ssh
# Status of nginx after install
systemctl status nginxControl services:
# Start / stop / restart
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
# Enable at boot / disable
sudo systemctl enable nginx
sudo systemctl disable nginxReload config without full restart (when supported):
sudo systemctl reload nginxService Logs with journalctl
# Follow ssh service log
sudo journalctl -u ssh -f
# Last 100 nginx log lines
sudo journalctl -u nginx -n 100 --no-pager
# Since today
sudo journalctl -u nginx --since todayApps logging to files still use tail -f /path/to/app.log—many Java/Node apps write their own files under /var/log or /opt/myapp/logs.
Example: Run Spring Boot as a systemd Service
Create user and directory (if not exists):
sudo useradd -r -s /bin/false myapp || true
sudo mkdir -p /opt/myapp
sudo chown myapp:myapp /opt/myappPlace app.jar in /opt/myapp/. Unit file /etc/systemd/system/myapp.service:
Enable and start:
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp
sudo journalctl -u myapp -fAdjust JAVA_HOME or JVM flags in ExecStart as needed.
Node with PM2 (Alternative)
Node teams often use PM2 instead of hand-written units—see deployment notes in project README. systemd remains the OS-level standard for Nginx, MySQL, and system daemons.
cron Scheduled Tasks
Edit crontab for current user:
crontab -eExample entries:
# m h dom mon dow command
# Daily backup at 2:30 AM
30 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
# Every 5 minutes health check
*/5 * * * * curl -sf http://127.0.0.1:8080/health || echo "down" >> /tmp/health.logList and logs:
crontab -l
grep CRON /var/log/syslog | tailUse systemd timers for complex scheduling on modern distros—cron remains ubiquitous and easy.
Mini Example: Install and Control Nginx
sudo apt update
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
curl -I http://127.0.0.1/
sudo systemctl status nginxFAQ
zombie processes?
Parent should reap children; zombies often mean buggy parent—investigate with ps aux | grep Z.
Port already in use?
sudo ss -tlnp | grep :8080 finds PID; stop conflicting service.
Changed unit file but behavior old?
sudo systemctl daemon-reload after editing .service files.
Java process disappears after SSH exit?
You started without nohup/systemd—use a service unit for production.