Work control for process management

Work control for process management

1 What is job control?

  We know that linux is a multi-task and multi-terminal operating system. We can work on multiple terminals or on multiple tasks at one terminal. It is called work management that multiple tasks are carried out simultaneously in a terminal. For example, at a terminal, you want to copy files, at the same time you want to compress packaged files, and even you want to edit files, at this time you need to use work management. This is probably the case with job management. Immediately above.

  First of all, you need to understand the concepts of the front desk and the back desk. The front desk is the terminal of our current landing shell operation. We interact with it, see and touch the terminal. Background is an operating environment that does not interact with users. We can put the executed commands into the background without manual management, and can accomplish the task automatically.

  Forebackground: Put the working process into the terminal running which interacts with the user.
  Running: Placing a work process in a background that no longer interacts with users.
  Background pause: The suspension state of the work process.
  Done: Work done.
  Terminated: The work process is terminated.


2. How to manage work

        2.1 Put the instructions directly in the background to execute:&

[root@big backup]# tar -zpcf /apps/backup/etc.tar.gz &>/apps/backup/backup.log /etc &
[1] 6798                    #Step 1: Package and compress / etc directories
                            #Step 2: Write the standard output stream to a file
                            #Step 3: & Put your work in the background     
[root@big backup]# jobs     #job: Check the working status is Running, that is, running in the background.                   
[1]+  Running                 tar -zpcf /apps/backup/etc.tar.gz /etc &>/apps/backup/backup.log &
[root@big backup]# jobs -l  #Jo-l. You can also view the PID of the work process 
[1]+  6798 Running                 tar -zpcf /apps/backup/etc.tar.gz /etc &>/apps/backup/backup.log &
[root@big backup]# jobs -l  #Look again and find the working state Done, that is, the end state, the work is completed.
[1]+  6798 Done                    tar -zpcf /apps/backup/etc.tar.gz /etc &>/apps/backup/backup.log

//Analysis:
        & Place background commands
        jobs -l You can see four things: work number PID working condition Work order
        [1]+     Work number,+Number denotes the first job
        6798     process IP(pid)
        Done     Completion of the work process
        tar...   Work order


2.2 Put the work done in the front desk into Stopped: (command+(ctrl+z))

[root@big backup]# tar -zpcf /tar -zpcf /apps/backup/etc.tar.gz  /etc         #Packing Compression Command
tar: Removing leading `/' from member names
^Z                                                #ctrl+z puts the foreground work process into a pause state
[1]+  Stopped                 tar -zpcf /apps/backup/etc.tar.gz /etc
[root@big backup]# jobs -l                        #View Work Information
[1]+  7214 Stopped                 tar -zpcf /apps/backup/etc.tar.gz /et    #Stopped means background pause


2.3 Put the background work into the foreground operation:fg

[root@big backup]# jobs -l                        #View Work Information
[1]+  7214 Stopped                 tar -zpcf /apps/backup/etc.tar.gz /et
[root@big backup]# fg %1            #Put the work in the front desk
tar -zpcf /apps/backup/etc.tar.gz /etc

2.4 Put Stopped in Running: bg

[root@big backup]# bg %1;jobs -l        #Put the work number 1 in the background and view the work information
[1]+ tar -zpcf /apps/backup/etc.tar.gz /etc &
[1]+  7434 Running                 tar -zpcf /apps/backup/etc.tar.gz /etc &
[root@big backup]# jobs -l              #Look at the working state again: Done, that's done.
[1]+  7434 Done                    tar -zpcf /apps/backup/etc.tar.gz /etc

2.5 kill Command Management

  Many people often regard killing as a command of killing process. In fact, it is wrong to understand that. The killing command is far more effective than that. Kill is more like a command to send a signal that tells the process what to do next. We can look at the kill-l support signal. Here I will introduce only four kinds of signals related to job management.

[root@big backup]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

//Analysis: Four Common Signals and Examples of Their Use
     9) SIGKILL Mandatory killing of running processes
    15) SIGTERM Normally terminate a running process
    18) SIGCON  Continue to run, equivalent to bg
    19) SIGSTOP Background pause, enter backstage pause(Stopped)
    
//Example:
    [root@big apps]#  tar -zcf /apps/tmp.tar.gz / &>/apps/tmp.log &    #Put the package compression process in the background
    [1] 8482
    [root@big apps]# jobs -l                                           #View Work Process Information
    [1]+  8482 Running                 tar -zcf /apps/tmp.tar.gz / &
    [root@big apps]# kill -19 %1                                       #Put the first job in the background and pause
    [1]+  Stopped                 tar -zcf /apps/tmp.tar.gz /
    [root@big apps]# jobs -l                                           #View Work Process Information
    [1]+  8482 Stopped (signal)        tar -zcf /apps/tmp.tar.gz / 
    [root@big apps]# kill -18 %1;jobs -l                               #Put the first job in the background
    [1]+  8482 Running                 tar -zcf /apps/tmp.tar.gz / &
    [root@big apps]# jobs -l
    [1]+  8482 Running                 tar -zcf /apps/tmp.tar.gz / &
    [root@big apps]# kill -15 %1                                       #Normal termination of work
    [root@big apps]# jobs -l                                           #Working status Teminated, terminated.
    [1]+  8482 Terminated              tar -zcf /apps/tmp.tar.gz /
    [root@big apps]# jobs -l                                           #Look back at the background again and see that there is no working process

//Analysis:
    Through the analysis of the above examples, we can know that( kill -19 )Work processes running in the background can be put into the background to pause
    Through the analysis of the above examples, we can know that( kill -18 )Work processes suspended in the background can be put into the background to run
    Through the analysis of the above examples, we can know that( kill -15 )The working process running in the background can be terminated normally.

2.6 Offline management process: nohup

  The aforementioned work management depends on the terminal. Once the terminal is shut down, the running or background suspended work will be terminated. Is there any way to get the commands we execute out of the limitation of the terminal, that is, when I close the current terminal, the original commands will continue to run. The answer is: Yes, of course, that is the nohup command for Li.

Format: nohup command&


terminal pts/0
[root@big apps]# nohup ping 10.0.0.1 &>/dev/null &            #Executing ping commands using nohup offline mode
[1] 8910
[root@big apps]# jobs -l                                      #View the Backstage Work Status
[1]+  8910 Running                 nohup ping 10.0.0.1 &>/dev/null &
[root@big apps]# exit                                         #Exit the current terminal
logout

Connection closed by foreign host.

Disconnected from remote host( 7.3 10.0.0.73) at 14:23:18.

//Terminal pts/1
[root@big ~]# ps aux | grep ping                            #Another terminal searches for ping processes
root       8910  0.0  0.1 127912  1228 ?        S    14:22   0:00 ping 10.0.0.1
root       8932  0.0  0.0 112648   960 pts/1    S+   14:23   0:00 grep --color=auto ping
[root@big ~]# kill 8910                                     #Normal termination of ping process
[root@big ~]# ps aux | grep ping                            #Check whether the process has been terminated
root       8973  0.0  0.0 112648   960 pts/1    S+   14:25   0:00 grep --color=auto ping

//Analysis:
    pts/0 Use nohup command & Executing commands in offline mode
    At another terminal pts/1 See ping Work process, discovery ping The working process is running, which proves that nohup Executing commands can be detached from the terminal
    Normal termination ping Process, verify termination
    That's the end of the experiment.

3. Summary

      Okay, now let's review the commands used in work management through the following figure.

Author: Michael Hu
 Time: 2017/05/14


Keywords: Linux shell

Added by marconi8 on Mon, 01 Jul 2019 01:47:11 +0300