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 : Does not affect 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 a lot of methods, here are listed two kinds. If we have a program pso.cpp, Generate the executable file by compiling pso We want to make pso Span style= "line-height:1.5" >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: Will pso run directly in the background and store the terminal output in the current directory pso.file file.

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 just don't hang up the meaning that will PSO run directly in the background and store the terminal output in the current

under the directory 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    // will be numbered n Task to background run

# fg    %n    // will be numbered n Task 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, you must first use Ctrl + Z suspend the task, and then use the BG make it perform in the background.


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 $ jobs-l [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./ $ bg%1 [1]+./ & $ jobs-l [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)

$ echo $$ 21734 $ nohup./ & [1] 29016 $ ps-ef | grep test 515 29710 21734 0 11:47 pts/12 00:00:00/bin/sh./ 515 29713 21734 0 11:47 PTS/12 00:00:0 0 grep Test
$ setsid./ & [1] 409 $ PS-EF |        grep test 515 410 1 0 11:49? 00:00:00/bin/sh./ 515 413 21734 0 11:49 pts/12 00:00:00 grep test

 $ ./ & [1] 2539 $ jobs -l [1]+  2539  running                 . / & $ disown -h %1 $ ps -ef | grep test  515        410     1  0  11:49 ?        00:00:00 /bin/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 ():

$ (./ &) $ ps-ef |        grep test 515 410 1 0 11:49? 00:00:00/bin/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 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 screen on:27963.screen_test (Detached) 1 Socket in/tmp /uscreens/s-jiangfeng. $ screen-r Screen_test

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: 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.