You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何我的Cron任务无法运行voteDay.php文件?

Troubleshooting Cron Job Failure for voteDay.php Script

Let's work through why your script runs manually but fails in Cron—this is super common, and almost always tied to environment or path differences between your terminal session and Cron's context.

First, let's recap your setup for clarity:

I need to run voteDay.php via Cron to add a test email to my database. The script works fine when I run it manually, but the Cron job won't execute it.

My script code:

<?php 
require_once '../phpWorks/key.php'; 
$stmt = $pdo->prepare('INSERT INTO mail ( consignor, recipient, mail ) VALUES (?, ?, ? )'); 
$stmt->execute( array( "dictionary", "gun girl", "cron jobs is working" ) ); 
print_r( $stmt->errorInfo() ); 
?>

My Cron command:
```bash
*/15 * * * * /opt/php56/bin/php /home/.../voteDay.php

Top Fixes to Try (in order)

1. Fix the relative path in your require_once

This is the #1 culprit for Cron failures. When you run the script manually, your working directory is the folder where voteDay.php lives—but Cron runs from your user's home directory (e.g., /home/your_username/). That means ../phpWorks/key.php points to a non-existent location when Cron runs it.

Two easy fixes:

  • Replace the relative path with an absolute path in your script:
    require_once '/home/your_project/phpWorks/key.php'; // Use the full server path to key.php
    
  • Or, tell Cron to switch to the script's directory first before running it:
    */15 * * * * cd /home/your_project/path/to/voteDay && /opt/php56/bin/php voteDay.php
    

2. Verify your full file path in the Cron command

Make sure you're not using a truncated path (/home/.../voteDay.php)—Cron needs the complete, absolute path to your script. Double-check the path with pwd in the terminal while in the script's folder, then copy-paste that full path into the Cron command.

3. Log Cron output to see exact errors

Cron doesn't show you errors by default—they get sent to your system mail (which you might not check). Redirect the output to a log file so you can see what's breaking:

*/15 * * * * /opt/php56/bin/php /home/full/path/to/voteDay.php >> /home/your_username/cron_vote_log.txt 2>&1

After 15 minutes, check cron_vote_log.txt—it'll tell you if there's a file missing, database connection error, or permission issue.

4. Check permissions and Cron's user context

  • Ensure the user running Cron (usually your system user) has read access to voteDay.php and key.php, plus write access to the database (though you said the script runs manually, so this is less likely, but worth confirming).
  • Verify that /opt/php56/bin/php is the correct path to your PHP CLI binary—run /opt/php56/bin/php -v in the terminal to make sure it exists and works.

5. Test with a minimal Cron script

Rule out general Cron issues by creating a simple test script test_cron.php:

<?php
file_put_contents('/home/your_username/cron_test.txt', 'Cron ran at ' . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
?>

Add this Cron job:

*/5 * * * * /opt/php56/bin/php /home/full/path/to/test_cron.php

If cron_test.txt gets created, Cron is working—your issue is definitely with the original script's path or dependencies. If not, check if the Cron service is running (sudo systemctl status cron on most systems) or if your user has Cron permissions.

内容的提问来源于stack exchange,提问作者Gai Kuroda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:38:55