Linux Series: Full Guide Managing Processes in Ubuntu

Linux Series: Full Guide Managing Processes in Ubuntu

ยท

9 min read

There are so many things that are running on your server at any point in time; these can be processes that you set up yourself or the in-built 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 own 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 2 sections above, so let's understand those next.

Foregrounding

Every time you run a command on your server and watch the result come up, what you're doing is called foregrounding (yes, it's automatic).

When a process runs in the foreground, it occupies the terminal and interacts directly with the you. and 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 and do not display output directly to it.

To run a process in the background, you can append an ampersand & to the command when executing it. 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:

nanoscript

It shows that nano has stopped but that just 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:

grepnano

The image above shows all the nano processes that are 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 that 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:

htopbg

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 process that is being run on your server with the ps command like so:

ps au

Image description

The command above display 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:

  1. USER: This column shows the username of the user who owns the process.

  2. PID: Stands for Process ID. This is a unique identifier assigned to each running process by the system.

  3. %CPU: Indicates the percentage of CPU time used by the process since the last update.

  4. %MEM: Represents the percentage of physical memory (RAM) used by the process.

  5. 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).

  6. RSS: Stands for Resident Set Size. It represents the amount of physical memory (RAM) the process uses, measured in kilobytes (KB).

  7. 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.

  8. 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

  9. START: Shows the time when the process started.

  10. TIME: Indicates when a process needs the CPU to calculate things.

  11. 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 detailed description of things you can do with the ps command (i can't possibly cover all ๐Ÿคฒ๐Ÿพ)

Let's understand how to manage the system processes in the next section.

Understanding Daemons

In Linux, system processes, often referred to as daemons (pronounced "dee-mons" ๐Ÿซต๐Ÿพ), are background processes that run continuously, performing various tasks essential for the proper functioning of the operating system or providing 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 are responsible for providing 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, 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 responsible for serving 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 used 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 what daemons are, let's explore some of the commands relating to them, how you can use them and what they do in the next section.

Managing System Services with

Linux provides the systemctl, which is 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 automatic startup of the specified service at boot time. For example, sudo systemctl disable apache2 disables the Apache web server from starting on boot.

Check 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, along with their status (whether 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, 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 Hashnode, LinkedIn, and Twitter. Thank you so much for reading, and I'll see you in the next one!

ย