"Go" causes the program to run in the background under Linux (the method of shutting down the terminal to keep the program running)

Source: Internet
Author: User

First, why do you want to make the program in the background

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

1: Whether the shutdown of our side does not affect the Japanese side of the program run. (not as before, we this network a disconnect, or a shutdown, the program will be broken or unable to find data, run a few days of the program can only start again, it is a worry)

2: No effect on computational efficiency

2: Let the program run in the background, will not occupy the terminal, we can use the terminal to do other things.

Second, how to make the program in the background execution

There are many ways to do this, and here are a list of two. If we have a program pso.cpp, generate the executable PSO after compiling , we want to make PSO in the Linux server background execution. After the client shuts down and re-login to the server, continue to view the results of the operation that was originally in the terminal output. (assuming the operation is in the current directory)

method 1 in the terminal input command:

#./PSO > Pso.file 2>&1 &

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

After the client shuts down and logs back on to the server, a direct view of the Pso.file file can see the results of the execution (Life

Order:#cat pso.file).

method 2 in the terminal input command:

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

Explanation:nohup is not suspended meaning, 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 on to the server, view 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//To run a task with number n in the background

# FG%n//The task that is numbered n is run to the foreground

# 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, you should use Ctrl + Z to suspend the task, and then use BG to make it perform in the background.

Report:

In Linux, if you want the process to run in the background, in general, we'll add & after the command, in fact, this is putting the command into a job queue:


[1] 17208

[1]+ 17208 Running

For commands that have already been executed in the foreground, they can also be re-executed in the background, first by pressing CTRL + Z to pause the already running process, and then using the BG command to put the stopped job in the background:


[1]+ Stopped ./test.sh
$ bg%1 [1]+./test.sh &

[1]+ 22794 Running

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)


21734

[1] 29016

515 29710 21734 0 11:47 PTS/12
515 29713 21734 0 11:47 PTS/12

[1] 409

515 410 1 0 11:49?
515 413 21734 0 11:49 PTS/12

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:


[1] 2539

[1]+ 2539 Running ./test.sh &
$ disown-h%1

515 410 1 0 11:49?
515 2542 21734 0 11:52 PTS/12

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 &)  

515 410 1 0 11:49?
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), 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 to use screen, first create a disconnected mode virtual terminal, and then use the-r option to reconnect the virtual terminal, in which any command executed, can achieve nohup effect, This is convenient when there are multiple commands that 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/uscreens/s-jiangfeng.

"Go" causes the program to run in the background under Linux (the method of shutting down the terminal to keep the program running)

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.