Shell background process FG BG wait etc usage

Source: Internet
Author: User
Tags auth diff

[[email protected] shelltest]# ./forever2.sh 2014  10 month  15 Day   Wednesday   03:36:24 cst2014  10 Month  15   Wednesday  03:36:26 CST^Z     (---NOTE: Ctrl + Z) [1]+  stopped                  ./FOREVER2.SH[[EMAIL PROTECTED] SHELLTEST]# FG %1./FOREVER2.SH2014 year  10 month  15 Day   Wednesday  03:36:32 cst2014  10 month  15 day   Wednesday  03:36:34 cst^c[[ email protected] shelltest]# ps -ef|grep forroot       1807  1773  0 03:04 ?        00:00:00  /usr/libexec/gdm-simple-slave --display-id /org/gnome/displaymanager/display1 -- Force-active-vtroot      1810  1807  0 03:04 tty1      00:00:08 /usr/bin/xorg :0 -nr -verbose -audit 4 -auth /var/run/gdm/ auth-for-gdm-9xeab2/database -nolisten tcp vt1root      1990      1  0 03:06 ?         00:00:00 /bin/dbus-daemon --fork --print-pid 5 --print-address 7 -- sessionroot      4309  4188  0 03:36 pts/4     00:00:00 grep for[[email protected] shelltest]# more  forever2.sh #!/bin/bashwhile :d o date sleep 2doneecho  "while  End------------------------"echo " Hou Run process ID Number: $! " echo  "script pre-run process ID number $$" [[email protected] shelltest]#

----

Wake him up in the background

[[email protected] shelltest]# ./forever2.sh 2014  10 month  15 Day   Wednesday   03:38:59 cst2014  10 month  15 day   Wednesday  03:39:01 CST^Z[1]+  Stopped                  ./forever2.sh[[email  protected] shelltest]# bg %1[1]+ ./forever2.sh &[[email protected]  shelltest]# 2014  10 month  15 day   Wednesday  03:39:07 cst2014 year  10 month  15 Day   Wednesday  03:39:09 cst2014  10 month  15 day   Wednesday  03:39:11 cst2014 year  10 month  15 Day   Wednesday  03:39:13 cst2014  10 month  15 day   Wednesday  03:39:15 cst2014 year  10 month  15 Day   Wednesday  03:39:17 cst^c[[email protected] shelltest]# 2014  10 month  15 Day   Wednesday   03:39:19 cst2014  10 month  15 day   Wednesday  03:39:21 cst2014 year  10 month  15 Day   Wednesday   03:39:23 cst^c[[email protected] shelltest]# 2014  10 month  15 day   Wednesday  03:39:25 cstlogout 

BG after he always print in front desk, CTRL + C kill him, had to ctrl+d

But he'll still be there.

[[email protected] shelltest]# ps-ef|grep foreverroot 4401 1 0 03:42? 00:00:00/bin/bash./forever2.shroot 4474 4444 0 03:43 pts/4 00:00:00 grep forever[[email protected] shelltest]#

Kill-9 4401 kill him.

Summarize the processes running in the background CTRL + C kills not dead

-----------------

Test Script test.sh:

I=1

While

Do

Echo$i

Sleep1

((i++))

Done

First, when running a job in the foreground, the terminal will be occupied by the job, and thus need to open another terminal to do other operations, in order to avoid this inconvenience we can put the job in the background to execute, there are two main ways


1. & Command


shtest.sh&


This command puts the script in the background, but the standard output is still displayed to the current terminal, affecting user actions, so it is better to redirect the output to another file


Shtest.sh&>/dev/null


If you need to see the output, you can also direct it to a fixed file.


2, through a series of commands such as CTRL+Z;BG, the operation has been run in the foreground to the background execution


If a job is already executing in the foreground, you can put the job in the background and hang it by Ctrl + Z. Then, through the Jobs command, view the jobs executed in the background and find the corresponding job ID, and execute bg%n (n for the job ID that was found by jobs) to continue execution.


There is also the case that the result is output to the terminal, and the same can be solved by the redirection method


Related commands:


Jobs------------View processes executed in the background


fg%n----------the background execution process n to the foreground, n means Jobnumber (the process number viewed by jobs, not PID)


CTRL + Z----------will be executed in the foreground process, put in the background and hang


bg%n---------will suspend the process in the background, continue to execute


CTRL + C----------The foreground process terminates


kill%n---------Kill the process running in the background, n means Jobnumber (the process number viewed by jobs, not PID)




Second, when the user logoff or network interruption, the terminal received SIGHUP signal, thereby closing all its sub-processes, the above two ways will be closed with the terminal exit, if we need to work in the background is not affected by the terminal exit, you can use the following two ways


