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


I have a perl script called

Now I want to write a script, lets say which will call the script and execute it every 2 minutes.

Any pointers how can I do it?

Replies are listed 'Best First'.
Re: Running a script iteratively.
by apl (Monsignor) on Apr 24, 2008 at 14:37 UTC
Re: Running a script iteratively.
by zentara (Archbishop) on Apr 24, 2008 at 14:50 UTC
    If you want to run it interactively, it usually means sending values to it, and getting values back. The general way to do it is thru IPC::Open3. Search for examples, read perldoc perlipc, or look at this very basic code
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Running a script iteratively.
by moklevat (Priest) on Apr 24, 2008 at 14:22 UTC
    I would use cron for this, but here is a non-robust solution.

    Update: For the reasons outlined by Fletch below, don't do this...

    #!/usr/bin/perl use strict; use warnings; while(1){ ``; sleep 120; }

      Eeeew. Backticks in void context? Use the right tool for the job.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        I said it was non-robust. Perhaps I should have described it as "icky" instead?
Re: Running a script iteratively.
by mscharrer (Hermit) on Apr 24, 2008 at 14:57 UTC
    Under an Unix installations (e.g. Linux) you could use a cron job for this and tell it to start the script every two minutes. Just google after 'cron job'.

    To code it in perl, e.g. for Windows without cron daemon, you could write something like this:

    while ( 1 ) { system("perl"); sleep(120); }
    This would delay for two minutes after the get_values script ended. You might want to add an possibility to stop this loop without killing the perl process.

    Update: This might look like a repost of an above post, but I got interrupted for an half hour after I wrote this post but before I could sent it. Afterwards I was just sending it without checking if anyone else gave the same answer in the meantime.

      It's OK, you used system. My backticks were met with revulsion. :-)

        I'll see your revulsion and raise you a silent bug. The above used system without separating the arguments, or checking return value. The perldoc shows better practice. If you're writing immutable code on your own box, you can obviously shove it all into one string but it's not a habit to cultivate.

        my @args = qw( perl ); system(@args) == 0 or die "system @args failed: $?"
Re: Running a script iteratively.
by andreas1234567 (Vicar) on Apr 24, 2008 at 20:43 UTC
    There is a Windows version of cron here (Disclaimer: Never used it).
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]