linux Background Execution Command and Specific Use of nohup

  • 2021-06-28 14:38:46
  • OfStack

When we are working in a terminal or console, we may not want to occupy the screen by running a job, because there may be more important things to do, such as reading e-mail.For processes with intensive disk access, we would prefer it to be able to run during the non-load peak times of the day (for example, in the morning).In order for these processes to run in the background, that is, not on the terminal screen, there are several options available.

&

When a job is run in the foreground, the terminal is occupied by the job;You can add a command after it & Implement background runs.For example: sh test.sh &
Commands suitable for running in the background are f i n d, time-consuming sorting, and a few s h e l l scripts.Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, as your machine will be silly waiting there.However, running a job in the background will output the results to the screen, interfering with your work.If a job running in the background produces a large amount of output, it is best to redirect its output to a file using the following methods:


command > out.file 2>&1 & 

This redirects all standard and error outputs to a file called out.file.

PS: When you submit a process successfully, a process number will be displayed which can be used to monitor the process or kill it. (ps-ef | grep process number or kill-9 process number)

nohup

Use & After the command, the job is submitted to run in the background and the current console is not occupied, but 1 when the current console is closed (when the account is exited), the job stops running.The nohup command can continue running the process after you exit the account.nohup means no suspension (no hang up).The general form of this command is:


nohup command &

If you submit a job using the nohup command, all output from the job is redirected by default to a file named nohup.out unless an output file is specified:


nohup command > myout.file 2>&1 &

After using nohup, many people just let go. In fact, it's possible that when the current account exits or ends abnormally, the command ends itself.Therefore, after running the command in the background using the nohup command, you need to exit the current account normally using exit to ensure that command 1 runs in the background.

ctrl + z
You can put a command that is being executed in the foreground behind the scenes and in a paused state.

Ctrl+c
Terminate foreground command.

jobs
See how many commands are currently running in the background.

The jobs-l option displays the PID for all tasks. The status of jobs can be running, stopped, Terminated.However, if the task is terminated (kill), shell deletes the process identity of the task from the list known to the current shell environment.

2 > & 1 parse


command >out.file 2>&1 &
command > out.file redirects the output of command to the out.file file, that is, the output is not printed to the screen, but to the out.file file. 2 > & 1 is to redirect standard errors to standard output, where standard output has been redirected to the out.file file, that is, to the out.file file.Last 1 & Is to have the command executed in the background. Imagine 2 > What does 1 mean, 2 and > Combination represents error redirection, while 1 represents error redirection to a file 1, not to standard output;Replace with 2 > & 1, & When combined with 1, it represents standard output and becomes an error redirecting to standard output.

Related articles: