Enable programs to run in the background under Linux (Turn off the terminal to keep the program running) __linux

Source: Internet
Author: User
first, why should the program in the background to perform

The programs we calculate are very long periods, usually in hours or even one weeks. The environment we use is remotely connected to a Japanese Linux server using Putty. So running a program in the background has the following three benefits:

Whether we turn off the machine does not affect the Japanese side of the program running. (Not like before, we this network a disconnect, or a shutdown, the program will be broken or can not find data, ran a few days of the program only to start again, very annoying)

Does not affect computational efficiency

Let the program run in the background, will not occupy the terminal, we can use the terminal to do something else. second, how to make the program in the background to perform

There are many ways to do this, here are two kinds. If we have a program pso.cpp, and we compile it to produce the executable file PSO, we have to make the PSO run in the background of the Linux server. After the client shuts down and then log back into the server, continue to view the results of the operation that was originally in the terminal output. (assuming the actions are in the current directory) Method 1 enters the command at the terminal:

Explanation: The PSO is run directly in the background and the terminal output is stored in the Pso.file file in the current directory.

  When the client shuts down and then logs back to the server, the Pso.file file is viewed directly to see the execution result (life

Order: #cat Pso.file). Method 2 in the Terminal input command:

# nohup  /pso > Pso.file 2>&1 &

Explanation: Nohup is not suspended meaning, will be the PSO directly in the background to run, and the terminal output stored in the current

Directory in the Pso.file file. After the client shuts down and logs back to the server, view the Pso.file directly

The file can look at the execution result (command: #cat pso.file). iii. Common task management commands

Jobs      //view tasks, return task number N and process number

BG  %n//Turn the task number n into   the background run

FG  %n   //Turn the task number N to the foreground run

ctrl+z    //Suspend current task

CTRL + C    //End Current task

Note: If you want to make the task run in the background the day before yesterday, use Ctrl+z to suspend the task, and then use BG to make it perform in the background. actual combat: 1, at the start of the task behind the background to perform

In Linux, if you want the process to run in the background, in general, we add & to the command, which in effect puts the command in a job queue:

$./test.sh &  
[1] 17208   
$ jobs-l  
[1]+ 17208 Running           ./test.sh &       
2, the front end of the task to carry out the background

For commands that have already been executed in the foreground, you can also put them back in the background, start by pressing Ctrl+z to pause the processes that are already running, and then use the BG command to run the stopped jobs in the background:

$./test.sh 
[1]+  Stopped               ./test.sh  
$ bg%1 
[1]+./test.sh &  
$ jobs-l 
[1]+ 22794 Running          
3, prevent the termination of the terminal after the stop

The above mentioned process is placed in the background, the parent process is the current terminal shell process, and once the parent process exits, the hangup signal is sent to all child processes, and the child process will exit after the Hangup is received. If we want 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   

The above experiment demonstrates the use of Nohup/setsid plus & to make a process run in the background without being affected by the current shell exit. 4. Similarly, for tasks that have been started on the front end, we also have a way to: 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   
Extra Articles ^_^ cheats 1 Subshell Way

The process is executed in a subshell, which in fact is similar to SETSID. 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   

Note: The test environment for this article is red Hat Enterprise Linux as Release 4 (Nahant Update 5), the shell is/bin/bash, and different OS and Shell may command somewhat differently. For example, Aix ksh, there is no disown, but can use nohup-p pid to get disown the same effect. Cheats 2 Screen mode

First create a disconnected mode of the virtual terminal, and then reconnect the virtual terminal with the-r option, any commands executed in it can achieve 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 screens on:
              27963.screen_test       (Detached) 1 Socket in/tmp/u Screens/s-jiangfeng.  

Original from: http://www.cnblogs.com/little-ant/p/3952424.html

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.