1. Nohup command


nohupshtest.sh&>/dev/null&


The Nohup command ignores the sighup signal, so the terminal exits without affecting the background job


2. Hang the job under a new session


(shtest.sh&>/dev/null&) or put shtest.sh&>/dev/null& in another script can be implemented


& also put in (), we will find that the submitted job is not in the job list, that is, can not be viewed through jobs, through PS view found that the new job ppid is 1 instead of the terminal PID, so the terminal will not affect our job after exiting




Third, the screen command can also achieve the corresponding function, and can solve the problem of human-computer Interaction program needs




What if the program is like this?

[Email protected] shelltest]# more forever.sh #!/bin/bashwhile:d o date sleep 2done&echo ' while end----------------- -------"echo" The process ID of the last background process is \$!:$! " echo "script is running the current process ID number \$$:$$" [[email protected] shelltest]#

Run:

[[email protected] shelltest]# ./forever.sh while  End------------------------The process ID of the last background process is the current process ID number run by the $!:4959 script $$:4958[[email protected] shelltest]#  2014  10 month  15 day   Wednesday  04:07:06 cst2014 year  10 month  15 Day   Wednesday  04:07:08  cst2014  10 month  15 day   Wednesday  04:07:10 cst2014 year  10 month  15 Day   Wednesday  04:07:12  cst2014  10 month  15 day   Wednesday  04:07:14 cst^c[[email protected] shelltest]#  2014  10 month  15 day   Wednesday  04:07:16 CST^C[[email protected] shelltest]#  2014  10 Month  15 Day   Wednesday  04:07:18 cst2014  10 month  15 day   Wednesday  04:07:20  CST2014  10 month  15 day   Wednesday  04:07:22 cst2014 year  10 month  15 Day   Wednesday  04:07:24  CST2014  10 month  15 day   Wednesday  04:07:26 cst2014 year  10 month  15 Day   Wednesday  04:07:28  CST2014  10 month  15 day   Wednesday  04:07:30 cst2014 years  10 month  15   Wednesday  04:07:32 cst2014  10 month  15 day   Wednesday  04:07:34 cst^ c[[email protected] shelltest]#

Ctrl + C is not dead

[[email protected] ~]# ps -ef|grep forroot      1807   1773  0 03:04 ?        00:00:00 / usr/libexec/gdm-simple-slave --display-id /org/gnome/displaymanager/display1 -- Force-active-vtroot      1810  1807  0 03:04 tty1      00:00:12 /usr/bin/xorg :0 -nr -verbose -audit 4  -auth /var/run/gdm/auth-for-gdm-9xEab2/database -nolisten tcp vt1root       1990     1  0 03:06 ?         00:00:00 /bin/dbus-daemon --fork --print-pid 5 -- Print-address 7 --sessionroot      4959     1   0 04:07 pts/2    00:00:00 /bin/bash ./forever.shroot      4979   4145  0 04:07 pts/3    00:00:00 grep for[[email  protected] ~]# kill -9  4959[[email protected] ~]#

Note that 4959 is the background process ID, not the process ID of the shell script

Reference:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4C/7D/wKioL1Q-ZqSzKhKTAAGZZbiGodI350.jpg "title=" Spec.png "alt=" Wkiol1q-zqszkhktaagzzbigodi350.jpg "/>


-------

Wait command

wait[process or job]: sets the waiting process or job identity-----$ wait%1 #等待作业号为3的作业完成

Cat Test1 | Uniq > Newtest1 &

Cat Test2 | Uniq > Newtest2 &

Wait

Diff Newtest1 Newtest2


In order to compare the differences between Newtest1 and Newtest2, you must first make the above two cat commands successful and complete and generate newtest1 and Newtest2, otherwise the diff execution will be wrong ... Wait is to ensure that the above command executes before the diff command is executed .... The diff command is not executed until the above command is completed.

SH aa.sh&p0=$!wait $p 0sh bb.sh & sh cc.sh &

Has been plagued by a problem: in shell programming, when a task is completed, the next two tasks can be run simultaneously, both of which do not affect each other. So when the first task is finished, you start the next two tasks, and I don't know how to start at the same time in the shell.


Solved today: Use the background to run &.

It's better to give an example.

There is a total execution of the all.sh, with three task shells, namely aa.sh, bb.sh,cc.sh.

When you run play aa.sh, run bb.sh,cc.sh at the same time.


This article is from the "operations to ETL" blog, please be sure to keep this source http://fuwenchao.blog.51cto.com/6008712/1564570

Shell background process FG BG wait etc usage

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.