Run the program in the background of Linux

Source: Internet
Author: User

I. Why do I need to execute the program in the background?

Our computing programs have a long cycle, usually several hours or even a week. We use Putty to remotely connect to a Linux server in Japan. Therefore, running the program in the background has the following three benefits:

1: whether or not we shut down the server does not affect the program running in Japan. (As before, when we disconnected the network or shut down the network, the program was disconnected or could not find the data. After a few days of running the program, we had to re-start the program. It was very annoying)

2: computing efficiency is not affected.

2: After the program runs in the background, it will not occupy the terminal. We can use the terminal to do other things.

Ii. How to make the program run in the background

There are many methods. Here we mainly list two methods. If we have a program named PSO. cpp that generates executable files after compilation, we need to make the program run in the Linux Server background. After the client is shut down, log on to the server again and continue to view the operation results originally output on the terminal. (Assume that all operations are in the current directory)

Method 1Enter the following command on the terminal:

#./PSO> PSO. File 2> & 1 &

Explanation: run the PSO directly in the background and store the terminal output in the PSO. File file in the current directory.

After the client is shut down and re-logged on to the server, view the PSO. File file to view the execution result (

Command: # Cat PSO. file ).

Method 2Enter the following command on the terminal:

# Nohup./PSO> PSO. File 2> & 1 &

Explanation: nohup means no suspension. It runs the PSO directly in the background and stores the terminal output in the current

Directory. After the client is shut down and re-logged on to the server, view the PSO. file directly.

File to view the execution result (command: # Cat PSO. file ).

Iii. Common Task Management commands

# Jobs // view the task and Return Task Number N and process number

# BG % N // run the task numbered N in the background

# FG % N // run the task numbered N on the frontend.

# Ctrl + z // suspend the current task

# Ctrl + C // end the current task

 

Note: If you want to place the task executed the day before yesterday in the background, you must first use Ctrl + Z to suspend the task and then use BG to execute it in the background.

 

 

 

Appendix:

 

 

In Linux, if you want to run the process in the background, we usually add & to the end of the command. In fact, the command is put into a Job Queue:

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

For commands that have been executed in the foreground, you can also re-execute them in the background.CTRL + zPause a running process, and then useBGCommand to run 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, for processes executed from the top to the backend, the parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to useNohupIgnore the hangup signal, orSetsidSet the parent process as 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 test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? AvailableDisownCommand:

$ ./test.sh &[1] 2539$ jobs -l[1]+  2539 Running                 ./test.sh &$ disown -h %1$ ps -ef | grep test515        410     1  0 11:49 ?        00:00:00 /bin/sh ./test.sh515       2542 21734  0 11:52 pts/12   00:00:00 grep test

Another method is to execute a process in a subshell, which 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 in this article is Red Hat Enterprise Linux as Release 4 (nahant Update 5) and shell is/bin/bash. Different OS and shell may have different commands. For example, the KSh of Aix does not have disown, but can be usedNohup-PPIDTo get the same effect as disown.

Another more powerful method is to useScreenFirst, create a virtual terminal in disconnected mode, and then use the-r option to reconnect to the virtual terminal. Any Command executed in the terminal 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 -listThere is a screen on:        27963.screen_test       (Detached)1 Socket in /tmp/uscreens/S-jiangfeng.$ screen -r screen_test

 

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.