# Background processes in post-receive hook

5 messages from 2011-04-02 to 2011-04-03. Participants: James Pickens, Junio C Hamano, Sitaram Chamarty, Joe Ratterman, Jonathan Nieder.
Thread: https://gitlist.dev/t/26969

## James Pickens, 2011-04-02 23:03

Subject: Background processes in post-receive hook
Message-ID: <AANLkTinrEt6DkwhknpDZ0iV9PR+7bBd3DAaPRJOgNFTG@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTinrEt6DkwhknpDZ0iV9PR%2B7bBd3DAaPRJOgNFTG%40mail.gmail.com

```
Hi,

I have a post-receive hook (bash script) that launches several background
processes and exits immediately.  I do that so I don't have to wait for the
background processes to finish every time I push to the repository, but Git
seems to wait for them to finish anyways, even though the post-receive hook
itself already exited.  Is there any way to stop Git from waiting and let the
push finish immediately?  Sorry if I'm missing something obvious...

Thanks,
James

```

## Junio C Hamano, 2011-04-03 00:35

Subject: Re: Background processes in post-receive hook
Message-ID: <7v8vvsgoj8.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8vvsgoj8.fsf%40alter.siamese.dyndns.org
In-Reply-To: <AANLkTinrEt6DkwhknpDZ0iV9PR+7bBd3DAaPRJOgNFTG@mail.gmail.com>

```
James Pickens <jepicken@gmail.com> writes:

> I have a post-receive hook (bash script) that launches several background
> processes and exits immediately.  I do that so I don't have to wait for the
> background processes to finish every time I push to the repository, but Git
> seems to wait for them to finish anyways, even though the post-receive hook
> itself already exited.  Is there any way to stop Git from waiting and let the
> push finish immediately?

An easy way to do that is to run whatever lengthy process you do not want
to wait for using

	echo "that command" | at now

in the hook ;-).

```

## Sitaram Chamarty, 2011-04-03 03:22

Subject: Re: Background processes in post-receive hook
Message-ID: <BANLkTin4-fMhi_5H1oZ+gPB99stj5GRjVA@mail.gmail.com>
URL: https://gitlist.dev/e/BANLkTin4-fMhi_5H1oZ%2BgPB99stj5GRjVA%40mail.gmail.com
In-Reply-To: <AANLkTinrEt6DkwhknpDZ0iV9PR+7bBd3DAaPRJOgNFTG@mail.gmail.com>

```
On Sun, Apr 3, 2011 at 4:33 AM, James Pickens <jepicken@gmail.com> wrote:
> Hi,
>
> I have a post-receive hook (bash script) that launches several background
> processes and exits immediately.  I do that so I don't have to wait for the
> background processes to finish every time I push to the repository, but Git
> seems to wait for them to finish anyways, even though the post-receive hook
> itself already exited.  Is there any way to stop Git from waiting and let the
> push finish immediately?  Sorry if I'm missing something obvious...

interestingly, the double fork trick doesn't work either...

  ( ( long-running-command & ) )

What Junio said may be the only alternative...

```

## Joe Ratterman, 2011-04-03 04:59

Subject: Re: Background processes in post-receive hook
Message-ID: <AANLkTim3Eu+rHc3_7_PWsALK3QwuzyX2Anbn8o6aCbAE@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTim3Eu%2BrHc3_7_PWsALK3QwuzyX2Anbn8o6aCbAE%40mail.gmail.com
In-Reply-To: <BANLkTin4-fMhi_5H1oZ+gPB99stj5GRjVA@mail.gmail.com>

```
On Sat, Apr 2, 2011 at 10:22 PM, Sitaram Chamarty <sitaramc@gmail.com> wrote:
> interestingly, the double fork trick doesn't work either...
>
>  ( ( long-running-command & ) )

I think that is because the hook is still reading from the command's
stdout.  If you close stdout/stderr, I would expect it to work.  For
example, the "sort" command below cannot exit until all sub-programs
have finished feeding it data.  You can see that despite the levels of
forking involved, the stdout of perl and the stdin of sort are the
same.

$ ( ( perl -e 'sleep 60; print "THERE\n";' & ) ) | sort > /tmp/out &
[1] 4033
$ ps h
...
 4033 pts/10   S      0:00 sort
 4035 pts/10   S      0:00 perl -e sleep 60; print "THERE\n";
...
$ ll /proc/{4035,4033}/fd
/proc/4033/fd:
total 0
lr-x------ 1 jratt bgq 64 Apr  2 23:49 0 -> pipe:[55227211]
l-wx------ 1 jratt bgq 64 Apr  2 23:49 1 -> /tmp/out
lrwx------ 1 jratt bgq 64 Apr  2 23:49 2 -> /dev/pts/10

/proc/4035/fd:
total 0
lr-x------ 1 jratt bgq 64 Apr  2 23:49 0 -> /dev/null
l-wx------ 1 jratt bgq 64 Apr  2 23:49 1 -> pipe:[55227211]
lrwx------ 1 jratt bgq 64 Apr  2 23:49 2 -> /dev/pts/10
$ cat /tmp/out
$ fg
( ( perl -e 'sleep 60; print "THERE\n";' & ) ) | sort > /tmp/out
$ cat /tmp/out
THERE



However, this version exits before the shell returns to the prompt
(and the output is lost).

$ ( ( perl -e 'close STDOUT; close STDERR;sleep 60; print "THERE\n";'
& ) ) | sort > /tmp/out &
[1] 4097
[1]+  Done                    ( ( perl -e 'close STDOUT; close
STDERR;sleep 60; print "THERE\n";' & ) ) | sort > /tmp/out
$



Joe

```

## Jonathan Nieder, 2011-04-03 05:06

Subject: Re: Background processes in post-receive hook
Message-ID: <20110403050619.GA32750@elie>
URL: https://gitlist.dev/e/20110403050619.GA32750%40elie
In-Reply-To: <BANLkTin4-fMhi_5H1oZ+gPB99stj5GRjVA@mail.gmail.com>

```
Sitaram Chamarty wrote:

> interestingly, the double fork trick doesn't work either...
>
>   ( ( long-running-command & ) )

Hopefully closing the output pipe does the trick.

	exec long-running-command >&- 2>&- &

Thanks for an interesting example.
Jonathan

```
