Linux Task Control

Source: Internet
Author: User

Linux/unix offers different multitasking with windows and a set of commands to switch back and forth tasks

BG FG & Ctrl+z nohup sitsid

  1. Ctrl-z Suspending Program
    [email protected]:~$./test.sh^z[1]+  Stopped                 ./Test.sh[email protected]: ~$

    When the job is running, execution ctrl-z suspends the job and returns information such as the job's JobID, similar to the information returned with the jobs command

  2. BG Background Execution
     [Email protected]:~$ jobs[ 1 ]  + Stopped./test.sh, [email protected]:  ~$ bg 1  [1]+./test.sh &  [email  Protected]:  ~$ 

    BG runs a pending job as a background job to re-execute these jobs in the current environment, and if the specified job is already running in the background, the BG command does not work and exits successfully. If the JobID parameter is not provided, the BG command uses the most recently suspended job

  3. FG Foreground Run job
     [Email protected]:~$ jobs[ 1 ]  + Stopped./test.sh, [email protected]:  ~$ FG 1   test.sh  [email protected]:  ~$ 

    FG uses the JobID parameter to indicate the specific job to run under the foreground. If you provide a JOBID,FG command to use a job that has been suspended in the background recently

  4. & Put the job in the background to execute
    [Email protected]:~$./test.sh &[133511[email protected]:~$

    When you run a job in the foreground, the terminal is occupied by the job, and you can use the & command to place the job in the background, and when the job runs in the background, it does not occupy the terminal. In fact, this is where the command is put into a job queue. Jobs running in the background will output the results to the screen, interfering with your work. If a job that runs in the background produces a lot of output, it's a good idea to redirect its output to a file using the following method:command >out.file 2>&1 &

  5. Nohup ignoring hang-up signals
    2>&1 &[133537[email protected]:~$ jobs

    when you execute a job , you can perform a background job with parentheses () and do something like nuhup, so that the job still works when the current Shell is closed. Principle with Setsid and disown

    [Email protected]:~$ (./test.sh &) [email protected]:~$ Ps-ef |grep Test515        410     1 0  One: the?xx:xx:xx/bin/sh./test.sh515      12483 21734 0  One: -pts/ A   xx:xx:xxgrep test

    Use & | When BG runs a background command, its parent process is still the process of the current terminal shell, and once the parent process exits, the hangup signal is sent to all child processes, and the child process exits after receiving hangup. If we are going to continue running the process while exiting the shell, we need to use nohup to ignore the hangup signal, or use SETSID to set the parent process to the INIT process ( process number 1), or you can use the Disown command

    1. Setsid
      [Email protected]:~$ ps-ef |grep Test515      29710 21734 0  One: -pts/ A   xx:xx:xx/bin/sh./test.sh515      29713 21734 0  One: -pts/ A   xx:xx:xxgrep test[email protected]:~$ setsid./test.sh &[1]409[email protected]:~$ Ps-ef |grep Test515        410     1 0  One: the?xx:xx:xx/bin/sh./test.sh515        413 21734 0  One: thepts/ A   xx:xx:xxgrep test

      For a foreground job that has already been performed, SETSID can set the parent process of the current job to 1, but for commands that are already running in the background we need to use the disown command

    2. Disown
      [Email protected]:~$./test.sh &[1]2539[email protected]:~$ Jobs-l[1]+2539Running./test.sh &[email protected]:~$ disown-h%1[email protected]:~$ Ps-ef |grep Test515        410    1 0  One: the?xx:xx:xx/bin/sh./test.sh515       2542 21734 0  One: thepts/ A   xx:xx:xxgrep test

      For background jobs that have been performed, disown can set the parent process of the current job to 1

Linux Task Control

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.