threads / discuss / 26969

Background processes in post-receive hook

Subject: Background processes in post-receive hook

## tl;dr

5 messages between Apr 2, 2011 and Apr 3, 2011.

replies: 4people: 5as markdown or json

James Pickens· Apr 2, 2011, 23:03 UTC · lore
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· Apr 3, 2011, 00:35 UTC · re: James Pickens · lore

Re: Background processes in post-receive hook

James Pickens <jepicken@gmail.com> writes:
Show 6 quoted lines
> 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· Apr 3, 2011, 03:22 UTC · re: James Pickens · lore

Re: Background processes in post-receive hook

On Sun, Apr 3, 2011 at 4:33 AM, James Pickens <jepicken@gmail.com> wrote:
Show 8 quoted lines
> 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· Apr 3, 2011, 04:59 UTC · re: Sitaram Chamarty · lore

Re: Background processes in post-receive hook

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· Apr 3, 2011, 05:06 UTC · re: Sitaram Chamarty · lore

Re: Background processes in post-receive hook

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

← back to recent threads