DEV Community

Cover image for While Linux is running other processes in the background
Dean Jones for Jamaica Homes

Posted on • Updated on

While Linux is running other processes in the background

When working in the Linux family of operating systems, there are times when you need to start a long process, such as searching for a file, and at the same time return to working in the console. It's perfectly reasonable to open multiple terminals and do each task individually, but what if we're not looking for the easy way out? In this case, it would make sense to run the process in the background. Let's see how to do it in two ways.

First you need to understand the syntax of the command line. Running one program/script, etc. execute explicitly, for example:

ping 10.10.0.1

or

./script.sh

or

/home/admin/scripts/script.sh

To start one program at the end of another, use the && (double ampersand) logical operator, for example:

mysqldump mybase -u user1 > db.sql && tar zcvf db.tar db.sql

Not a very good example, since you could use the redirect “|”, approximately it would look like this:

mysqldump mybase -u user1 | tar -cvf db.tar -

All of these examples are great for scripting, but running "long" processes will make the terminal inaccessible, as noted above. In this case, the following example will help:

find / -iname “*.txt” > abc.list &

It is the single & at the end of the line, separated by a space.

As you can see, the first line starts the process, but since there are few txt files in the system, the process ends. A subsequent press of Enter in the terminal reports that the process has completed successfully.

Let's consider another option. Let's use the ping utility, we will write the results to a file.

Due to the lack of restrictive parameters for this command, its execution will be interrupted when the hard disk space runs out.

Terminating the process with killall will show that the process did not end on its own, but was “killed”.

You can control background processes, in this case, with the command:

jobs

The jobs command itself will show how many processes are running and what exactly is running, but the different keys help display more information. The -l switch will also show the system process ID (PID). The -p switch will display only PIDs, without a description.

You can use the fg 1 or %1 command to enter foreground mode.

Alternative way.

Use the screen multiplexer utility. Some distributions require installation.

CentOS:

yum install screen

Ubuntu:

apt-get install screen

Running the command is simple:

screen

After execution, a window with introductory text will appear.

Press Enter.

The contents of the terminal will be updated. We can execute a command, for example, the same ping. To return to the main terminal, press ctrl + a, and then the d key.

A message will appear in the terminal that the process has been detached.

You can view the running screens with the command:

screen -list

Return to the process can be done with the command:

screen -r

or

screen -r

In our case:

screen -r 2636.pts-0.ubuntu

As you can see, the process was running in the background.

Sometimes you need a script that would run a process in the background when the system boots. In this case, you should use the following example:

screen -d -m -S SCREEN_NAME program_1

-d -m Runs the screen process in detached mode. A new session will be created, but it will not be shown.

-S - will set the name for the process. Useful when multiple screen processes are running.

You can open the process window with the command:

screen -r my_ping

For more information visit hostrooster.com

Top comments (0)