How to make the program run in the background under Linux

  
 

First, why should the program be executed in the background

The programs we calculate are all very long-term, usually hours or even weeks. The environment we use is to connect to a Japanese Linux server remotely using putty. So having the program run in the background has the following three benefits:

1: Whether we shut down this side does not affect the program running in Japan. (Not as before, when our network is disconnected, or a shutdown, the program will be broken or unable to find data, the program that runs for a few days can only come back again, it is very troublesome)

2 : Does not affect the efficiency of calculation

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

Second, how to make the program execute in the background

There are many methods, here are two mainly listed. If we have the program pso.cpp, after compiling to generate the executable file pso, we want to make the pso execute in the background of the Linux server. When the client shuts down and then logs back in to the server, it continues to view the results of the output that was originally output at the terminal. (Assuming the operations are all in the current directory)

Method 1 Enter the command at the terminal:

# ./pso > pso.file 2>&1 &

Explanation: PSO is placed directly in the background, and the terminal output is stored in the pso.file file in the current directory.

When the client is shut down and re-login to the server, you can directly view the pso.file file to see the execution result (Life

Order: #cat pso.file).


Method 2 Enter the command at the terminal:

# nohup ./pso > pso.file 2>&1 &

Explain: Nohup means not hanging, put pso directly in the background, and store the terminal output in the pso.file file in the current

directory. After the client shuts down and logs back in to the server, directly view the pso.file

file to see the execution result (command: #cat pso.file).

Three, common task management commands

# jobs //View tasks, return task number n and process number

# bg %n //will be numbered n task Run to the background

# fg %n //Run the task numbered n to the foreground

# ctrl+z //suspend the current task

# ctrl+c //End the current task


Note: If you want to put the task to run in the background the day before, then first use ctrl + z to suspend the task, then use bg to make the post execution .








In Linux, if you want Let the process run in the background. In general, we can add & after the command. In fact, this puts the command 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. First press ctrl+z to pause the running process, then Use 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 from the top to the background, the parent process is still the process of the current terminal shell, and once the parent process exits, it will send a hangup signal to all child processes. The child process will also quit after receiving the hangup. If we want to continue running the process while exiting the shell, we need to use nohup to ignore the hangup signal, or setsid will set the parent process to the init process (process number is 1)

$ echo $$ 21734 $ nohup . /test.sh & [1] 29016 $ ps -ef

Copyright © Windows knowledge All Rights Reserved