Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git post-commit hook as a background task

Tags:

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?

like image 582
GeorgieF Avatar asked Oct 27 '11 14:10

GeorgieF


People also ask

What is post-commit hook in git?

The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.

Can you commit pre-commit hooks?

pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.


2 Answers

Here's how it works for me:

#!/bin/sh # I am a post-commit hook nohup /usr/local/bin/my_script &>/dev/null & 
like image 170
David Avatar answered Sep 23 '22 13:09

David


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)

like image 29
Kaiden Prince Avatar answered Sep 19 '22 13:09

Kaiden Prince