如何制作" nohup ./script.sh&认"在post-receive git hook中工作?

时间:2016-02-04 22:15:31

标签: git bash shell githooks nohup

我希望用

调用脚本
nohup ./script.sh & disown

将在后台执行,并且在推送时不会看到它的输出。 但我看到了输出,我不得不等待一段时间。以下是被调用脚本的内容:

#!/bin/bash
echo 'test'
sleep 5

如何让它作为我的git hook脚本的分离进程运行? 感谢

更新

我已经明白我不需要nohup ...出于某种原因,它无法在后台运行我的脚本(也可能不同意)。所以我在我的钩子里有以下字符串,它现在正在工作:

./script.sh > /dev/null 2>&1 & disown

感谢@CharlesDuffy向我指出nohup的无用(在这个特例中)。

1 个答案:

答案 0 :(得分:1)

如果您希望脚本自行分离,请考虑:

#!/bin/bash

# ignore HUP signals
trap '' HUP

# redirect stdin, stdout and stderr to/from /dev/null
exec >/dev/null 2>&1 <&1

# run remaining content in a detached subshell
(
  echo 'test'
  sleep 5
) & disown

或者,您可以从父级执行这些操作:

(trap '' HUP; ./yourscript &) >/dev/null <&1 2>&1 & disown