LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to trigger a background tar archive process on apache server (http://www.linuxquestions.org/questions/programming-9/how-to-trigger-a-background-tar-archive-process-on-apache-server-4175427470/)

dvah 09-16-2012 01:28 AM

how to trigger a background tar archive process on apache server
 
I would like to trigger a background tar process on apache server by a client browser request for a page.
I tried to have a PHP script execute exec("tar.... command &", ...) on the server when it handle the page request from the client. The server trigger the tar command but instead of running it as a background process while it finish generating the page and return it to the browser, it wait untill the tar command is done.

How can I cause the server to run the tar in the backgound and return immediately to the user?

theNbomr 09-16-2012 11:47 AM

This relates to the relationship of a web server and it's child CGI process. As the parent process, the web server launches the CGI child process with the child's stdout directed to a file descriptor on the web server that is open for reading. The web server wants to swallow all of the output from the child process, so it waits for the file descriptor to reach eof(), which normally doesn't happen until the child process terminates. This is all consistent with the behavior you are reporting.
In order for the web server to see eof() on it's child's output, you will need to coerce the child process to close it's stdout stream. For most applications, this is not possible, since the application simply isn't written with that scenario in mind. The solution, then, is probably to launch an intermediate CGI that can launch the tar process, but can also close it's standard output, so the web server can return a page to the client browser, and stop waiting for the child to complete.
--- rod.

dvah 09-16-2012 08:27 PM

theNbomr thanks,
 
questions:
1. if I close the child process stdout will it not trigger the server to close/kill the child process? or does the server allow the child process to continue running even after its stdout is closed? keep in mind that the archive process which is the function of this child process may take a long time.

2. In my case the child process is a PHP script, if I close the PHP stdout will it be good enough?

theNbomr 09-17-2012 11:00 AM

I don't think there are any hard rules for this kind of thing. The specific web server will do whatever it sees fit. I think that an Apache web server will only look at the child process' standard output, and leave the child running. I'm not sure PHP is handled the same way as a CGI child, since PHP is, in some way, built into the web server, at east as I understand it. I've never really used PHP enough to know the behavior in this circumstance.
--- rod.

Guttorm 09-18-2012 10:22 AM

PHP Code:

exec('nohup tar ... > /tmp/tar.output.txt 2> /tmp/tar.errors.txt < /dev/null &'); 



All times are GMT -5. The time now is 07:26 AM.