[Linux] Job Management

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.