Work Management in Linux (Job control)

Source: Internet
Author: User

Before using Linux will always accidentally press CTRL + Z, and then appear to understand the situation, thinking that the program suddenly did not, today specializes in the next Linux under a few shortcut keys and work management.

One of them found a very good article, most of it is reproduced inside.

Original address: http://blog.chinaunix.net/uid-26495963-id-3062757.html

I. Concept of Job

A job is a generic term for a series of operations that perform a task in a batch-processing environment. In the era of personal contact with computers, the batch environment has not been seen, only a number of special industries and environments are still using such concepts, only in the book contact.
Under Linux, you can simply interpret the job as one or more of the commands that are linked through the pipe and the tasks they perform.
For example, I need to log the current login user information to a file named Loginuser, then I might go back to the following command to complete:
[email protected]:~$ who > Loginuser
They can make the entire process from the execution of the command to the output of the Loginuser file a job.

two. Specific meanings of ctrl-z, Ctrl-c, ctrl-d under Linux

The first knowledge of Linux, may be ctrl-z, ctrl-c, ctrl-d the specific meaning of confusion, because these three keys can in some cases let the shell out of the executing command or program, prompting the user to enter the next command, so that the first and try Illusion, It is considered that the function of the three keys is the same, but the key can play a role in the specific situation, but the action is uncertain.

Ctrl-z: The key is the default suspend key (Suspend key) under Linux, and when you type Ctrl-z, the system suspends the running program (notice that it hangs or pauses), and then puts it in the background, giving the user related job information. At this point, the program does not really stop, the user can restore the job to the context before the pause by using the FG, BG Command, and continue execution.
Ctrl-c: The key is the default interrupt key (Interrupt key) under Linux, and when you type CTRL-C, the system sends an interrupt signal to the running program and Shell. The specific response will vary depending on the program. After receiving this signal, some programs will immediately end and eject the program, some programs may ignore the interrupt signal, and some programs will take some other action after receiving this signal. When the shell receives the interrupt signal, it returns to the prompt and waits for the next command.
ctrl-d: This key is EOF for standard input and output below Linux. In devices that use standard input and output, the symbol is considered to be read to the end of the file, so the input or output is ended .

two. Some knowledge of job control under Linux (including the use of jobs, BG, FG, kill commands) 1. Run the Foujian job in the background by using the ' & ' Operation

Special note: The "backstage" we mentioned here refers to a situation in which the CTRL + C interrupt can be avoided in terminal mode, not in the background of the system. So, the background of work management is still related to the terminal, when you use & in the background, the end of the interruption, then the work will be interrupted. If you need more information, find the Nohup or screen command.

   usually in our Linux terminal, when a command is run, the terminal always waits for a particular command or program to run, and then we can continue to run the next command. What if we're going to run a more time-consuming command, but there's something else that needs to be done while the command is running?
you can have a task run in the background by adding a ' & ' operator to the back of the command (note that this is running in the background). (Of course, if you are using the GUI interface, you can also reopen a window to run a new command or program)
For example, we need to copy a larger file, while copying the other things, then add a ' & ' behind the copy command to copy it in the background:
[email protected]:~$ cp/media/bigfile/home/lennon/downloads/&
[1] 3526
[Email protected]:~$
At this point, we can see that in the terminal, after executing a command, give some information, and then return, and then prompt the user to enter the next command, so that the program or command is executed in the background.
In the displayed information, the numbers in square brackets indicate the job number that the system assigns to this job, where ' [1] ' is 1, which is the job's job. And then a larger number, is the system assigned process ID (PID), this PID in the system represents the process.
a job that runs in the background, when it finishes running, and enters a carriage return, gives a hint in the terminal:
[Email protected]:~$ rm Downloads/linux_11gr2_database_1of2.zip &
[1] 3666
[Email protected]:~$
[1]+ done RM downloads/linux_11gr2_database_1of2.zip

2. Use the jobs command to view the job in the current system

