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:
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:
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.
Use the screen multiplexer utility. Some distributions require installation.
yum install screen
apt-get install screen
Running the command is simple:
After execution, a window with introductory text will appear.
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:
Return to the process can be done with the command:
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
Welcome to the world of DomainRooster, where roosters (and hens) rule the roost! We're a one-stop shop for all your entrepreneurial needs, bringing together domain names and website hosting, and all the tools you need to bring your ideas to life. With our help, you'll soar to new heights and hatch great success. Think of us as your trusty sidekick, always there to lend a wing and help you navigate the sometimes-complex world of domain names and web hosting. Our team of roosters are experts in their fields and are always on hand to answer any questions and provide guidance. So why wait? Sign up today and join the ranks of the world's greatest entrepreneurs. With DomainRooster, the sky's the limit! And remember, as the saying goes, "Successful people do what unsuccessful people are not willing to do." So don't be afraid to take that leap of faith - DomainRooster is here to help you reach for the stars. Caw on!