Linux Command background run

Source: Internet
Author: User
Tags uppercase letter rsync

There are two ways of doing this:

1. Command &: Background run, you shut down the terminal will stop running

2. Nohup Command &: Run in the background, you will continue to run the terminal

First, Introduction

Linux/unix The biggest advantage that distinguishes Microsoft platform is the real multi-user, multi-tasking. Therefore, in the task management also has the characteristic management thought.

we know that in Windows above, we either have a program running in the background as a service, or stop the service. Instead of allowing the program to switch between foreground and background. Linux provides FG and bg commands that allow you to easily schedule tasks that are running. Suppose you find that a program running in the foreground takes a long time, but you need to do something else, you can Hang up the program with Ctrl-z, and then you can see the system prompt:

[1]+ stopped/root/bin/rsync.sh

then we can schedule the program to execute in the background: ( BG the number that follows is the job number)

#bg 1

[1]+/root/bin/rsync.sh &

with Jobs command to view a running task:

#jobs

[1]+ running/root/bin/rsync.sh &

If you want to bring it back to the foreground, you can use

#fg 1

/root/bin/rsync.sh

This way, you can only wait for the task to complete on the console.

& throw instructions into the background to execute

[Ctrl]+z pause the foreground task in the background

Jobs View the working status of the background

FG%jobnumber take the background tasks to the front desk to handle

BG%jobnumber put the task in the background to handle

Kill manage tasks in the background

Second, &

in the Linux , when a job is run in the foreground, the terminal is occupied by the job, and when the job is run in the background, it does not occupy the terminal. You can use the & command to place jobs in the background. In fact, this is where the command is put into a job queue:

$ ./test.sh &

[1] 17208

$ jobs-l

[1]+ 17208 Running./test.sh &

Be careful when running a job in the background: commands that require user interaction are not executed in the background, because your machine will be there to wait. However, a job running in the background will output the results to the screen, interfering with your work. If a job that runs in the background produces a lot of output, it's a good idea to redirect its output to a file using the following method:

Command >out.file 2>&1 &

in the example above, 2>&1 indicates that all standard output and error outputs will be redirected to a Out.file In the file. When you successfully submit a process, a process number is displayed, which can be used to monitor the process, or to kill it.

Example: Look for the name " httpd.conf "file and redirect all standard output and error output to Find.dt in the 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 already been executed in the foreground, they can also be re-executed in the background, by pressing CTRL + Z to pause the running process, and then using BG command to put a stopped job in the background, such as tesh.sh that is being executed in the foreground, using Ctrl + Z to suspend it:

$./test.sh

[1]+ Stopped./test.sh

$ bg%1

[1]+./test.sh &

$ jobs-l

[1]+ 22794 Running./test.sh &

However, if the process is executed above the background, its parent process is still the current terminal Shell process, and once the parent process exits, it sends Hangup signal to all child processes, child processes received Hangup will also exit later. If we are going to continue running the process while exiting the Shell , we need to use nohup to ignore the hangup signal . , or Setsid set the parent process to Init Process ( process number is 1)

$ echo $$

21734

$ nohup./test.sh &

[1] 29016

$ PS-EF | grep test

515 29710 21734 0 11:47 pts/12 00:00:00/bin/sh./test.sh

515 29713 21734 0 11:47 pts/12 00:00:00 grep test

$ setsid./test.sh &

[1] 409

$ PS-EF | grep test

515 410 1 0 11:49? 00:00:00/bin/sh./test.sh

515 413 21734 0 11:49 pts/12 00:00:00 grep test

The above experiment demonstrates the use of Nohup/setsid Plus & causes the process to run in the background without being affected by the current Shell the impact of the exit. So what do you do with 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 11:49? 00:00:00/bin/sh./test.sh

515 2542 21734 0 11:52 pts/12 00:00:00 grep test

There is another way, even if the process is in a Subshell implemented in fact this and Setsid similarities. The method is simple, enclose the command in parentheses () :

$ (./test.sh &)

$ PS-EF | grep test

515 410 1 0 11:49? 00:00:00/bin/sh./test.sh

515 12483 21734 0 11:59 pts/12 00:00:00 grep test

Note: This test environment is Red Hat Enterprise Linux as Release 4 (Nahant Update 5), Shell to be /bin/bash , the different OS and the Shell there may be different commands. For example AIX ksh, no disown, but can use nohup-p PID to get disown the same effect.

There is also a more powerful way to use Screen , first create a disconnected mode virtual terminal, and then use the - R option to reconnect the virtual terminal, and any commands executed in it can be reached Nohup effect, which is convenient when there are multiple commands that need to be executed in the background in succession:

$ SCREEN-DMS Screen_test

$ screen-list

There is a screens on:

27963.screen_test (Detached)

1 Socket In/tmp/uscreens/s-jiangfeng.

$ screen-r Screen_test

Third, Nohup

If you are running a process and you feel that the process will not end when you exit the account, you can use the nohup command. This command can continue to run the appropriate process after you exit the account. nohup is the meaning of not hanging (no Hang Up). the general form of the command is:

Nohup Conmmand &

If you use Nohup command to submit a job, all output of the job is redirected to a file named Nohup.out file, unless the output file is specified separately:

Nohup Command > Myout.file 2>&1

in the example above, the output is redirected to the Myout.file file.

Four, .* ,? ,[...] , [!...] wait

Here are the special characters:

* matches any string in the file name, including an empty string.

? matches any single character in the file name.

[...] Match Any characters that are contained in [] .

[!...] Match [] Central African exclamation point! The character after.

when s H e l l when these characters are encountered, they are treated as special characters rather than ordinary characters in the file name, so that the user can use them to match the corresponding file name.

1) Lists the I or o The file name begins with: #ls [io]*

2) List log. the file name that starts, follows a number, and can then be any string: #ls Log. [0-9]*

3) In contrast to example two, list log. a file name that starts, doesn't follow a number, and can then be any string : #ls log. [!0-9]*

4) Lists all the LPS The beginning, the middle can be any two characters, and finally the 1 End of file name: #ls LPS?? 1

5) list all filenames that begin with an uppercase letter: $ ls [a-z]* 6) Lists all the . the file name at the beginning (an implied file, such as . profile , . rhosts , . Histo ry , etc.) : $ ls. *

    • Other Related commands:

Jobs : See how many commands are currently running in the background

FG : The command in the background is moved to the foreground to continue running. If there are multiple commands in the background, you can use FG%jobnumber to bring up the selected command,%jobnumber through the jobs ordinal ( not pid) of the command being executed in the background that the command was traced to

BG : A command that pauses in the background changes to continue execution. If there are multiple commands in the background, you can use BG%jobnumber to bring up the selected command,%jobnumber through the jobs ordinal ( not pid) of the command being executed in the background that the command was traced to

    • Kill process

Killing a program that has already started is the same as the normal way:

    • pkill-9 name
    • Killall name
    • Kill PID
    • ...
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.