Linux Program background Run

Source: Internet
Author: User
Tags rsync

Linux Command background run

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 of the Microsoft platform is the real multi-user, multi-tasking. Therefore, in the task management also has the characteristic management thought.
We know that on Windows, 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: (the number behind BG is the job number)
#bg 1
[1]+/root/bin/rsync.sh &
Use the Jobs command to view the tasks that are running:
#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 pauses the foreground task in the background
Jobs View the working status of the background
FG%jobnumber The backstage task to the front desk for processing.
BG%jobnumber put tasks in the background to handle
Kill management tasks in the background

Second, &

In 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 file called Out.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 a file named "httpd.conf" and redirect all 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 a command that has already been executed in the foreground, it can be re-placed in the background, first by pressing CTRL + Z to pause the process that is already running, and then using the BG command to put the stopped job in the background, for example, by using CTRL + Z to suspend the tesh.sh that is executing in the foreground:
$./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 process of the current terminal shell, and once the parent process exits, the hangup signal is sent to all child processes, and the child process will exit after receiving hangup. 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 to set the parent process to the INIT process (process number 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 & to make the process run in the background without being affected by the current shell 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 also a way, even if the process is executed in a subshell, in fact, this is similar to Setsid. 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: The test environment for this article is red Hat Enterprise Linux as Release 4 (Nahant Update 5), Shell is/bin/bash, different OS and Shell may command somewhat differently. For example, Aix ksh, there is no disown, but you can use the Nohup-p PID to achieve disown the same effect.

There is also a more powerful way is to use screen, first create a disconnected mode of the virtual terminal, and then use the-r option to reconnect the virtual terminal, in which the execution of any command, can achieve the effect of nohup, which is more convenient when there are multiple commands in the background continuous execution:

$ 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 submit a job using the Nohup command, all output from 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 example above, the output is redirected to the Myout.file file.

    • 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 is the ordinal (not PID) of the command being executed in the background through the jobs command.
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 is the ordinal (not PID) of the command being executed in the background through the jobs command.

Sometimes, we need to start a program in the terminal and make it run-but if you close the terminal, then the program is closed. So is there any way to keep the programs that have been started from this terminal running after shutting down the terminal?

Pre-Knowledge: the difference between xterm,console,tty,pts,pty
  • The shell is something that communicates directly with the kernel.
  • Xterm is a software concept that can be connected to the console via this program to control the host, which can be understood as a CLI-like terminal emulator, while Gnome-terminal,konsole is a GUI-style terminal emulator
  • Console is the console of a host and is a physical concept.
  • TTY, Pty, PTS are terminals and are hardware or device concepts.
  • TTY is a generic term for all end devices
  • Pty is one of them, pseudo terminal, or virtual terminal
    • "&" command line End method:

Under Unix/linux If you want the program to run independently of the terminal, it is generally used & at the end of the command to let the program run automatically. (Can not append space after command)

Open gnome-terminaland execute the following command:

There are a few points to note:
      1. The program that has been started is still attach to the current PTS, and only the current terminal emulator is closed (exiting with the Exit command), and the process is automatically inherited by the TTY.
[email protected]:~$ ps-e | grep totem//program has been started in Totem &, currently attached to Pts0 8819 pts/0 00:00:00 totem[email Protect ed]:~$ PS-E |        grep Totem//PTS0 's analog terminal is closed by the Exit command, Totem automatically attached to TTY8819? 00:00:00 Totem[email protected]:~$
      1. Process with debug output, you need to press the ENTER key to interrupt the current debug output. However, if the program continues to printf, you will not be able to enter any commands.
[email protected]:~$ VLC &[1] 8850[email protected]:~$ VLC media player 1.0.6 Goldeneye[0x8b998b0] Main Libvlc:running VLC with the default interface. Use ' CVLC ' to use VLC without interface.                      &NBSP ;                          ,         &NB Sp    //enter pressed[email protected]:~$                   &NBSP ;           //show a clean terminal now** (: 8850): CRITICAL * *: giop_thread_request_push:assertion ' Tdata! = NULL ' failed                            nbsp                                 //Still In output data ......                      &NBSP                          ,         &NB Sp    //shutdown program [1]+ done vlc[email protected]:~$
    1. You cannot log the debug output of the program.
    2. This terminal can be closed only if the virtual terminal is $ or #, otherwise it may cause the process that has started to be closed (press enter--If the program continues to output information without appearing $ or #)
    • Use the Nohup command :

Nohup Description: Run command, ignoring hangup signals. (Ignore any interrupt/suspend signal to keep the command executing)

But when you try to use the command:

1 nohup command

You will encounter a moderate problem ...

[Email protected]:~$ nohup vlcnohup:ignoring input and appending output to ' nohup.out '

Yes, although it automatically logs debug information to the Nohup.out file, you can't do anything with this terminal.

So you need to mix with the first method, namely

nohup command {option} &

When mixed, it automatically logs the output of the command you executed to a file named nohup.out with the permission -RW ——-.

But you still need

[Email protected]:~$ nohup VLC &[1] 9045[email protected]:~$ nohup:ignoring input and appending output to ' nohup.out '                                                         Click here to enter or CTRL + C to [email protected]:~$ 

As with the use of the "&" nature, the current launcher terminal if not closed, the program has been started attached to the PST, if the terminal is closed, it is automatically attached to the TTY.

If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out. By default,nohup output to the nohup.out file by default, and you can also use redirection to specify the output file:

nohup command {option} > Myout.file 2>&1 &

This terminal can be closed only if the virtual terminal is $ or #, otherwise it may cause the process that has started to be closed (press enter--If the program continues to output information without appearing $ or #)

    • 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 is the ordinal (not PID) of the command being executed in the background through the jobs command.
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 is the ordinal (not PID) of the command being executed in the background through the jobs command.

    • Kill process

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

  • Pkill-9 Name
  • Killall Name
  • Kill PID
  • ...
    • Resources

Http://topic.csdn.net/u/20100201/17/a34370cc-8a61-4315-a4d0-84242362064d.html
Http://www.linuxsir.org/bbs/thread362001.html
Http://www.williamlong.info/archives/482.html
Http://dev.firnow.com/course/6_system/linux/Linuxjs/2008716/133186.html

Linux Program background Run

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.