Linux 121: Managing Services and Processes on Linux

Linux 121: Managing Services and Processes on Linux

Managing services and processes is essential for maintaining a healthy and responsive Linux system. In this guide, we'll explore how to start, stop, monitor, and manage both foreground processes and background services effectively.


1. Understanding Processes and Services

  • Processes are instances of running programs.

  • Services (daemons) are background processes that often start at boot (e.g., sshd, cron, nginx).

Knowing how to control them allows you to maintain uptime, troubleshoot issues, and optimize resource use.


2. Managing Processes with ps, top, and htop

ps – Snapshot of Running Processes


ps aux

Lists all processes with useful details like PID, CPU, and memory usage.

top – Real-Time Process Monitor


top

Interactive view of system resource usage. Press k to kill a process by PID.

htop – Enhanced Interactive Monitor

Install it if not available:


sudo apt install htop # Debian/Ubuntu sudo yum install htop # RHEL/CentOS

Then run:


htop

Navigate with arrow keys, filter, and kill processes more easily.


3. Starting, Stopping, and Managing Services

Most modern Linux systems use systemd to manage services via systemctl.

Common systemctl Commands


sudo systemctl start nginx # Start a service sudo systemctl stop nginx # Stop it sudo systemctl restart nginx # Restart it sudo systemctl status nginx # Check status

Enable or Disable at Boot


sudo systemctl enable nginx # Start on boot sudo systemctl disable nginx # Don’t start on boot

List All Services


systemctl list-units --type=service

4. Managing Services with init.d and service (Older Systems)

For legacy systems not using systemd:


sudo service apache2 start sudo service apache2 status

Or directly:


sudo /etc/init.d/apache2 restart

5. Managing Background and Foreground Processes

Run a Process in Background


./script.sh &

View Background Jobs


jobs

Bring Background Job to Foreground


fg %1

Kill a Background Job


kill %1

Or by PID:


kill 1234

Use kill -9 1234 to forcefully stop if needed.


6. Using nice and renice to Control CPU Priority

Start with Lower Priority


nice -n 10 command

Change Priority of Running Process


renice -n 5 -p 1234

7. Monitoring Systemd Services with Logs

Use journalctl to view logs:


sudo journalctl -u nginx # Logs for nginx sudo journalctl -xe # View system logs with errors

8. Auto-Restarting Services

Create or edit a systemd unit override:


sudo systemctl edit your-service

Then add:


[Service] Restart=always RestartSec=3

9. Conclusion

Process and service management is a critical Linux administration skill. Whether you’re diagnosing issues, optimizing system load, or automating server behavior, knowing how to interact with systemctl, ps, and top will make you significantly more efficient.


Next in the series:
Linux 122: Using and Managing System Logs on Linux

Post a Comment (0)
Previous Post Next Post

ads