Run pipeline in the background from git hook
+2
−0
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:
#!/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:
$ 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 &
.
$ 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.
...
scripts/LinuxManBook/build.sh >~/tmp/LMB-HEAD.pdf & #This works
1 answer
+0
−0
Works for me
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
alx | (no comment) | Nov 26, 2023 at 09:52 |
Use nohup(1), and redirect stdout and stderr to /dev/null
The following works as expected, not having to wait until the PDF is generated.
#!/bin/sh
test "$1" = "refs/heads/main" || exit 0;
cd /srv/src/alx/linux/man-pages/man-pages/;
unset $(git rev-parse --local-env-vars);
git fetch srv >/dev/null;
git reset srv/main --hard >/dev/null;
nohup sh <<__EOF__ >/dev/null 2>&1 &
scripts/LinuxManBook/build.sh 2>/dev/null \
| sponge /srv/www/share/dist/man-pages/git/HEAD/man-pages-HEAD.pdf;
__EOF__
0 comment threads