首页 > 解决方案 > 我想用 crontab (Centos 7) 自动运行 1 个文件 Demfile.pl。但是 /var/spool/mail/root 发送邮件到日志如下

问题描述

我想Demfile2.plcrontab(Centos 7)自动运行 1 个文件。但是/var/spool/mail/root发送邮件到日志如下:

DBD::SQLite::db do failed: no such table: HISTORY1 at /root/perl/test/Demfile2.pl line 31.
DBD::SQLite::db do failed: no such table: HISTORY1 at /root/perl/test/Demfile2.pl line 31.

我不知道为什么:((这是我的代码:

use DBI; 
my $driver = "SQLite"; 
my $database = "test.db"; 
my $dsn = "DBI:$driver:dbname=$database"; 
my $userid = ""; 
my $password = ""; 
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; 
print "Opened database successfully\n"; 
$dir = "/root/perl/test*"; 
my @file = glob($dir);
@files = glob("test"); 
$temp = 0; 
$size = @file; 
$tempsize = 0; 
for (my $i = 0; $i < $size; $i++){ 
    print @file[$i]; 
    $temp++; 
    my $filesize = -s "@file[$i]"; 
    print "$filesize\n"; 
    $tempsize += $filesize; 
    my $now = date "+%Y-%m-%d//%H-%M-%S" | tr -d "\n"; 
    my $stmt = qq(INSERT INTO HISTORY1 (ID, TIME_NOW, TEN_FILE, SO_FILE, SIZE, HETHONG)VALUES ($i, '$now', '$file', '$tempsize', '$tempsize', 'A')); 
    my $rv = $dbh->do($stmt) or die $DBI::errstr; 
    print "Records created successfully\n";
} 
$dbh->disconnect();
print"\n";
print "so file $temp\n";
print "total size $tempsize\n";

标签: perltriggerscronauto

解决方案


我不知道为什么 :((

但是错误信息很清楚。

没有这样的表:HISTORY1

所以在我看来,您的数据库文件 ( test.db) 不包含您认为的数据库模式。您可以使用 SQLite 命令行工具来测试这个理论,sqlite3. 尝试运行以下命令:

$ sqlite3 test.db
sqlite> .tables

这将为您提供test.db文件中表的列表。希望您会在此处看到“HISTORY1”。

另一件事要检查。您是否从正确的目录运行它?您的代码似乎假定它是从包含该test.db文件的目录中运行的。如果您从任何其他目录运行代码,那么我认为它将创建一个新的、空的数据库文件,显然,它不会包含您的表。


推荐阅读