When we execute commands (such as tar) in Linux Bash, they are all performed on the foreground by default, that is, we must wait until the command ends before executing Subsequent commands. In fact, we can control command execution in the background, which is the Job management of Linux.
&-Drop the command to the background for execution
Add & at the end of the command to throw the command to the background for execution, as shown below:
# tar -zcvf source.tar.gz ./source > /tmp/source_tar.log 2>&1 &[1] 10015
Note 1: although we put the job in the background, the standard output and standard error output will still interfere with the foreground, so generally, for the background job, all use output redirection, write the output information to the file, without interfering with the front-end work.
NOTE 2: When you drop the command to the background, the screen will display a line of information (the 2nd rows shown above), 1st columns represent the job number, and 2nd columns represent the PID.
[Ctrl]-z-drop the current job to the background and pause. If I am executing a compression command, this command takes a long time, as shown below:
[root@lx15 /opt ]# tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1
If I press [ctrl]-z, it will throw the job to the background and pause it, as shown below:
[1]+ Stopped tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1
Jobs-view the current background working status
Jobs [-lrs]-l: list job details-r: only list jobs in the run state: only jobs in the stop status [root @ lx15/opt] # jobs-l [1] + 10647 Stopped tar-zcvf oracle.tar.gz. /oracle>/tmp/oracle_tar.log 2> & 1
Fg-Move background work to the foreground
fg %jobnumber# fg %1tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1
Bg-Change Background work to running
[root@lx15 /opt ]# jobs[1]+ Stopped tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1[root@lx15 /opt ]# bg %1[1]+ tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1 &[root@lx15 /opt ]# jobs[1]+ Running tar -zcvf oracle.tar.gz ./oracle > /tmp/oracle_tar.log 2>&1 &
Nohup-run it offline. We can know from the above that adding & after the command can make it run in the background, but once you exit or cancel the terminal (exit), the command will be terminated. If you want your command to run after exit or logout, you must add nohup before the command.
Nohup [command] -- run offline on the foreground
Nohup [command] & -- offline background running