简体   繁体   中英

Execute perl script continuously

I'm new to Perl and i'm using Windows Machine

Well i'm using PHP as well as Perl.. My complete web application was written in PHP. I have written a Perl Script for DB interaction for every 20 minutes. My Perl script goes like this.

use strict;
use warnings;

sub Process1()
{
    use DBI;
    use Date::Format;

    my $DBH=DBI->connect("DBI:mysql:dbname:localhost","root","");
    my $sth=$DBH->prepare("UPDATE tablename SET column1='blah'"); #update query
    $sth->execute();
    while (my @row=$sth->fetchrow_array)
    {
        print $row[0] . "\n";
    }
}


while(1) {
    &Process1(); 
    sleep 900;
}

If I have to run my perl script, I would just open Perl Commad line and execute the command

perl C:xampp\htdocs\samplescript.pl

My questions are -

  • How do I run the same Perl script in my Linux server.
  • How to execute the command?
  • If I close the perl command line in windows, the script will be stopped.
    How to stop the same script in linux server?

IMHO, the best way to do this is to write your perl code as a simple "one-shot" treatment, and rely on the features provided by the system to run it repetitively:

  • Scheduled Tasks on Windows
  • Crontab on Linux

How do I run the same Perl script in my Linux server?

There are several ways to execute an perl script on linux.

You need to execute your Script with an interpreter. In the most cases you use:

/usr/bin/env perl /Path/To/Your/script.pl

Or

/usr/bin/perl /Path/To/Your/script.pl

Atleast there is a better way:

You define the Interpreter Path into the first row in your script.

#!/usr/bin/env perl
use strict;
use warnings;

sub Process1()
{
    use DBI;
    use Date::Format;

    my $DBH=DBI->connect("DBI:mysql:dbname:localhost","root","");
    my $sth=$DBH->prepare("UPDATE tablename SET column1='blah'"); #update query
    $sth->execute();
    while (my @row=$sth->fetchrow_array)
    {
        print $row[0] . "\n";
    }
}


while(1) {
    &Process1(); 
    sleep 900;
}

Then you are able to execute your script with the following command:

/path/to/your/script.pl

How to execute the command?

You can execute the same command at the Linux Terminal?


If I close the perl command line in windows, the script will be stopped. How to stop the same script in linux server?

You can use the linux "kill" command. If you run it in Background. The script will also stop if you close the Terminal Session or pressing strg+c.

You can also look at this module and build your own Perl Command to stop the script.

Getopt::Long


My Tip

Daemonize your Script and insert some commands to handle the script. So it can run in Background.

Explanation: Daemon(computing)

This may help if you didn't find them online.

To run the script in Linux perl "path"/script.pl . To run the script continuously in linux either use crontab or run the script via shell script and run it background.

And to stop the script in Linux find and kill the process using ps -ef|grep script.pl command and kill -9 "process_id" respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM