Open cescp opened 7 years ago
Off the top of my head, try closing stdin, stdout and stderr in the child process, probably something like:
exec 0<&-
exec 1>&-
exec 2>&-
Of course this means you won't be able to produce any more content from the background script but you don't really have a place to send it to anyway (you might get away with closing stdout only and keep logging to stderr but you'd have to check it)
Your solution worked! As you suggested:
#!/bin/bash
exec 0<&-
exec 1>&-
exec 2>&-
echo "Starting child process..."
sleep 10
echo "Child process done."
exit 0
makes the child to be detached. Specifying redirection in do-something.sh, instead of do-process.sh, also worked:
#!/bin/bash
echo "Status: 202"
echo -e "Content-type: text/plain\n"
echo "Calling child process..."
/usr/local/bin/do-process.sh 1>&- 2>&- &
echo "Returned from child process."
exit 0
You can even specify a file where to redirect the stdout or stderr of the child:
/usr/local/bin/do-process.sh 1>&- 2>/tmp/stderr_from_do-process &
but you must always specify a redirection for 1 and for 2. If one of the two redirections is missing, the child process is not detached. Thank you very much!
@cescp : Is there any reason why this issue is still open? What do you expect that fcgiwrap project to do about it? - If nothing, kindly close.
I'm trying to do the following: when a request is received, an immediate response 202 should be given, while a long background process is started. In nginx.conf:
/usr/local/bin/do-something.sh
/usr/local/bin/do-process.sh
The expected result is that ampersand used in calling do-process.sh should detach the child process, and 202 code should be returned immediately, as when calling /usr/local/bin/do-something.sh from console. But when I make a request via nginx:
the response is not given immediately, but after 10 seconds.
I'm using spawn with config /etc/sysconfig/spawn-fcgi:
in a CentOS 7 computer.
Is this the right way of calling a process and start a child process without waiting it to finish?