Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Run sequential processes (Matlab, SAS, etc.) from Perl

by rizzy (Sexton)
on Nov 26, 2010 at 19:43 UTC ( #873920=perlquestion: print w/replies, xml ) Need Help??
rizzy has asked for the wisdom of the Perl Monks concerning the following question:

I would like to run several programs (e.g., Matlab, SAS, another Perl script) from perl, but need to wait until each is processed to start the second. The reason is the previous process spits out a txt or csv file that is used by the next. Its probably easy to do in Perl, but I can't seem to figure it out. I think I need to use fork or waitpid, but am not sure exactly how.

Maybe you can do this directly in Unix? Any help would be appreciated. (note: accidentally deleted the original question. This is essentially what I originally posted)
  • Comment on Run sequential processes (Matlab, SAS, etc.) from Perl

Replies are listed 'Best First'.
Re: Run sequential processes (Matlab, SAS, etc.) from Perl
by Corion (Pope) on Nov 26, 2010 at 19:56 UTC

    See system. Or read about shell scripts.

Re: Run sequential processes (Matlab, SAS, etc.) from Perl
by chrestomanci (Priest) on Nov 26, 2010 at 22:48 UTC

    There realy is no need for perl for this. Just write a simple shell script:

    #!/bin/bash matlab myscript.m mylogfile.txt sas # ... etc.

    Or if you are truly impatient, you can put the whole thing on one command line. Just separate the commands with semi colons (;), double check it is correct, and hit enter.

Re: Run sequential processes (Matlab, SAS, etc.) from Perl
by chrestomanci (Priest) on Nov 27, 2010 at 14:12 UTC

    If you must edit your original question, then please don't change the meaning. If someone comes to this thread in a years time they won't know what you originally asked.

    In answer to your second question, pipe symbols (|) should be OK in your crontab, but personally, if I am doing anything remotely complex in a cronjob, I always write a script, and then call it from cron.

    The reason is that any job called directly from cron gets a very simple shell with hardly any features, and not much environment set-up, so stuff that works from your normal command line sometimes does not from cron. If you call a script you can put a hash-bang line in to bash, and then set-up your PATH and the rest of your environment explicitly, so everything works.

      Thanks for the response. I meant to reply to my post, but accidentally deleted it instead. I've tried to recreate it.
Re: Run sequential processes (Matlab, SAS, etc.) from Perl
by 7stud (Deacon) on Nov 26, 2010 at 23:39 UTC

    Maybe you can do this directly in Unix

    In unix, it's as simple as:

    $ prog1 | prog2 | prog3 | prog4
      Okay, I tried this at the command line, but it immediately started processing BOTH before the first had finished:

      matlab mycode.m log.txt | perl

      the matlab code spits out a csv file that is needed in the perl script.

      The matlab code is run in the background. Maybe that's affecting this.

      By the way, I also tried using semicolons as suggested, but the result was the same. I need to wait until the first process has completed before starting the second.
        The pipe idea (prog1 | prog2 | prog3) works best when these prog's are designed to read STDIN and output to STDOUT (not directly to files). I don't think that is what you want.

        If I understand correctly, when you type in the command "matlab...", that program starts a process in the background and returns immediately to the command line while this background process is still running. If you type in a command that uses the output file that matlab creates, it doesn't work because matlab is still working on that output file. So, make a simple script file with the commands as described by others. Right after the matlab command, put in the "fg" command. That moves the most recently started background process into the foreground. Now the next command won't get executed until this now foreground process finishes.

        An alternative is to use the "wait" command. wait command. Just put "wait" after the matlab command. See the example in the link.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://873920]
Approved by Old_Gray_Bear
[LanX]: YM: talking about breaches ... did you read about Cambridge Analytica harvesting 50e6 FB profiles for political propaganda?
LanX ... oh ... Brexit delayed ... again ... oh wonder ...
[Your Mother]: Heh. I keeping seeing related headlines but my narcissism is a perihelion right now so I'm busy. :P
[LanX]: https://www. news/series/ cambridge- analytica-files
[Your Mother]: s/a /at /;

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2018-03-19 13:17 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.