ibeneedinghelp has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am using system() to call a seperate program that processes data I feed it and outputs a log file (All being done in UNIX). Unfortunatly the program that is called also opens up a GUI. The output is written but until the user quits the program that was opened, my script is not able to process the output file. Is there a way to have my script continue running even if the called program isn't quit? I would like to be able to check the output log to see if it is done being written to and then have my script continue by processing it. Ideally I want to be able to keep the program that was called running. Any ideas?

Thanks

Replies are listed 'Best First'.
Re: System() question
by sgifford (Prior) on Jun 13, 2006 at 01:38 UTC
    If the GUI does write the output before the user quits, but your script doesn't read it because it's waiting for the GUI to finish, you can probably fix this with system("prog ... &"), which will run the program in the background without waiting for it to finish (this is a quick way of doing fork, as McDarren suggests).

    If it doesn't write the output until it exits, you can kill it with kill when you think it's probably done, or else close its window with the xkill program.

    You may also want to look closely at whether the program has a "non-interactive" mode, or what it does if $ENV{DISPLAY} is unset.

      Cool, I appreciate everybodies help. I tried running the fork method first and got it working and then tried system("....&"). Both worked wonderfully. I had to add the following code to look for when the output log was written and look for a tag that shows when it is done being written too. I'm worried thought that this may not be the most efficient way of accomplishing this. Will the continual checking take up too much processing time? Thanks

      while (open (OUTPUT, "$path/output.txt") == 0) {

      if ($cnt == 0) {
      print "Waiting for Output.log from Program\n\n";
      }
      $cnt++;
      }

      my($lastline) = 0;
      until ($lastline =~ m/END_OF_FILE/) {
      open (OUTPUT, "$path/output.txt") or die "Can't
      open output log: $!\n";

      while (<OUTPUT>) {
      $lastline = $_;
      }
      close OUTPUT;
      unless ("$lastline" =~ "$prevline") {
      print $lastline;
      }
      $prevline = $lastline;
      }
Re: System() question
by liverpole (Monsignor) on Jun 12, 2006 at 23:54 UTC
    Hi ibeneedinghelp,

    I think that your question is unfortunately a little vague.  Rather than risk guessing exactly what you're trying to do, I'd like to recommend that you provide some example code, which will give me (and other monks) the opportunity to try running it, and see what it is that's not happening the way you'd expect or hope.

    A good tip:  if you have a LOT of code, try whittling it down so that it exhibits the behavior you're having questions on.  This will potentially yield an extra benefit, in that you may discover the problem yourself.

    But even if it doesn't, the shorter a script (or scripts) that you can provide, the more eager many of the seasoned veterans here will be to "dive in" and point you towards the path of enlightenment.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks for the tip liverpole... I didn't realize how vague my question actually was until I had posted it. Looks like fork and system(... &) solved the problem. I still had to figure out how to check to see if the output was done, included the code for how I did that below. Thanks again.
Re: System() question
by McDarren (Abbot) on Jun 13, 2006 at 00:19 UTC
    Sounds to me like you need to use fork