Git post-commit hook as a background task
Asked Answered
L

4

25

I have a script, that I need to run after committing to a project under git revision control. Therefore I created a post-commit hook in my projects .git directory in the subdirectory /hooks, named it 'post-commit' and gave it the following contents:

#!/bin/sh
# I am a post-commit hook
/usr/local/bin/my_script &

my_script is executable and runs fine in /bin/sh. In fact it has a runtime of several seconds, so I want it to be backgrounded and detached from the current shell. That's why I put the trailing '&' to my hook.

The problem now is, that the '&' seems to be ignored. When I commit using gitx 0.7.1 under OSX Lion, gitx hangs for exactly the period that my_script needs to run.

I tried a lot, but do not get the process itself into the background.

What is wrong here?

Lapointe answered 27/10, 2011 at 14:22 Comment(0)
B
18

Here's how it works for me:

#!/bin/sh
# I am a post-commit hook
nohup /usr/local/bin/my_script &>/dev/null &
Bigley answered 15/11, 2011 at 7:24 Comment(2)
David, meanwhile I found a similar approach: nohup /usr/local/bin/my_script 2>&1 > /dev/null &Lapointe
Not exactly the correct answer, but will guide some people: consider implementing a real daemon. If in ruby you can use Daemons or read the basics of what you should be doing in this description of how a daemon works in unix: daemons.rubyforge.org/Daemons.htmlStrawn
O
4

You could also use the at command. You may have to install it first

echo /path/to/your/executable | at now

OR:

echo bash /path/to/your/script | at now

See the at(1) manual page for more info about at (man at or the online version)

Odontoblast answered 25/9, 2015 at 14:27 Comment(0)
V
2

Try to use nohup

#!/bin/sh
# I am a post-commit hook
nohup /usr/local/bin/my_script &
Vermont answered 27/10, 2011 at 14:25 Comment(1)
Hey WagnerVaz, tried that, but unfortunately it does not work. The script seems not to detach from the current process and gitx is stale as long as the script runs.Lapointe
M
1

If you change #!/bin/sh to #!/bin/bash (assuming you're ok with using bash), and use nohup, your example should work.

Mikkanen answered 7/5, 2015 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.