简体   繁体   English

Cron工作帮助

[英]Cron jobs help

I am trying to make a cron job via my websites cpanel. 我正在尝试通过我的网站cpanel进行cron工作。 I have talked to the support services and they gave me this command to run to execute a php file on my website. 我已经与支持服务部​​门进行了交谈,他们给了我该命令以运行以在我的网站上执行php文件。

/usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php

This doesnt seem to work though, I have also set the minute interval to every minute using */1. 但是,这似乎不起作用,我还使用* / 1将分钟间隔设置为每分钟。

I am using the code below inside of the file update.php: 我在文件update.php中使用下面的代码:

<?php
ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

require('minequery.class.php');
include('../config/config.php');

    $date = date("D-M-Y");

    $file = fopen("UPDATE_LOG($date).txt", 'w');

    $query = mysql_query("SELECT * FROM servers") or die(mysql_error());

    if($query) {

    while($row = mysql_fetch_array($query)) {

        $ip = $row['ip'];
        $port = $row['port'];
        $name = $row['name'];

        $string = "[UPDATE LOG]: $date - Updated Server $name \n";


        fwrite($file, $string);

        print("[UPDATE LOG] Updated Server $name </br>");



    }

    mail("duncan@mymcstatus.net","UPDATED","Server has updated","From: Mymcstatus");    

    } else {

        print("Cant query");
    }



?>

When I go to update.php manually through the web browser that code works, but nothing seems to happen with the Cronjob. 当我通过网络浏览器手动更新update.php时,该代码有效,但Cronjob似乎没有任何反应。 What am I missing or doing wrong? 我想念什么或做错什么?

There are a few things that could be going on here. 这里可能会发生一些事情。 The first is to check the file permissions of update.php and make sure it is executable. 首先是检查update.php的文件权限,并确保它是可执行的。 The cron may be executing as a different user that doesn't have permission to execute update.php. cron可能以没有权限执行update.php的其他用户身份执行。 The 2nd thing you can try is including this as the very first line of update.php with no whitespace before it. 您可以尝试的第二件事是将其作为update.php的第一行,其中没有空格。

#!/usr/local/bin/php

Usually cron jobs aren't run from the same directory where your PHP lives so it's possible that it is running but the output file is being created elsewhere. 通常,cron作业不是在您的PHP所在的目录中运行,因此有可能正在运行,但输出文件是在其他位置创建的。 Try changing the output file path to be the full path to the file, ie: 尝试将输出文件路径更改为文件的完整路径,即:

$file = fopen("/home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/UPDATE_LOG($date).txt", 'w');

With the help of the above comments I managed to fix the file paths but it also came down to the command. 借助以上注释,我设法修复了文件路径,但它也归结为命令。 I had put the wrong user path in here is the command that works. 我在这里输入了错误的用户路径,这是有效的命令。

/usr/local/bin/php -q /home/mymcstat/domains/mymcstatus.net/public_html/redesign/scripts/update.php

Thanks for the help 谢谢您的帮助

It's always a good idea to cd to your script's path. cd到脚本的路径总是一个好主意。 This way you don't need any change in the name of the include and require files and any other file names engaging in file operations. 这样,您就不需要在名称的任何变化includerequire的文件,并在文件操作博取任何其它的文件名。 Your cronjob command could look like this: 您的cronjob命令可能如下所示:

cd /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/; cd /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/; /usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php / usr / local / bin / php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php

You don't need to supply any absolute file paths this way. 您无需通过这种方式提供任何绝对文件路径。

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

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