Git post-commit hook as a background task

不问归期 提交于 2019-11-28 06:58:41

问题


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?


回答1:


Here's how it works for me:

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



回答2:


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)




回答3:


Try to use nohup

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



回答4:


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



来源:https://stackoverflow.com/questions/7917324/git-post-commit-hook-as-a-background-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!