37

(OSX 10.7) An application we use let us assign scripts to be called when certain activities occur within the application. I have assigned a bash script and it's being called, the problem is that what I need to do is to execute a few commands, wait 30 seconds, and then execute some more commands. If I have my bash script do a "sleep 30" the entire application freezes for that 30 seconds while waiting for my script to finish.

I tried putting the 30 second wait (and the second set of commands) into a separate script and calling "./secondScript &" but the application still sits there for 30 seconds doing nothing. I assume the application is waiting for the script and all child processes to terminate.

I've tried these variations for calling the second script from within the main script, they all have the same problem:

  • nohup ./secondScript &
  • ( ( ./secondScript & ) & )
  • ( ./secondScript & )
  • nohup script -q /dev/null secondScript &

I do not have the ability to change the application and tell it to launch my script and not wait for it to complete.

How can I launch a process (I would prefer the process to be in a scripting language) such that the new process is not a child of the current process?

Thanks, Chris

p.s. I tried the "disown" command and it didn't help either. My main script looks like this:

[initial commands]
echo Launching second script
./secondScript &
echo Looking for jobs
jobs
echo Sleeping for 1 second
sleep 1
echo Calling disown
disown
echo Looking again for jobs
jobs
echo Main script complete

and what I get for output is this:

Launching second script
Looking for jobs
[1]+ Running ./secondScript &
Sleeping for 1 second
Calling disown
Looking again for jobs
Main script complete

and at this point the calling application sits there for 45 seconds, waiting for secondScript to finish.

p.p.s

If, at the top of the main script, I execute "ps" the only thing it returns is the process ID of the interactive bash session I have open in a separate terminal window.

The value of $SHELL is /bin/bash

If I execute "ps -p $$" it correctly tells me

PID   TTY TIME    CMD
26884 ??  0:00.00 mainScript

If I execute "lsof -p $$" it gives me all kinds of results (I didn't paste all the columns here assuming they aren't relevant):

FD   TYPE   NAME
cwd  DIR    /private/tmp/blahblahblah
txt  REG    /bin/bash
txt  REG    /usr/lib/dyld
txt  REG    /private/var/db/dyld/dyld_shared_cache_x86_64
0    PIPE   
1    PIPE   -> 0xffff8041ea2d10
2    PIPE   -> 0xffff 8017d21cb
3r   DIR    /private/tmp/blahblah
4r   REG    /Volumes/DATA/blahblah
255r REG    /Volumes/DATA/blahblah
5
  • What is the parent of mainScript ?
    – thom
    Dec 2, 2013 at 22:53
  • mainScript is being launched from the unnamed application (CVS). You can tell CVS "launch my script when a user commits a file". Dec 2, 2013 at 22:57
  • In that case, your script is not running on a bash shell directly and the application is doing the shell duties, that explains a lot. You will have to resort to start the slow process outside the processtree of the application.
    – thom
    Dec 2, 2013 at 23:15
  • I assumed CVS is just doing a system("/bin/bash mainScript.sh"), what "shell duties" would CVS even be able to assume? Dec 2, 2013 at 23:46
  • If CVS is just doing (/bin/bash mainScript) you would in pstree -p see the launched bash shell as parent of mainScript and CVS as parent of that bash shell. If CVS is directly parent of mainScript, there is no bash shell involved. What CVS would do is 'mimicking' bash in a very spartan way to intercept every system call.
    – thom
    Dec 2, 2013 at 23:58

5 Answers 5

40

The typical way of doing this in Unix is to double fork. In bash, you can do this with

( sleep 30 & )

(..) creates a child process, and & creates a grandchild process. When the child process dies, the grandchild process is inherited by init.


If this doesn't work, then your application is not waiting for child processes.

Other things it may be waiting for include the session and open lock files:

To create a new session, Linux has a setsid. On OS X, you might be able to do it through script, which incidentally also creates a new session:

# Linux:
setsid sleep 30

# OS X:
nohup script -q -c 'sleep 30' /dev/null &

To find a list of inherited file descriptors, you can use lsof -p yourpid, which will output something like:

sleep   22479 user    0u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    1u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    2u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    5w   REG  252,0        0  1048806 /tmp/lockfile

In this case, in addition to the standard FDs 0, 1 and 2, you also have a fd 5 open with a lock file that the parent can be waiting for.

