Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A Run pipeline in the background from git hook

I'm trying to run a pipeline to update a PDF after every push to the 'main' branch. I want it to be atomic, so it doesn't touch the existing PDF until it has finished, so I need to use sponge(1) (...

1 answer  ·  posted 6mo ago by alx‭  ·  last activity 6mo ago by alx‭

Question shell-scripting git
#2: Post edited by user avatar alx‭ · 2023-11-24T22:02:29Z (6 months ago)
  • Run pipeline in background from git hook
  • Run pipeline in the background from git hook
#1: Initial revision by user avatar alx‭ · 2023-11-24T16:48:34Z (6 months ago)
Run pipeline in background from git hook
I'm trying to run a pipeline to update a PDF after every push to the 'main' branch.  I want it to be atomic, so it doesn't touch the existing PDF until it has finished, so I need to use sponge(1) (from moreutils).

I've tried the following script:

```sh
#!/bin/sh

test "$1" = "refs/heads/main" || exit 0;

cd ~/tmp/man-pages/;

unset $(git rev-parse --local-env-vars);
git fetch srv			>/dev/null;
git reset srv/main --hard	>/dev/null;

do_work()
{
	scripts/LinuxManBook/build.sh 2>/dev/null \
	| sponge ~/tmp/LMB-HEAD.pdf >/dev/null 2>&1;
}

do_work &
```

If I run the script in a terminal, it works as expected:

```sh
$ time ./hooks/post-update refs/heads/main

real	0m0.015s
user	0m0.007s
sys	0m0.008s
```

And after 15 seconds, the PDF is there.

However, if I do a git push, I must wait for it to finish.  It seems to be ignoring the `&`.

```sh
$ time git push -f tmp 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 24 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.40 KiB | 1.40 MiB/s, done.
Total 5 (delta 3), reused 0 (delta 0), pack-reused 0
remote: From /home/alx/tmp/man
remote:  + 3ac7ec68b...8b824c494 main       -> srv/main  (forced update)
To /home/alx/tmp/man.git/
 + 3ac7ec68b...8b824c494 main -> main (forced update)

real	0m16.051s
user	0m0.025s
sys	0m0.014s
```

Is there anything I'm missing?  If I transform the script to do a simpler redirection, without using a pipe, then it works as expected.  It seems it's the pipe that is making me wait.

```sh
...

scripts/LinuxManBook/build.sh >~/tmp/LMB-HEAD.pdf &  #This works
```