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
Linux/Unix is different from Microsoft's platform. The biggest advantage is the real multi-user, multi-task. Therefore, there are also special management ideas in task management.
We know that in windows, we either makeProgramThe service is always running in the background, or the service is stopped. The program cannot be switched between the foreground and background. Linux
The FG and BG commands are provided, allowing you to easily schedule running tasks. Suppose you find it takes a long time to run a program on the front end, but you need to do other things, you can use
CTRL-Z: Hold 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 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] + zAbort a foreground task to the background to pause
JobsView the working status of the background
FG % jobnumberTake the background task to the foreground for processing
BG % jobnumberPut the task in the background for processing
KillManage 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 run commands that require user interaction in the background, because your machine will be silly. 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. After a process is successfully submitted, a process number is displayed, which can be used to monitor or kill the process.
for example, search for the name "httpd. CONF file, and redirect all standard output and error output to find. in the 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). The shell is/bin/bash. Different OS and shell commands may be different. For example, the KSh of Aix does not have disown, but nohup can be used.
-P pid to achieve 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
- ...