Experience sharing: How to analyze the program running in the background under Linux-Linux has to learn this!

1. Why should the program be executed in the background

Experience sharing: How to run a program in the background under Linux—Linux has to learn this way

 

Our calculation procedures are very long, usually several hours or even a week. The environment we use is to use putty to remotely connect to a Japanese Linux server. So making the program run in the background has the following three benefits:

1: Whether we shut down or not will not affect the operation of programs in Japan. (It won’t be like before, when our network is disconnected or shut down, the program will be broken or no data can be found. A program that has been running for a few days can only be restarted, which is very annoying)

2: Does not affect calculation efficiency

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

2. How to make the program execute in the background

There are many methods, here are two main ones. If we have the program pso.cpp, the executable file pso is generated after compilation, and we need to make the pso execute in the background of the linux server.

After the client is shut down, log in to the server again and continue to view the running results originally output on the terminal. (Assuming all operations are in the current directory)

Method 1 Enter the command in the terminal:

Experience sharing: How to run a program in the background under Linux—Linux has to learn this way

 

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

Explanation: Put pso directly in the background to run, and store the terminal output in the pso.file file in the current directory.

After the client is shut down and log in to the server again, you can directly view the pso.file file to see the execution result (command

令:#cat pso.file )。

 

Method 2 Enter the command in the terminal:

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

Explanation: nohup means not to hang, put pso directly in the background to run, and store the terminal output in the current

Pso.file in the directory. When the client is shut down and re-login to the server, directly view the pso.file

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

Three, common task management commands

Experience sharing: How to run a program in the background under Linux—Linux has to learn this way

 

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

# bg %n //Transfer the task number n to the background

# fg %n //Transfer task number n to the foreground

# ctrl+z //Suspend the current task

# ctrl+c //End the current task

Note: If you want to make the task executed the day before yesterday run in the background, you must first use ctrl+z to suspend the task, and then use bg to execute it in the background.

Click for more information, more free open source projects and courses are waiting for you to watch!

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/109183276
Recommended