help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Checking status of external process


From: John W. Eaton
Subject: Checking status of external process
Date: Wed, 15 Dec 2004 13:33:45 -0500

On 15-Dec-2004, Quentin Spencer <address@hidden> wrote:

| I want to run a command using the "system" function that starts an 
| external program that performs some computations. I would like to be 
| able to start it in the background, and then run a loop that periocially 
| checks the status of the program, loads the results, and kills the 
| external process if certain conditions are met, using a loop something 
| like this:
| 
| PID = system(command, 1, 'async');
| while(1)
|     sleep(5)
|     if( PID has completed)
|        break
|     else
|        load results
|        perform computations
|        if(some condition)
|           kill(PID);
|           break;
|        end
|     end
| end
| 
| The problem is that I don't know how to test to see whether PID has 
| completed running. Is there any way to do this using built in octave 
| commands?

Yes, you can use waitpid.  Something like

  page_screen_output = 0;
  pid = system ("xterm", 1, "async");
  while (1)
    sleep (5);
    if (waitpid (pid, 1) == pid)
      printf ("PID = %d exited\n", pid);
      break;
    else
      printf ("PID = %d still running\n", pid);
      [s, err, msg] = stat ("~/killit");
      if (! err)
        kill (pid, SIG.QUIT);
        printf ("PID = %d killed\n", pid);
        break;
      endif
    endif
  endwhile

seems to work for me.  I see messages every 5 seconds when the xterm
process is running.  If I close the xterm window, the next time
through the loop, waitpid notices and breaks out of the loop.  If I
touch a file called ~/killit while the loop is running, the stat call
notices the new file and the kill sends SIGQUIT to the xterm process,
which then exits.

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]