Run Linux Command in the background
Reprinted from: http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html
There are two methods:
1. Command &: run in the background. If you turn off the slave, it will stop running.
2. nohup command &: run in the background. If you close the terminal, it will continue to run.
I. Introduction
the biggest advantage of Linux/Unix compared with Microsoft platform is that it is truly multi-user and multi-task. Therefore, there are also special management ideas in task management.
we know that on windows, we either use a Program as a service to keep running in the background or stop the service. The program cannot be switched between the foreground and background. Linux provides FG and BG commands, allowing you to easily schedule running tasks. Suppose you find it takes a long time to run a program on the foreground, but you need to do other things, you can use ctrl-Z to suspend the program, and then you can see the system prompt:
[1] + stopped/root/bin/rsync. sh
then we can schedule the program to the background for execution: (the number following BG is the job number)
# BG 1
[1] +/root/bin/rsync. sh &
run the jobs command to view the running tasks:
# jobs
[1] + running/root/bin/rsync. sh &
if you want to call it back to the foreground, you can use
# FG 1
/root/bin/rsync. sh
in this way, you can only wait for the task to be completed on the console.
& Drop the command to the background for execution
[CTRL] + z Abort a foreground task to the background to pause
Jobs View the working status of the background
FG % jobnumber Take the background task to the foreground for processing
BG % jobnumber Put the task in the background for processing
Kill Manage background tasks
Ii ,&
In Linux, when running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command. In fact, the command is put into a Job Queue:
$./Test. Sh &
[1] 17208
$ Jobs-l
[1] + 17208 running./test. Sh &
Be careful when running jobs in the background: do not execute commands that require user interaction in the background, because your machine will be silly there. However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file:
Command> out. File 2> & 1 &
In the preceding example, 2> & 1 Indicates that all standard output and error output will be redirected to an out. File file. After a process is successfully submitted, a process number is displayed, which can be used to monitor or kill the process.
For example, find the file named "httpd. conf" and redirect all the standard output and error output to the find. dt file:
# Find/etc/httpd/-name "httpd. conf"-print> find. dt 2> & 1 &
[2] 7832
After the command is successfully submitted, the system gives its process number 7832. For commands that have been executed on the foreground, you can also run them in the background. First, press Ctrl + Z to pause the running processes, and then run the BG command to run the stopped jobs in the background, for example, tesh is being executed on the front-end. sh use Ctrl + Z to suspend it:
$./Test. Sh
[1] + stopped./test. Sh
$ BG % 1
[1] +./test. Sh &
$ Jobs-l
[1] + 22794 running./test. Sh &
However, for processes executed from the top to the backend, the parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to use nohup to ignore the hangup signal.Or setsid sets the parent process as the INIT process (process number 1)
$ Echo $
21734
$ Nohup./test. Sh &
[1] 29016
$ PS-Ef | grep Test
515 29710 21734 0 00:00:00 pts/12/bin/sh./test. Sh
515 29713 21734 0 00:00:00 pts/12 grep Test
$ Setsid./test. Sh &
[1] 409
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 413 21734 0 00:00:00 pts/12 grep Test
The above test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? You can use the disown command:
$./Test. Sh &
[1] 2539
$ Jobs-l
[1] + 2539 running./test. Sh &
$ Disown-H % 1
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 2542 21734 0 00:00:00 pts/12 grep Test
Another method is to execute a process in a subshell, which is similar to setsid. The method is simple. Enclose the command in parentheses:
$ (./Test. Sh &)
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 12483 21734 0 00:00:00 pts/12 grep Test
Note: The test environment in this article is Red Hat Enterprise Linux as Release 4 (nahant Update 5) and shell is/bin/bash. Different OS and shell may have different commands. For example, there is no disown in KSh of Aix, but nohup-p pid can be used to obtain the same effect of disown.
Another more powerful method is to use screen. First, create a virtual terminal in disconnected mode, and then use the-r option to re-connect to the virtual terminal to execute any commands in it, can achieve the nohup effect, which is more convenient when multiple commands need to be executed continuously in the background:
$ Screen-DMs screen_test
$ Screen-list
There is a screen on:
27963. screen_test (detached)
1 socket in/tmp/uscreens/S-Jiangfeng.
$ Screen-r screen_test
Iii. nohup
If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account. Nohup means no hang up ). The command is generally in the following format:
Nohup conmmand &
If you use the nohup command to submit a job, all the output of the job is redirected to a file named nohup. Out by default, unless the output file is specified separately:
Nohup command> myout. File 2> & 1
In the preceding example, the output is redirected to the myout. File file.
4 ,.*,?, [...], [!...] And so on
The following are special characters:
* Match any string in the file name, including an empty string.
? Match any single character in the file name.
[...] Matches any character in.
[!...] Match! .
When s h e l encounters the above characters, it will treat them as special characters instead of common characters in the file name, so that users can use them to match the corresponding file name.
1) list file names starting with I or O: # ls [iO] *
2) list the names of files starting with log. Followed by a number, which can then be any string: # ls log. [0-9] *
3) In contrast to example 2, list the names of arbitrary strings starting with log. Which are not followed by a number .[! 0-9] *
4) list all files whose names start with LPS and can contain any two characters in the middle, and end with 1: # ls LPs ?? 1
5) list all names starting with an uppercase letter: $ ls [A-Z] * 6) list all names starting. name of the file starting. profile ,. rhosts ,. histo ry, etc.): $ ls. *
Jobs: View the number of commands currently running in the background
FG: Transfers the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use FG % jobnumber to call up the selected command. % jobnumber is the serial number (not PID) of the command being executed in the background found through the jobs command)
BG: Pause a command in the background to continue execution. If there are multiple commands in the background, you can use BG % jobnumber to call up the selected command. % jobnumber is the serial number (not PID) of the command being executed in the background found by the jobs command)
Killing started programs is the same as normal:
- Pkill-9 Name
- Killall Name
- Kill PID
- ...
Category: Linux
Reprinted from: http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html
There are two methods:
1. Command &: run in the background. If you turn off the slave, it will stop running.
2. nohup command &: run in the background. If you close the terminal, it will continue to run.
I. Introduction
the biggest advantage of Linux/Unix compared with Microsoft platform is that it is truly multi-user and multi-task. Therefore, there are also special management ideas in task management.
we know that on windows, we can either keep a program running as a service in the background or stop the service. The program cannot be switched between the foreground and background. Linux provides FG and BG commands, allowing you to easily schedule running tasks. Suppose you find it takes a long time to run a program on the foreground, but you need to do other things, you can use ctrl-Z to suspend the program, and then you can see the system prompt:
[1] + stopped/root/bin/rsync. sh
then we can schedule the program to the background for execution: (the number following BG is the job number)
# BG 1
[1] +/root/bin/rsync. sh &
run the jobs command to view the running tasks:
# jobs
[1] + running/root/bin/rsync. sh &
if you want to call it back to the foreground, you can use
# FG 1
/root/bin/rsync. sh
in this way, you can only wait for the task to be completed on the console.
& Drop the command to the background for execution
[CTRL] + z Abort a foreground task to the background to pause
Jobs View the working status of the background
FG % jobnumber Take the background task to the foreground for processing
BG % jobnumber Put the task in the background for processing
Kill Manage background tasks
Ii ,&
In Linux, when running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command. In fact, the command is put into a Job Queue:
$./Test. Sh &
[1] 17208
$ Jobs-l
[1] + 17208 running./test. Sh &
Be careful when running jobs in the background: do not execute commands that require user interaction in the background, because your machine will be silly there. However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file:
Command> out. File 2> & 1 &
In the preceding example, 2> & 1 Indicates that all standard output and error output will be redirected to an out. File file. After a process is successfully submitted, a process number is displayed, which can be used to monitor or kill the process.
For example, find the file named "httpd. conf" and redirect all the standard output and error output to the find. dt file:
# Find/etc/httpd/-name "httpd. conf"-print> find. dt 2> & 1 &
[2] 7832
After the command is successfully submitted, the system gives its process number 7832. For commands that have been executed on the foreground, you can also run them in the background. First, press Ctrl + Z to pause the running processes, and then run the BG command to run the stopped jobs in the background, for example, tesh is being executed on the front-end. sh use Ctrl + Z to suspend it:
$./Test. Sh
[1] + stopped./test. Sh
$ BG % 1
[1] +./test. Sh &
$ Jobs-l
[1] + 22794 running./test. Sh &
However, for processes executed from the top to the backend, the parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to use nohup to ignore the hangup signal.Or setsid sets the parent process as the INIT process (process number 1)
$ Echo $
21734
$ Nohup./test. Sh &
[1] 29016
$ PS-Ef | grep Test
515 29710 21734 0 00:00:00 pts/12/bin/sh./test. Sh
515 29713 21734 0 00:00:00 pts/12 grep Test
$ Setsid./test. Sh &
[1] 409
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 413 21734 0 00:00:00 pts/12 grep Test
The above test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? You can use the disown command:
$./Test. Sh &
[1] 2539
$ Jobs-l
[1] + 2539 running./test. Sh &
$ Disown-H % 1
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 2542 21734 0 00:00:00 pts/12 grep Test
Another method is to execute a process in a subshell, which is similar to setsid. The method is simple. Enclose the command in parentheses:
$ (./Test. Sh &)
$ PS-Ef | grep Test
515 410 1 0? 00:00:00/bin/sh./test. Sh
515 12483 21734 0 00:00:00 pts/12 grep Test
Note: The test environment in this article is Red Hat Enterprise Linux as Release 4 (nahant Update 5) and shell is/bin/bash. Different OS and shell may have different commands. For example, there is no disown in KSh of Aix, but nohup-p pid can be used to obtain the same effect of disown.
Another more powerful method is to use screen. First, create a virtual terminal in disconnected mode, and then use the-r option to re-connect to the virtual terminal to execute any commands in it, can achieve the nohup effect, which is more convenient when multiple commands need to be executed continuously in the background:
$ Screen-DMs screen_test
$ Screen-list
There is a screen on:
27963. screen_test (detached)
1 socket in/tmp/uscreens/S-Jiangfeng.
$ Screen-r screen_test
Iii. nohup
If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account. Nohup means no hang up ). The command is generally in the following format:
Nohup conmmand &
If you use the nohup command to submit a job, all the output of the job is redirected to a file named nohup. Out by default, unless the output file is specified separately:
Nohup command> myout. File 2> & 1
In the preceding example, the output is redirected to the myout. File file.
4 ,.*,?, [...], [!...] And so on
The following are special characters:
* Match any string in the file name, including an empty string.
? Match any single character in the file name.
[...] Matches any character in.
[!...] Match! .
When s h e l encounters the above characters, it will treat them as special characters instead of common characters in the file name, so that users can use them to match the corresponding file name.
1) list file names starting with I or O: # ls [iO] *
2) list the names of files starting with log. Followed by a number, which can then be any string: # ls log. [0-9] *
3) In contrast to example 2, list the names of arbitrary strings starting with log. Which are not followed by a number .[! 0-9] *
4) list all files whose names start with LPS and can contain any two characters in the middle, and end with 1: # ls LPs ?? 1
5) list all names starting with an uppercase letter: $ ls [A-Z] * 6) list all names starting. name of the file starting. profile ,. rhosts ,. histo ry, etc.): $ ls. *
Jobs: View the number of commands currently running in the background
FG: Transfers the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use FG % jobnumber to call up the selected command. % jobnumber is the serial number (not PID) of the command being executed in the background found through the jobs command)
BG: Pause a command in the background to continue execution. If there are multiple commands in the background, you can use BG % jobnumber to call up the selected command. % jobnumber is the serial number (not PID) of the command being executed in the background found by the jobs command)
Killing started programs is the same as normal:
- pkill-9 name
- killall name
- kill PID
- ...