Enable programs to run in the background under Linux __linux

Source: Internet
Author: User

One, why to make the program line

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:

1: Our side whether the shutdown 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)

2: Does not affect the computational efficiency

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

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 numbered N to run in the background

# FG%n//Transfer the task number N 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, 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 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 &

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                 ./test.sh &

However, as in the process above to the background, the 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 exits 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   00:00:00 grep test

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. What do you do with a process that is 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 in a subshell execution, in fact, this and setsid similar. 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), 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.

A more powerful way is to use screen to first create a disconnected mode virtual terminal, and then reconnect the virtual terminal with the-r option, and any commands executed in it can achieve the nohup effect. This 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 sockets In/tmp/uscreens/s-jiangfeng.

$ screen-r Screen_test

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.