Moment For Technology

How to efficiently run Linux commands in the background?

Posted on Dec. 1, 2022, 5:02 p.m. by Michael Hart
Category: The back-end Tag: linux operations The back-end

This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

A lifelong learner, practitioner, and sharer committed to the path of technology, an original blogger who is busy and sometimes lazy, and a teenager who is occasionally boring and sometimes humorous.

Welcome to search "Jge's IT Journey" on wechat!

How to run Linux commands in the background?

preface

Normally, when running a command on a Linux terminal, we have to wait for the previous command to be executed before entering and executing the next command, called running in the foreground or foreground process. While running in the foreground, the process will occupy your shell and you can also use the input device to interact with its terminal.

What happens when a command takes a long time to complete and you want to run other commands at the same time?

  • First: start a new Shell session and run commands in it.
  • Second: running commands in the background.

Background processes are started from the terminal and run in the background. Users do not need to interact with the terminal.

Key combination and command

For example, the httpD-2.2.17.tar. gz package will be decompressed for a long time under the terminal screen, or even a long screen brush to decompress the package, so we can press Ctrl+Z to pause and hide in the background, which will return a stop number. The process is suspended and will not continue to run.

Enter the bg command to continue executing the task in the background.

Enter the fg + serial number command to return the process to the foreground for execution.

During the execution, you can run the jobs command to view all background tasks.

# tar -zxf httpd-2.2.17.tar.gz ## Press Ctrl+Z to stop [1]+ Stopped tar -zxf httpd-2.2.17.tar.gz # bg ## Enter the bg command to continue running in the background [1]+ tar -zxf httpd-2.2.17.tar.gz  # jobs ## to view all background tasks [1]+ Done tar -zxf httpd-2.2.17.tar.gzCopy the code

Second,

Add after the command to make it run in the background of the terminal.

# ./yum_install.sh 
Copy the code

After the command is executed, the yum script is run in the background, but the output is displayed on the terminal, so we can redirect the output of the command to the file.

# ./yum_install.sh  202001141411out.txt 21 
Copy the code

# yum # yum # yum # yum # yum # yum # yum # yum # yum

If the script file has been executed for a period of time and is stuck, you can use the first method: jobs command

# jobs -l
Copy the code

Or use the ps command

# ps -aux | grep yum_install.sh
Copy the code

Third, nohup

: You can make commands run in the background and the process will be killed when the console is shut down. If you want the process to run after the console is shut down, you need to use nohup.

No hup, no hang up.

Use nohup at the beginning of the command execution. When the console is closed, the process continues to run in the background.

# nohup ./yum_install.sh 
Copy the code

As above nohup implementation methods, when the judge whether the script is done, can execute the command ps - aux | grep yum_install. Sh view;

In addition, we can output the results of the run to the nohup.log file. If no output redirection is specified, the log file is output to the nohup.out file by default.

# nohup ./yum_install.sh  nohup.log 21 
Copy the code

Recommended reading


Original is not easy, if you think this article is a little useful to you, please give me a like, comment or forward for this article, because this will be my power to output more quality articles, thanks!

By the way, dig friends remember to give me a free attention yo! In case you get lost and you can't find me next time.

See you next time!

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.