git checkout does not work when backgrounded
I am trying to get a ssh trigger that will do something with git repository. When I run the code on background, it works. Adding a nohup does not have effect. The relevant bit just says:
git checkout master Already on 'master'
But when I finally background it, it stops working with:
git checkout master fatal: unable to write new index file
(output is directed to a log file; that is done on foreground as well; the log file includes the command being executed).
I've printed the id and umask at the beginning of the script and they look the same. And in both cases it's executed by ssh trigger, the only difference is & after the command.
The script looks like this:
#!/bin/sh export GIT_DIR=/somewhere nohup git-using-script.sh </dev/null >/dev/null 2>&1 & sleep 1
Without the sleep 1, the nohup does not start (the redirections happen before the nohup starts, ssh manages to exit before the nohup creates a new session and the command is terminated anyway). But without the & everything else works, with it the script runs, but git produces the error.
The only difference I could think of is that with the ssh already down there is no tty and git is trying to access it for some reason or another. So I worked it around by using script(1). The resulting invocation is somewhat insane, but it works:
nohup script -qec "/path/to/git/script.sh args" /dev/null \ </dev/null >/dev/null 2>&1 &
I suspect the attempt to get the controlling terminal (it was not connected to any stream even before) has something to do with output paging, though I don't see why git checkout would need that. Nor why it should fail the operation.