Make the program run in the background under Linux

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:

$./test.sh &[1] 17208$ jobs-l[1]+ 17208 Running                 ./test.sh &

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:

$./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 test515      29710 21734  0 11:47 pts/12 00:00:00/bin/sh   ./test.sh515      29713 21734  0 11:47 PTS/12   00:00:00 grep test
$ setsid./test.sh &[1] 409$ ps-ef | grep test515        410     1  0 11:49?        00:00:00/bin/sh./test.sh515        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 test515        410< C11/>1  0 11:49?        00:00:00/bin/sh./test.sh515       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 test515        410     1  0 11:49?        00:00:00/bin/sh./test.sh515      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 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-listthere is a screens on:        27963.screen_test       (Detached) 1 Socket in/tmp/uscreens /s-jiangfeng.$ Screen-r Screen_test

Reprinted from Http://www.cnblogs.com/xianghang123/archive/2011/08/02/2125511.html

Make the program run in the background under Linux

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.