When we work on a terminal or console , Maybe you don't want a job to occupy the screen , Because there may be more important things to do , Like reading email . For processes with intensive disk access , We'd like it to be able to run during off peak hours of the day ( For example, in the early morning ). To enable these processes to run in the background , That is, not running on the terminal screen , There are several options available .
- & When running a job in the foreground , The terminal is occupied by the job ; You can add... After the command & Realize background operation . for example ：sh test.sh & Commands suitable for running in the background are f i n d、 Time consuming sorting and some s h e l l Script . Be careful when running jobs in the background ： Commands that require user interaction should not be executed in the background , Because then your machine will wait there . however , Jobs running in the background will also output the results to the screen , Interfere with your work . If you run a job in the background, it will produce a lot of output , It's best to use the following method to redirect its output to a file ：
command > out.file 2>&1 &
such , All standard output and error output will be redirected to a location called out.file In the file of .
PS： When you successfully submit the process , A process number will be displayed , It can be used to monitor the process , Or kill it .(ps -ef | grep Process number perhaps kill -9 Process number ）
- nohup Use & After the command , Jobs are submitted to run in the background , The current console is not occupied , But once you turn off the current console ( When exiting an account ), The job will stop running .nohup Command can continue to run the corresponding process after you exit the account .nohup Just don't hang up ( no hang up). The general form of the order is ：
nohup command &
If you use nohup Order to submit the assignment , By default, all the output of the job is redirected to a file named nohup.out In the file of , Unless you specify an output file ：
nohup command > myout.file 2>&1 &
Used nohup after , A lot of people just don't care , In fact, this may happen when the current account exits or ends abnormally , The order ended itself . So it's using nohup After the command runs in the background , Need to use exit Exit the current account normally , This ensures that the command runs in the background all the time .
- ctrl + z You can put a command that is being executed in the foreground in the background , And it's suspended .
- Ctrl+c Terminate the front desk command .
- jobs See how many commands are currently running in the background . jobs -l Option to display... For all tasks PID,jobs The state of can be running, stopped, Terminated. But if the mission is terminated （kill）,shell From the present shell Delete the process ID of the task from the list known to the environment .
- 2>&1 analysis
command >out.file 2>&1 &
- command>out.file Yes, it will command The output of is redirected to out.file file , That is, the output content is not printed on the screen , But output to out.file In file .
- 2>&1 Is to redirect standard errors to standard output , The standard output here has been redirected to out.file file , The standard error is also output to out.file In file . the last one &, Let the command be executed in the background .
- Just imagine 2>1 What is the ,2 And > Combination represents error redirection , and 1 On behalf of the error redirection to a file 1, It doesn't mean standard output ; Switch to 2>&1,& And 1 The combination represents the standard output , It becomes an error redirection to standard output .
That's all Liangxu tutorial website For all of you Linux Related knowledge .
本文为[Pioneer of code farmers]所创，转载请带上原文链接，感谢