Handling Services and Processes in Linux Ubuntu - Notes By ShariqSP

Managing Services and Processes in Linux Ubuntu

What are Linux Services?

In Linux, services (or daemons) are background processes that start during system boot and run until shutdown. They provide essential functions such as networking, printing, and web serving. Proper management of these services ensures system performance, security, and stability.

Systemd Overview

Ubuntu uses systemd for service management. It replaces older init systems and provides a standardized method to control services. The systemctl command is used for managing these services.

Checking Service Status

To check the status of a service:

sudo systemctl status servicename

Example:

sudo systemctl status apache2

Starting and Stopping Services

To start or stop a service:

sudo systemctl start servicename
sudo systemctl stop servicename

Example:

sudo systemctl start apache2
sudo systemctl stop apache2

Restarting and Reloading Services

To restart or reload a service:

sudo systemctl restart servicename
sudo systemctl reload servicename

Example:

sudo systemctl restart apache2
sudo systemctl reload apache2

Enabling and Disabling Services

To configure a service to start automatically at boot or prevent it from starting:

sudo systemctl enable servicename
sudo systemctl disable servicename

Example:

sudo systemctl enable apache2
sudo systemctl disable apache2

Viewing Service Logs

To view logs for a service:

sudo journalctl -u servicename

Example:

sudo journalctl -u apache2

List All Services

To list all services and their statuses:

systemctl list-units --type=service

Understanding Processes

Processes are instances of running programs. Each process is identified by a unique Process ID (PID) and has its own resources and memory space.

Listing Processes

To list all running processes:

ps aux

To list processes with additional details and in a hierarchical view:

ps aux --forest

Finding a Process by Name

To find a process by name:

pgrep processname
ps -ef | grep processname

Example:

pgrep apache2
ps -ef | grep apache2

Getting Detailed Information about a Process

To get detailed information about a specific process by PID:

ps -p PID -o pid,ppid,cmd,%mem,%cpu

Example:

ps -p 1234 -o pid,ppid,cmd,%mem,%cpu

Terminating Processes

To terminate a process:

kill PID

To forcefully terminate a process:

kill -9 PID

Example:

kill 1234
kill -9 1234

Conclusion

Managing both services and processes is crucial for maintaining the performance and stability of a Linux Ubuntu system. By understanding how to control services and manage processes effectively, you can ensure your system operates smoothly and efficiently.