If we need to look at the current system and have those jobs, use the ' job ' command:
[email protected]:~$ Jobs
[1]-Running cp/media/bigfile/home/lennon/downloads/&
[2]+ Stopped cat
[Email protected]:~$
When this happens, there are 2 jobs in my system, one is just the running Copy command ([1] identity) and one is the cat command that stops running ([2]). Here ' Running ', ' Stopped ' indicates the status of the task .

Jobs Command Reference
command name: Jobs
usage rights: All permissions
Command Description: Lists the job in the system. Note: Not all shells can use this command
Syntax: Jobs [-P |-l] [-n] [-p] [-X] [job ID]
Parameters:
-P |-l:report The Process group ID and working directory of the jobs.
-n:display only jobs, that has stopped or exited since last notified.
-p:displays The process IDs for the process group leaders of the selected jobs.
-x:replace any job_id found in command or arguments with the corresponding
Process group ID, and then execute command passing it arguments.
job id:the Job ID.

use of 3.Suspend key and BG commands (to run a running job in the background)

If you don't know what the job is doing before running the job, but after the job runs, it's time-consuming and you want to put it in the background so you can do something else while the task is running, and you can do the following:
Use the Suspend key (Suspend key, usually ctrl-z) to suspend the task (that is, to pause) and then use the ' BG ' command to resume execution in the background.
[email protected]:~$ cp bigfile BIGFILE.BAC
^z
[1]+ Stopped cp bigfile Bigfile.bac
[email protected]:~$ BG%1
[1]+ CP bigfile Bigfile.bac &
[Email protected]:~$
after using Ctrl-z, the system will pause the currently running job, move it to the background, give the user a prompt to change tasks (including job number, status, job), and then prompt the user to enter the next command.
after the job has been suspended, you can use the ' bg ' command to get the job back to where it was just interrupted and put it in the background to run. Use ' bg%job number ' to specify which job you need to operate on, and here '% ' tells the system that the numbers behind it are a job # (not '% ' may also be possible). Of course, when there is only one job in the system, you can also ignore the change parameters.

BG Command Reference:
command name: BG
usage rights: All permissions
Command Description: Resumes a stopped job in the background to continue running. Note that the command cannot be run under all Unix shells
syntax: BG [-L] [-p] [-X] [job]
Parameters:
-l:report The process group ID and working directory of the jobs.
-p:report only the process group ID of the jobs.
-x:replace any job_id found in command or arguments with the corresponding process
group ID, and then execute command passing it arguments.
job:specifies The job want to run in the background

4. Use the FG command to change the job in the background to the foreground

use the ' FG%job Number ' command (perhaps not '% ') when you need to switch jobs in the background to the foreground.
[email protected]:~$ cp bigfile Bigfile.bac &
[1] 3815
[Email protected]:~$ FG 1
CP bigfile Bigfile.bac

FG Command Reference:
command name: FG
usage rights: All permissions
Command Description: Moves the background task to the foreground, or resumes the task if it is paused.
Note that this command does not work in all shells.
syntax: FG [%job]
Parameters:
%job:specifies the job, the want to run in the foreground.



5. How to end a job

if you want to end a running job, you can use the break key (interrupt key, usually ctrl-c) to end it.
[email protected]:~$ cp bigfile Bigfile.bac
^c
[Email protected]:~$
if the above method does not work, then you might consider using Ctrl-z (Suspend key) to pause the job, use the ' Jobs ' command to view the job number, and then end the job with the ' kill ' command.
[email protected]:~$ cp bigfile BIGFILE.BAC
^z
[1]+ Stopped cp bigfile Bigfile.bac
[email protected]:~$ Jobs
[1]+ Stopped cp bigfile Bigfile.bac
[email protected]:~$ kill%1
[Email protected]:~$
[1]+ Terminated cp bigfile Bigfile.bac
' % ' in ' kill%1 ' tells the system that the following number is a job #. By default, kill will send a termination signal (-TERM) to the program. If this signal does not work, consider using ' kill-kill%job number ' to send a Kill signal (-kill).
the use of the ' kill ' command, just look at the man, info also can, here do not say, there are too many things. But commonly used also ' kill%job number ', ' Kill-kill%job number ', ' kill [-kill] PID '.

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.