Solved

Spawning long process

Posted on 1997-11-11
5
188 Views
Last Modified: 2008-02-26
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)
0
Comment
Question by:emmons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1208561
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.
0
 
LVL 4

Author Comment

by:emmons
ID: 1208562
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)

0
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1208563
Ok, the flush is making sure the messages are written by your program but the server isn't sending them to the client. The way around this is to tell the server to send everything you write directly to the client, by renaming your process so it starts with "nph", which stands wfor "non-parsed headers". This also means you will need to write all headers, so before your "Content-Type" line add:

print "HTTP/1.1 200 OK\n";

You can forget my suggestion with the "&", since it seems that's not the problem. Good luck!
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208564
Let me clarify that what you should rename to nphxxx is the script called by the server, not the shell script you are invoking.
0
 
LVL 4

Author Comment

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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question