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
Requirement Description Set SNAT policy using iptables Make hosts on 192.168.100.0/24 network segmen
Under Linux, the IDE device is named after hd. Generally, there are two IDE interf
I will talk about how to use the command in Linux to test the read and write input of the hard disk
1. Introduction to LVS1. Provide high availability and load balancing capabilities for application s
The basic concept of Linux server cluster
In-depth discussion of Varnish cache hit rate
Linux operating system kernel startup parameters detailed analysis
How to add Nginx to system services
Explain the various problems that occur when Fedora 9 is installed.
Neighbor subsystem of linux protocol stack (related process 6)
Analyze the Linux firewall framework from five aspects
Error:storage class specified for parameter problem
Teach you how to complete Linux network address configuration
Download address: Windows 10 Build 10056 latest image leaked
Win10 mobile version of the annual version has not been updated
How to close the Metro application Metro interface program Win8 how to exit
Win8 experience serialized second hit taskbar and personal account! (1)
Win XP taskbar changes to display a double-layer solution
Set the timing to boot? Use CMOS (1)
Who moved my computer? Win7 helps you monitor the whole process.
Experts share the Win 7 problem step recorder operation guide