简体   繁体   English

在crontab中执行时,Perl脚本无法写入数据库,即使在手动执行时也可以写入

[英]Perl script unable to write to database when executed in crontab despite being able to write if executed manually

I've been wrestling with this issue for awhile now.. and I have tried many solutions, such as: 我已经为这个问题努力了一段时间。并且我尝试了许多解决方案,例如:

  1. Why can't DBD::SQLite insert into a database through my Perl CGI script? 为什么DBD :: SQLite无法通过我的Perl CGI脚本插入数据库?

  2. Why do I get sqlite error, “unable to open database file”? 为什么出现sqlite错误,“无法打开数据库文件”?

Brief 简要

A few weeks ago, I migrated my server from Laravel 4.0 to another server which is now the latest version of Laravel 5.0. 几周前,我将服务器从Laravel 4.0迁移到了另一个服务器,该服务器现在是Laravel 5.0的最新版本。

In the old server , I have this Perl file which is a scraper which I run using a crontab every 30 minutes called getListOfClasses.pl 在旧服务器中 ,我有一个Perl文件,这是一个刮板,我每隔30分钟使用crontab运行一次,名为getListOfClasses.pl

Using the following crontab command on my OLD server, I would run this: 在我的OLD服务器上使用以下crontab命令,我将运行此命令:

0,30 * * * * /var/www/loop/storage/scripts/getListOfClasses.pl  >> /var/www/loop/storage/logs/laravel-scraper.log 2>&1

Which executes the scraper in /var/www/loop/storage/scripts/getListOfClassesFromSubjects.pl and writes to my database in /var/www/loop/storage/database.sqlite 其中在执行刮板/var/www/loop/storage/scripts/getListOfClassesFromSubjects.pl和写入我的数据库/var/www/loop/storage/database.sqlite

After my move, Laravel 5.0 changed the default database location from storage to database , so I edited my crontab to reflect that change as well the database name from: 移动之后,Laravel 5.0将默认数据库位置从storage更改为database ,因此我编辑了crontab来反映该更改以及数据库名称的来源:

my $dbFile = '../storage/database.sqlite';

to the new file path location 到新的文件路径位置

my $dbFile = '../../database/database.sqlite';

The Issue 问题

If I run my scraper manually at: 如果我在以下位置手动运行刮板:

/var/www/schedulizer/storage/scripts/getListOfClasses.pl

I am able to scrape just fine. 我能刮得很好。 However, if I rely on the crontab to execute the script, I receive the following errors: 但是,如果我依靠crontab执行脚本,则会收到以下错误:

DBI connect('dbname=../../database/database.sqlite','',...) failed: unable to open database file at /var/www/schedulizer/storage/scripts/getListOfClasses.pl line 22.

Line 22 is my $dbh = DBI->connect($dsn, $user, $password, { . I don't believe this line of code is relevant - I guess my server has issues writing to that database. 第22行是my $dbh = DBI->connect($dsn, $user, $password, { 。我不相信这行代码是相关的-我想我的服务器在写入该数据库时遇到了问题。

The permissions that my SQLite database has is the following: 我的SQLite数据库具有的权限如下:

-rwxrwxrwx 1 www-data root 8845312 Nov  3 00:05 database.sqlite

The folder in which the database exists has the following permissions: 数据库所在的文件夹具有以下权限:

drwxr-xr-x  5 www-data root       4096 Nov  3 00:05 database

These permission levels all are the same as my old server's permission levels for both the database file as well as the folder.. I've also tried chown and chmod 777 on the database file so it has all the permissions possible. 这些权限级别都与我的旧服务器对数据库文件以及文件夹的权限级别相同。.我还尝试对数据库文件使用chownchmod 777 ,因此它具有所有可能的权限。 Still no luck. 仍然没有运气。

Any one have clues why? 有人知道为什么吗?

When testing manually, you probably went to the correct working directory for the script to do the editing. 手动测试时,您可能已进入脚本的正确工作目录进行编辑。 Starting it from any other location would most likely have caused failure as well. 从任何其他位置启动它也很可能也导致了故障。

  • Use absolute pathes instead of relative to make sure you access the correct and existing directory. 使用绝对路径而不是相对路径,以确保您访问正确的目录和现有目录。
  • Include a cd /some/where ; 包括cd /some/where ; before your command in the crontab. 在crontab中的命令之前。 Cron sets your home directory, no matter where the called program sits. 无论被调用程序位于何处,Cron都会设置您的主目录。

The second proposal is imho the more portable, because it does not require script changes when the location or machine is changed; 第二个建议是恕我直言,因为它在更改位置或机器时不需要更改脚本;因此,它更便于移植。 you simply adapt it in your (machine-specific) crontab. 您只需在您的(特定于计算机的)crontab中调整它即可。

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

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