Link to home
Start Free TrialLog in
Avatar of emmons
emmons

asked on

Spawning long process

I have built a cgi interface to a report generator. The reports take hours to generate, so I want to launch the task and return. I build a shell script that I then launch (for various reasons, it needs to be a shell script), but I cannot get it to return my "launched" message to the browser until the process is ended. How can I do this?
It would be sufficient to print the launched info, and then hang, but it won't print anything for me.
(FYI, my code is actually in Python, but I am hoping that the solution will be similar)
Avatar of alamo
alamo

I suspect there are two things happening here.

First is that any status messages you print before launching your script are probably being buffered and thus aren't sent to the browser. The solution is to turn buffering off, which is done in perl with "$| = 1;" (assuming STDOUT is the active filehandle). This will cause what you print to be sent back to the browser immediately.

Second, normally a shell script invoked via system() won't return until it completes. So if you are trying to print a "Launched successfully" message after your system() call it won't be printed until the process is done. The solution here is to launch your script as a background process, i.e. add "&" to the end of the command.

Good luck, hope this helps... if this turns out to be applicable to your problem I'll post it as an answer.
Avatar of emmons

ASKER

thanks alamo.

I will try the buffering thing. Though I do a flush() and then a close() of stdin and stdout before I call my script.

I did try putting the ampersand after my task, but that runs it in the background and does not actually detach it from the shell. I fear that I need to run it as a detached process in order to get it to return.

In case it helps, the relevent code is...

def tellStatus( stuff) :
   printPageHeader()
   print stuff,
   print "<br>File ",
   print temp_file_name,
   print " has been submitted for execution<br>",
   print "please reference this number to the administrator<br>"
   sys.stdout.flush()
   sys.stdout.close()
   sys.stdin.close()

   tellStatus( "Close before fork")
   if( fork() == 0) :
# execute the code in a child process
      createScriptFile()
      chmod( temp_file_name, 0777)
      command_line = "nohup " + temp_file_name + " &"
      os.system( command_line)
      sys.exit(0)
   else :
      sys.exit(0)

ASKER CERTIFIED SOLUTION
Avatar of alamo
alamo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me clarify that what you should rename to nphxxx is the script called by the server, not the shell script you are invoking.
Avatar of emmons

ASKER

Is that how to get this stuff to be unbuffered??!!
Thank you very much. That should do it.