简体   繁体   中英

Crontab doesn't log to file

I'm using crontab to execute my script every minute. But script don't work properly. This script should create file, log to another file and write to DB. Only write to DB work. When I start script manually it work fine. This is script:

#!/usr/bin/php
<?php
require 'include/functions.php';
require 'include/logger.php';

$lock_file_name = "test.txt";
$lock_file = fopen($lock_file_name, "w+");
fclose($lock_file);

$log_file = "test.log";
$log = new log($log_file);
$logEnabled = 1;

if ($logEnabled==1) {$log->add("DEBUG: Start test.");}

if_dbconn();
$SQL = "INSERT INTO .`test` VALUES (1666, 6, 6, '6', '6', '6', '6', 6, '6', '6', 6, 6, 6, '6')";
mysql_db_query($db,$SQL);
?>

My cron look like this:

* * * * * /usr/bin/php -f /path/to/script/testCron.php > /dev/null

I would remove

> /dev/null

and replace this with logging your stdout/stderr to a file to see what's going on.

> /tmp/cron.log 2>&1

Processes run under cron with a very limited environment, and I suspect your program is expecting something in the environment that isn't there, or is running in a directory that doesn't permit writing the log file. The above redirection will tell you immediately what's going on.

You need to specify a full path or change current directory before running script. The script has no access rights to create a log file in the directory where cron starts it.

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