1、 function .sh file
Direct use ./sh Files can be run , But if you want to run in the background , Even if the current terminal can be turned off , need nohup Command and & command .
function ： At the end of an order , This command can be executed in the background
function ： Run command without hanging up
2、 View the current background running command
There are two commands you can use ,jobs and ps, The difference is that jobs Used to view the tasks running in the background of the current terminal , If you change the terminal, you can't see it . and ps The command is used to view the dynamics of a transient process , You can see background processes running on other terminals .
function ： View the tasks running in the background of the current terminal jobs -l Option to display all tasks of the current terminal PID,jobs The state of can be running,stopped,Terminated.+ The sign indicates the current task ,- The sign indicates the next mission .
function ： View all current processes ps -aux | grep "test.sh" #a: Show all programs u: Display in user oriented format x: Show all programs , Do not differentiate by terminal
3、 Turn off the current background running command
kill command ： The end of the process （1） adopt jobs Command view jobnum, And then execute kill %jobnum （2） adopt ps Command to view the process number PID, And then execute kill %PID If it's a foreground process , Direct execution Ctrl+c You can stop
4、 Switching and control of foreground and background processes
（1）fg command function ： Transfer the commands in the background to the foreground to continue If there are multiple commands in the background , You can use first jobs see jobnun, And then use fg %jobnum Call up the selected command . （2）Ctrl + z command function ： Put a command that is being executed in the foreground in the background , And it's suspended （3）bg command function ： A command to pause in the background , To continue to execute in the background If there are multiple commands in the background , You can use first jobs see jobnum, And then use bg %jobnum Call out the selected command to continue execution .
5、 function Python Example
[[email protected] nlp]# nohup python -u jbabs.py > out.log 2>&1 &  20087 [[email protected] nlp]# jobs + Running nohup python -u jbabs.py > out.log 2>&1 &
Run one Python Script , Usually set as follows
$ python /data/python/server.py >python.log &
1、 > It means to put the standard output （STDOUT） Redirect to That document , Here's a redirection to python.log
2、 & Indicates that the script is executed in the background
This will achieve the goal , however , We quit shell At the window , Must use exit Command to exit , otherwise , After you quit , The process will also follow shell And disappear （ sign out 、 close ）
1、1 It's standard output （STDOUT） File descriptor for ,2 It's standard error （STDERR） File descriptor for
1> python.log Simplified as > python.log, To redirect standard output to python.log This file
2、2>&1 To redirect standard error to standard output , here &1 Indicates standard output , Why standard errors need to be redirected to standard output , Because standard error has no buffer , and STDOUT Yes . This will lead to commond > python.log ,2> python.log file python.log Opened twice , and STDOUT and STDERR Will compete to cover , This is definitely not what we want
3、 Okay , We can now shut down shell window （ I use it SecureCRT, There's more to use Xshell）, Instead of typing exit This is the order to exit shell 了
$ ps aux|grep python
tomener 1885 0.1 0.4 13120 4528 pts/0 S 15:48 0:00 python /data/python/server.py
tomener 1887 0.0 0.0 5980 752 pts/0 S+ 15:48 0:00 grep python
When we close directly shell window , Then connect to the server , see Python The process of , The discovery process is still
however , stay python You can't see the output while running ！
python The output of is buffered , Lead to python.log3 You can't see the output right away .
Use -u Parameters , bring python Do not enable buffering .
So the correction order , It can be used normally
$ nohup python -u test.py > out.log 2>&1 &