Thread: background jobs running inside a bash script

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    background jobs running inside a bash script

    I have a bash script which calls several jobs in background.

    If I run the background jobs by running the bash script in the shell, I cannot control the jobs in the shell directly. In this case, how to?

    If I paste the content of the script into the shell and run it, then I can use the job control commands to play with the background jobs in the content.

    Thanks and regards!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What do you mean by "I cannot control the jobs" What type of control do you want? If you want a simple SIG_KILL, SIG_TERM, SIG_HUP, etc, that is VERY easy to do:

    Code:
    MY_JOB="zipzap -LONGLIST OF -Options"
    eval ${MY_JOB} &
    MY_JOB_PID=`ps ax | grep -e "${MY_JOB}" | grep -v grep | awk '{print $1}'`
    kill -<whatever> $MY_JOB_PID
    ****ASSUMPTION: grep is being called without the -n option, else the print $1 would need to be print $2 (I think).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Like suspend the jobs by C-z, get the info of the jobs by "jobs", ...

    If you put your code for background jobs into a bash script, and run it in a shell, can you still use those commands on the background jobs in the same shell?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lehe View Post
    If I run the background jobs by running the bash script in the shell, I cannot control the jobs in the shell directly. In this case, how to?
    Do you mean inside the script? It's the same thing.

    %+ will refer to the last running job sent to the background, so for example if you want to display an image but put the "display" process in the background so you can get some user input while the picture is being displayed, then kill the display process so the image viewer closes, then print the input:
    Code:
    display "rorshache.jpg" &
    read something
    kill %+
    echo $something
    I use this technique when questioning hostages

    There's other shortforms like %+ to save you having to get pids. They're listed at the bottom of this page:
    Job Control Commands

    Quote Originally Posted by lehe View Post
    If you put your code for background jobs into a bash script, and run it in a shell, can you still use those commands on the background jobs in the same shell?
    I believe so -- try it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thank you. But my question was not what you said. An example will explain it better, I think.

    Say you have a background job
    Code:
    display "rorshache.jpg" &
    You wrote it into a bash script called test.sh. Now run it by
    Code:
    bash test.sh
    Now if you want to do some job management, for example, to see what job is running background
    Code:
    jobs
    The job will not be shown.

    If you run
    Code:
    display "rorshache.jpg" &
    directly in the shell, you will be able to use job control commands.

    My question is in the first case how to do the job control?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lehe View Post
    My question is in the first case how to do the job control?
    You can get the process info for every instance of "display" running this way:

    ps -C display

    if all you want is the pid:

    ps -C display -o pid=

    You could make that easy on yourself by outputting the pid from the script:

    Code:
    echo $$ 
    display "some.jpg" &
    echo $!
    $$ is the pid of the script itself. $! is the pid of the last job.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by MK27 View Post
    ps -C display -o pid=
    Please note that the above is NOT portable.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    "Jobs" are a shell concept. Once the shell script terminates, the jobs no longer exist -- they are just processes at that point.

    To get actual job control, you cannot invoke the script in a subshell. Instead, you should evaluate it directly in the currently running shell. Instead of running:

    Code:
    $ ./myScript
    Do this:

    Code:
    $ . ./myScript
    That's a dot followed by a space. This causes the currently running shell to interpret the script, instead of launching a subshell. The jobs will then be accessible from the standard job control
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running hidden executables in background
    By Fender Bender in forum Windows Programming
    Replies: 15
    Last Post: 04-17-2005, 10:59 AM
  2. Running A Routine in the background
    By smegly in forum C Programming
    Replies: 4
    Last Post: 05-16-2004, 06:42 PM
  3. running program in the background
    By sunil21 in forum C Programming
    Replies: 2
    Last Post: 09-07-2003, 05:57 PM
  4. I'm stupid, my winapp still running in background
    By Shadow12345 in forum Windows Programming
    Replies: 3
    Last Post: 01-14-2003, 09:22 PM
  5. Running program on background?
    By Couhilin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2001, 07:50 AM