help-octave
[Top][All Lists]
Advanced

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

Re: system-question


From: Joao Cardoso
Subject: Re: system-question
Date: Wed, 27 Oct 1999 18:35:45 +0100

Mats Jansson wrote:

> Hi!
>
> I want to start a program in the background from octave. I want to be
> able to check if the program is still running and I want to be able to
> close the program from octave.
>
> My idea was to start the program (in this case emacs) with
>
>            pid = system("emacs", 1, "async").
>
> To check if the program is still running I could use 'system("ps")',
> to close the program I could use 'system(["kill ", num2str(pid)])'.
>
> system("emacs", 1, "async") returns a PID to a new octave-process, and
> not to the emacs-process, so I don't know the PID to the process to
> kill.

you must 'exec' after fork, so the exec program (emacs) will substitute
the forked one (octave).
somethink like:

function pid = launch (command, args)

# pid = launch (command, args)
#
# this is similar to system(async), (fork/exec really)
# but it first closes stdout and stderr to avoid spurious messages

if (nargin == 1 || nargin == 2)

        if (nargin == 1)
                args = "";
        endif

        if (isstr (command))
                pid = fork ();
                fp = fopen("/dev/null","w+");
                if (pid == 0)
#                       dup2 (fp, stdin);       # can't close this one...

                        dup2 (fp, stdout);
                        dup2 (fp, stderr);
#                       on_signal(2,"SIG_IGN","");
                        if (exec (command, args) < 0)
                                error ("launch: unable to start process
`%s'", c
ommand);
                                exit (0);
                        endif
                else
                        fclose(fp);
                        return;
                endif
        else
                error ("launch: file name must be a string");
        endif
else
        usage ("launch (command, args)");
endif

endfunction


>
>
> Does anybody know how to solve the problem?
>
> Thanks!
> Mats
>
> address@hidden ~]$ octave --norc
> GNU Octave, version 2.0.14 (i386-redhat-linux-gnu).
> Copyright (C) 1996, 1997, 1998, 1999 John W. Eaton.
> This is free software with ABSOLUTELY NO WARRANTY.
> For details, type `warranty'.
>
> octave:1> system("ps f")
>   PID TTY      STAT   TIME COMMAND
> 24927 pts/7    S      0:00 -csh
> 24987 pts/7    S      0:00 octave --norc
> 24988 pts/7    R      0:00  \_ ps f
> ans = 0
> octave:2> pid=system("emacs",1,"async")
> pid = 25024
> octave:3> system("ps f")
>   PID TTY      STAT   TIME COMMAND
> 24927 pts/7    S      0:00 -csh
> 24987 pts/7    S      0:00 octave --norc
> 25024 pts/7    S      0:00  \_ octave --norc
> 25025 pts/7    S      0:03  |   \_ emacs
> 25031 pts/7    R      0:00  \_ ps f
> ans = 0
> octave:4> ## emacs are closed (ctrl-X ctrl-C)
> octave:4> system("ps f")
>   PID TTY      STAT   TIME COMMAND
> 24927 pts/7    S      0:00 -csh
> 24987 pts/7    S      0:00 octave --norc
> 25032 pts/7    R      0:00  \_ ps f
> ans = 0
> octave:5> ## This works fine, I can check if pid is still present
> octave:5> pid=system("emacs",1,"async")
> pid = 25034
> octave:6> system("ps f")
>   PID TTY      STAT   TIME COMMAND
> 24927 pts/7    S      0:00 -csh
> 24987 pts/7    S      0:00 octave --norc
> 25034 pts/7    S      0:00  \_ octave --norc
> 25035 pts/7    S      0:03  |   \_ emacs
> 25036 pts/7    R      0:00  \_ ps f
> ans = 0
> octave:7> system("kill -9 25034")
> ans = 0
> octave:8> system("ps f")
>   PID TTY      STAT   TIME COMMAND
> 24927 pts/7    S      0:00 -csh
> 24987 pts/7    S      0:00 octave --norc
> 25038 pts/7    R      0:00  \_ ps f
> 25035 pts/7    S      0:03 emacs
> ans = 0
> octave:9> ## Emacs is still there, I want it to disappear!
>
> ---------------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.  To ensure
> that development continues, see www.che.wisc.edu/octave/giftform.html
> Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
> ---------------------------------------------------------------------

--
Joao Cardoso                |   e-mail: address@hidden
INESC, R. Jose Falcao 110   |   tel:    + 351 2 2094322
4050 Porto, Portugal        |   fax:    + 351 2 2008487





---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

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