简体   繁体   English

连续执行 perl 脚本

[英]Execute perl script continuously

I'm new to Perl and i'm using Windows Machine我是 Perl 的新手,我正在使用 Windows 机器

Well i'm using PHP as well as Perl.. My complete web application was written in PHP.嗯,即时通讯使用 PHP 以及 Perl.. 我完整的 web 应用程序是用 PHP 编写的。 I have written a Perl Script for DB interaction for every 20 minutes.我每 20 分钟编写一个用于数据库交互的 Perl 脚本。 My Perl script goes like this.我的 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;
}

If I have to run my perl script, I would just open Perl Commad line and execute the command如果我必须运行我的 perl 脚本,我只需打开 Perl Commad 行并执行命令

perl C:xampp\htdocs\samplescript.pl

My questions are -我的问题是——

  • How do I run the same Perl script in my Linux server.如何在我的 Linux 服务器中运行相同的 Perl 脚本。
  • How to execute the command?如何执行命令?
  • If I close the perl command line in windows, the script will be stopped.如果我在 Windows 中关闭 perl 命令行,脚本将停止。
    How to stop the same script in linux server?如何在linux服务器中停止相同的脚本?

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:恕我直言,最好的方法是将您的 perl 代码编写为简单的“一次性”处理,并依靠系统提供的功能重复运行它:

  • Scheduled Tasks on Windows Windows 上的计划任务
  • Crontab on Linux Linux 上的 crontab

How do I run the same Perl script in my Linux server?如何在我的 Linux 服务器中运行相同的 Perl 脚本?

There are several ways to execute an perl script on linux.有几种方法可以在 Linux 上执行 perl 脚本。

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?您可以在 Linux 终端执行相同的命令吗?


If I close the perl command line in windows, the script will be stopped.如果我在 Windows 中关闭 perl 命令行,脚本将停止。 How to stop the same script in linux server?如何在linux服务器中停止相同的脚本?

You can use the linux "kill" command.您可以使用 linux 的“kill”命令。 If you run it in Background.如果你在后台运行它。 The script will also stop if you close the Terminal Session or pressing strg+c.如果您关闭终端会话或按 strg+c,脚本也会停止。

You can also look at this module and build your own Perl Command to stop the script.您还可以查看此模块并构建您自己的 Perl 命令来停止脚本。

Getopt::Long Getopt::长


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 .在 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.要在 linux 中连续运行脚本,请使用 crontab 或通过 shell 脚本运行脚本并在后台运行。

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.并在 Linux 中停止脚本,分别使用ps -ef|grep script.pl命令和kill -9 "process_id"查找并终止进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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