So many things are running on your server at any given time; these can be processes that you set up yourself or built-in ones that run automatically without your knowledge.
In this article, I will teach you everything you need to know about managing these processes based on your server needs.
Let's start by exploring what a process is in the next section.
For this article, I have set up an Ubuntu desktop virtual machine on my system. However, you should be able to follow along with any Linux system you have, either virtual or physical.
Processes
In Linux, processes are instances of executing programs. Each process has its unique process ID (PID) and can include multiple threads. Every command you run on your server becomes a process until it finishes running.
Imagine you have a kitchen where you're cooking several dishes simultaneously. Each dish you're preparing can be considered a process.
Let's explore what a job is in the next section.
Jobs
Jobs in Linux refer to tasks that are executed by the shell. A job can consist of one or more processes. When a job is started from the shell, it can either run in the foreground, where it interacts directly with the user, or in the background, allowing the user to continue working while the job executes asynchronously.
Think of it like assigning tasks to different people in your team. For instance, if you're managing a project, you might assign one person to design a logo (a job), another to write content (another job), and so on.
I've mentioned foreground and background in the two sections above, so let's understand those next.
Foregrounding
Every time you run a command on your server and watch the result come up, you're doing foregrounding (yes, it's automatic).
When a process runs in the foreground, it occupies the terminal and interacts directly with you. You typically wait for the foreground process to complete before executing further commands. Foreground processes receive input from the terminal and display output directly to it.
To run a process in the foreground, you simply execute the command without any special options or symbols.
Now that you know that you have been foregrounding without knowing it, let's explore its sister, backgrounding, in the next section.
Backgrounding
Backgrounding allows a process to run independently of the terminal, freeing you to continue issuing commands. Background processes do not occupy the terminal, so you can continue using it for other tasks. Background processes typically do not receive input from the terminal or display output directly.
When executing a process in the background, you can append an ampersand &
to the command. You can also send a foreground process that's taking too long to the background by suspending it (pressing Ctrl+Z
) and then using the bg
command to resume its execution in the background.
For example, if you are editing a script and you need to quickly create a file the script will interact with before you save it, you can do that like this:
nano instscript.sh
Once you have it opened, you can now send it to the background by pressing Ctrl+T+Z
, and you should see something like this:
It shows that nano
has stopped
but that means it's running in the background now and you can run any command you need to before you continue editing the script. You can confirm that by running the following command:
ps au | grep nano
And you should see something like this:
The image above shows all the nano
processes in the background on your server (including the ones that aren't yours).
You can bring the process back to the foreground using the fg
command like so:
fg 1
Remember the [1]+
when you sent the nano process to the background? That's the PID you'll use to manage the process, but your PID might be a little different (because you are not using my server π«΅πΎ).
You can also run a command and send it to the background immediately by adding an ampersand (&) to the end like so:
htop &
This will start htop
, assign it a PID, and send it to the background immediately, and the result should look like this:
You can also bring it back to the foreground like so:
fg htop
The command above will bring htop
back to the foreground.
Remember to always save your work before exiting or logging out of your shell to avoid losing them.
Viewing All Processes
You can see all the processes that are being run on your server with the ps
command like so:
ps au
The command above displays information about all processes running on the system. Each column in the output represents different attributes of these processes. Here's an explanation of each column:
-
USER
: This column shows the username of the user who owns the process. -
PID
: Stands for Process ID. This is a unique identifier assigned to each running process by the system. -
%CPU
: Indicates the percentage of CPU time used by the process since the last update. -
%MEM
: Represents the percentage of physical memory (RAM) used by the process. -
VSZ
: Stands for Virtual Memory Size. It represents the total amount of virtual memory (including RAM and swap space) the process uses, measured in kilobytes (KB). -
RSS
: Stands for Resident Set Size. It represents the amount of physical memory (RAM) the process uses, measured in kilobytes (KB). -
TTY
: Indicates the terminal associated with the process. This field will be displayed as a question mark if the process is not associated with a terminal. -
STAT
: Represents the current status of the process. Common status codes include:-
R
: Running -
S
: Sleeping -
D
: Waiting in Disk (uninterruptible sleep) -
Z
: Zombie -
T
: Stopped
-
-
START
: Shows the time when the process started. -
TIME
: Indicates when a process needs the CPU to calculate things. -
COMMAND
: Displays the command name or program associated with the process.
Each row in the output represents a different process running on the system, and each column provides specific information about that process.
You can also see the system-wide process by adding x
to the command above like so:
ps aux
The command above will return the same result in the image but longer. Test it and see π.
Also, you can combine the command above with grep
to look for any troublesome process:
ps aux | grep process_name
The command above will return only the process you specified. Also, you can use the man
command to learn more about the ps
command like so:
man ps
The command above will return a detailed description of things you can do with the ps
command (I can't possibly cover all π€²πΎ)
In the next section, we will understand how to manage the system processes.
Understanding Daemons
In Linux, system processes, often referred to as daemons (pronounced "dee-mons" π«΅πΎ), are background processes that run continuously. They perform various tasks essential for the proper functioning of the operating system or provide specific services to users or other programs.
Let's explore some key points you need to remember about daemons in Linux.
Background Processes
Daemons typically run in the background and do not require direct user interaction. They are detached from the controlling terminal and often start automatically when the system boots up.
System Services
Daemons provide various system services, such as handling network requests, managing hardware devices, monitoring system performance, and executing scheduled tasks.
No User Interface
Unlike regular user applications, daemons usually do not have a graphical user interface (GUI) and interact with the system through configuration files, command-line tools, or specific application programming interfaces (APIs).
Control and Management
System administrators can control and manage daemons using various tools provided by the operating system. These tools include commands like systemctl
and service
and configuration files located in directories like /etc/init.d
or /etc/systemd
.
Examples of Daemons
Some common examples of daemons in Linux include:
-
httpd (Apache)
: A web server daemon that serves web pages over the HTTP protocol. -
sshd (OpenSSH)
: A daemon that provides secure shell (SSH) access to the system for remote administration. -
cron
: A daemon to schedule and execute periodic tasks or commands. -
syslogd/rsyslogd
: Daemons responsible for logging system messages. -
NetworkManager
: A daemon that manages network connections.
As you can now see, daemons play a crucial role in the Linux ecosystem by providing essential services and maintaining the stability and functionality of the operating system.
Now that you understand daemons, let's explore some related commands, how to use them, and what they do in the next section.
Managing System Services with
Linux provides the systemctl
, an extensive command for managing system services. Here are some essential systemctl
commands with Apache web server examples:
Start a Service
sudo systemctl start <service_name>
This command starts the specified service. For example, sudo systemctl start apache2
starts the Apache web server.
Stop a Service
sudo systemctl stop <service_name>
This command stops the specified service. For example, sudo systemctl stop apache2
stops the Apache web server.
Restart a Service
sudo systemctl restart <service_name>
This command stops and then starts the specified service. For example, sudo systemctl restart apache2
restarts the Apache web server.
Reload a Service
sudo systemctl reload <service_name>
This command reloads the configuration of the specified service without stopping it. For example, sudo systemctl reload apache2
reloads the configuration of the Apache web server.
Enable a Service to Start on Boot
sudo systemctl enable <service_name>
This command configures the specified service to start automatically at boot time. For example, sudo systemctl enable apache2
enables the Apache web server to start on boot.
Disable a Service from Starting on Boot
sudo systemctl disable <service_name>
This command disables the specified service's automatic startup at boot time. For example, sudo systemctl disable apache2
disables the Apache web server from starting on boot.
Check the Status of a Service
systemctl status <service_name>
This command displays the current status of the specified service. It provides information about whether the service is running, its PID, and any recent log messages.
List All Services
systemctl list-units --type=service
This command lists all available services on the system and their status (active, inactive, or failed).
These systemctl
commands are essential for managing services on an Ubuntu system, allowing you to start, stop, restart, enable, disable, reload, and check the status of services.
Conclusion
And that's it! You just learned how to manage processes on your Linux server. You also learned and practiced many concepts, including processes, foregrounding, backgrounding, and much more.
You also explored ps
and systemctl
commands, including but not limited to what they are, how to use them, and what their results mean.
Please feel free to leave a comment to correct, suggest, or even teach me something new! π
Finally, remember to follow me here on Dev, LinkedIn, and Twitter. Thank you so much for reading, and I'll see you in the next one!
Top comments (5)
Thanks, Alex.
I'm glad you liked it!
Great series here, Adams! Appreciate ya sharing. π
Thanks, Micheal! π«‘
Great guide, learnt quite a lot from this