To close fd 5, you can use exec 5>&-. If you think the lock file might be stdin/stdout/stderr themselves, you can use nohup to redirect them to something else.

1
  • Sorry, my update and your response crossed paths ... I tried adding the line ( ( script & ) & ) and had the same result. Dec 2, 2013 at 21:49
17

Another way is to abandon the child

#!/bin/bash

yourprocess &

disown

As far as I understand, the application replaces the normal bash shell because it is still waiting for a process to finish even if init should have taken care of this child process. It could be that the "application" intercepts the orphan handling which is normally done by init.

In that case, only a parallel process with some IPC can offer a solution (see my other answer)

9
  • Alas, OSX doesn't have a "disown" command. [Sorry, my bad, disown is a bash command and there's just no man page for it. But I'm not yet able to make it work since I can't find documentation for it] Dec 2, 2013 at 21:54
  • @BettyCrokker: info bash and search for "disown", or look here. Dec 2, 2013 at 22:01
  • Cool, thanks! So the next problem ... I assume thorn's answer is mostly pseudocode, I can't actually call disown without any options. It looks like I need to pass it the job ID, and I'm off searching the web to figure out how I get the job ID ... Dec 2, 2013 at 22:03
  • NO, it is not pseudocode. You don't need a job ID. disown just eradicates all it's children from it's local processtable. Ofcourse you have to replace yourprocess with the name of your process.
    – thom
    Dec 2, 2013 at 22:09
  • When I say "I can't actually call disown without any options" what I mean is "when I call disown without any options it gives me an error message "disown: current: no such job" Dec 2, 2013 at 22:11
12

I think it depends on how your parent process tries to detect if your child process has been finished. In my case (my parent process was gnu make), I succeed by closing stdout and stderr (slightly based on the answer of that other guy) like this:

sleep 30 >&- 2>&- &

You might also close stdin

sleep 30 <&- >&- 2>&- &

or additionally disown your child process (not for Mac)

sleep 30 <&- >&- 2>&- & disown

Currently tested only in bash on kubuntu 14.04 and Mac OSX.

0
2

If all else fails:

  1. Create a named pipe

  2. start the "slow" script independent from the "application", make sure executes it's task in an endless loop, starting with reading from the pipe. It will become read-blocked when it tries to read..

  3. from the application, start your other script. When it needs to invoke the "slow" script, just write some data to the pipe. The slow script will start independently so your script won't wait for the "slow" script to finish.

So, to answer the question:
bash - how can I launch a new process that is NOT a child of the original process?

Simple: don't launch it but let an independent entity launch it during boot...like init or on the fly with the command at or batch

2
  • very good thx! only this works for me. learned about mkfifo cool :). no matter what I try, from any answers I find, disown and nohup wont work with chromium and google-chrome, may be cairo-dock too. Oct 13, 2018 at 1:51
  • also this worked too (someCommand&disown)&disown but I still dont understand why (beyond the obvious), anyway I mixed it with the listener you suggested :) Oct 27, 2018 at 19:56
2

Here I have a shell

└─bash(13882)

Where I start a process like this:

$ (urxvt -e ssh somehost&)

I get a process tree (this output snipped from pstree -p):

├─urxvt(14181)───ssh(14182)

where the process is parented beneath pid 1 (systemd in my case).

However, had I instead done this (note where the & is) :

$ (urxvt -e ssh somehost)&

then the process would be a child of the shell:

└─bash(13882)───urxvt(14181)───ssh(14182)

In both cases the shell prompt is immediately returned and I can exit without terminating the process tree that I started above.

For the latter case the process tree is reparented beneath pid 1 when the shell exits, so it ends up the same as the first example.

├─urxvt(14181)───ssh(14182)

Either way, the result is a process tree that outlives the shell. The only difference is the initial parenting of that process tree.

For reference, you can also use

  • nohup urxvt -e ssh somehost &
  • urxvt -e ssh somehost & disown $!

Both give the same process tree as the second example above.

└─bash(13882)───urxvt(14181)───ssh(14182)

When the shell is terminated the process tree is, like before, reparented to pid 1.

nohup additionally redirects the process' standard output to a file nohup.out so, if that is a useful trait, it may be a more useful choice.

Otherwise, with the first form above, you immediately have a completely detached process tree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.