Perl用作脚本,但不用作守护进程

时间:2017-03-06 14:05:25

标签: perl daemon

我编写了一些Perl脚本来自动完成一些工作。

除了这一个之外,所有这些功能都很好:

use DBI;

use strict;

use Proc::Daemon;
use File::Copy;

# variables to connect to databases
my $info_to_connect_to_database;

Proc::Daemon::init

my $continue = 1;

$SIG{TERM} = sub { $continue = 0 };

while ( $continue ) {

    sleep( 15 );

    my $sql = 'SELECT something FROM somewhere';
    my $sth = $dbh->prepare( $sql );
    $sth->execute();

    while ( my @row = $sth->fetchrow_array ) {

        my $id              = $row[0];
        my $hash            = $row[1];
        my $direction       = '/home/somewhere/' . $hash;
        my $other_direction = '/var/storage/' . $id;

        opendir( my $dir, $direction ) or die "Cannot open direction: " . $direction . "\n";

        while ( my $file = readdir $dir ) {

            next if ( $file eq "." or $file eq ".." );

            my $from = $direction . "/" . "$file";
            chmod 0755, $direction;
            chown 48, 48, $direction;

            if ( move( $from, $other_direction ) ) {

                my $insert_sql = "INSERT INTO something(data, moredata) VALUES ( '$id'  ,  '$file' )";
                my $insert_sth = $dbh->prepare( $insert_sql );
                $insert_sth->execute();
            }
        }
    }
}

如果我在没有Proc::DaemonProc::Daemon::init的情况下运行,这种方式非常好,但我希望这可以作为一个过程不断运行。

我在其他几个脚本上做了同样的工作。没有错误或日志,因为如果我不将它作为一个过程它实际上有效。我现在很无能为力。

0 个答案:

没有答案