paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

Basic CLI: Services and Process Management

Understanding System Services

Introduction

In Linux, services and processes play a critical role in system performance and automation. Services, also known as daemons, run in the background, handling essential tasks like networking, logging, and authentication. Managing these services efficiently ensures a stable and optimized system.

This guide explores fundamental service and process management techniques, including how to list, start, stop, and control processes effectively. Whether you're a system administrator or a power user, mastering these commands will enhance your ability to interact with a Linux-based system.

Types of Services

Internal System Services

User-Installed Services

In Linux, services are called daemons and are typically identified by the letter d at the end of the program name, such as sshd (SSH daemon) and systemd (system initialization daemon). The systemd process is the first to start on a modern Linux system, holding process ID (PID) 1. All system processes can be viewed under /proc/.

Managing Services with systemctl

The systemctl command is a powerful tool for managing system services. It provides control over the systemd system and service manager.

Basic Commands:

# Start a service
systemctl start ssh

# Check service status
systemctl status ssh

# Enable service to start at boot
systemctl enable ssh

These commands help ensure that essential services are running and restart automatically if the system reboots.

Controlling Processes with kill

Processes in Linux can have various states:

Processes can be controlled using commands like kill, pkill, pgrep, and killall.

List available signals:

kill -l

Common signals:

To kill a frozen process:

kill -9 [PID]
``

## Background and Foreground Process Management

### Sending Processes to the Background
Sometimes, you need to run processes in the background while continuing to use the terminal.
```bash
ping -c 10 www.hackthebox.eu
[Ctrl + Z]   # Suspends the process

Bringing Background Processes to the Foreground

root@htb$ jobs
[1]+ running ping -c 10 www.google.com &
root@htb$ fg 1

This resumes the specified process in the foreground, allowing direct interaction.

Executing Multiple Commands Efficiently

Linux provides multiple ways to execute commands sequentially or conditionally:

Using Semicolon (;)

Executes all commands regardless of success or failure:

echo "1"; echo "2"; echo "3"

Using Double Ampersand (&&)

Executes the next command only if the previous one succeeds:

echo "1" && ls EXISTING_FILE && echo "3"

Using Pipes (|)

Sends output from one command as input to another:

ls -l | grep "file.txt"

Each method has its use cases and helps streamline command execution.

Conclusion

Mastering Linux services and process management is crucial for system stability and efficiency. From starting and stopping services with systemctl to managing processes using kill, jobs, and fg/bg, these commands empower users to control system resources effectively.

By utilizing background processing and command chaining, you can optimize workflows and enhance productivity. Experiment with these techniques to manage your Linux environment like a pro!