Solved

Spawning long process

Posted on 1997-11-11
5
184 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now