简体   繁体   English

我怎样才能让我的 cron 工作在 cpanel 中工作?

[英]How can I get my cron job working in cpanel?

I need to automate a couple things for my website, one of them being having the database cleared at midnight every night.我需要为我的网站自动化一些事情,其中​​之一是每晚午夜清除数据库。 I looked at cron job tutorials and came up with a script我查看了 cron 工作教程并想出了一个脚本

<?php 
$con = mysqli_connect("localhost", "root", "password", "test");
$sql = "DELETE FROM markers";
$con->exec($sql);
echo "Record deleted successfully";
$con = null;

INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);

this just wipes the database and re-inserts records with empty values to be modified throughout the day.这只是擦除数据库并重新插入带有空值的记录,以便全天修改。

I tried setting up a cron job to run this at midnight using cpanel, my path is我尝试使用 cpanel 设置一个 cron 作业在午夜运行它,我的路径是

/usr/bin/php /home/user/public_html/command.php

I have the command.php in my uploaded web files and set the job to run every 30 mins to test it, but no matter what I do I cannot seem to get it to work.我上传的 web 文件中有 command.php 并将作业设置为每 30 分钟运行一次以测试它,但无论我做什么,我似乎都无法让它工作。

If you want to run a PHP script located in the public_html directory, the correct cron command will be:如果要运行位于 public_html 目录中的 PHP 脚本,正确的 cron 命令将是:

/usr/local/bin/php /home/**cPaneluser**/public_html/yourscript.php

Or you can just use curl request as the cron job command.或者您可以仅使用curl request作为 cron 作业命令。

curl "https://www.yoursite.com/yoursctipt.php"

To run your script using a cron job the same way you do in the browser:要使用与在浏览器中相同的方式使用 cron 作业运行脚本:

/usr/bin/curl -o command.log '/home/user/public_html/command.php' >/dev/null 2>&1

the -o redirects stdout, and ">/dev/null 2>&1" redirects stderr to stdout, which will let you see both the results and any errors in the command.log file. -o 重定向标准输出,而 ">/dev/null 2>&1" 将标准错误重定向到标准输出,这将让您看到结果和 command.log 文件中的任何错误。

What i noticed is that you are using我注意到你正在使用

/usr/bin/php /home/user/public_html/command.php

Have you tried just using the plain php command only?你试过只使用普通的 php 命令吗?

30 * * * * php /path/to/php/file >> /path/to/desired/log/file/location

My assumption is that the php is not installed on usr/bin directory.我的假设是 php 没有安装在usr/bin目录中。

You can set cron in linux based server like that您可以像这样在基于 linux 的服务器中设置 cron

0 0 * * * wget -O /dev/null your_url ( http://localhost/test.php ) 0 0 * * * wget -O /dev/null your_url ( http://localhost/test.php )

it will run at every mid night.它将在每个午夜运行。

for set cron time please visit Crontab要设置 cron 时间,请访问Crontab

Try any of these:尝试以下任一方法:

wget -O -q http://example.com/command.php

php /home/user/public_html/command.php

/usr/bin/curl http://www.example.com/command.php

Also, make sure you enter your email address in the Cpanel cron job so you can at least know that the script is being ran on schedule, if it is then the problem is probably somewhere else.另外,请确保您在 Cpanel cron 作业中输入您的电子邮件地址,以便您至少可以知道脚本正在按计划运行,如果是,那么问题可能出在其他地方。

Also, in your code, there's this line:此外,在您的代码中,有这一行:

INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);`

Putting this line literally in your script does nothing and should cause an error.将这一行字面意思放在脚本中没有任何作用,应该会导致错误。 You should execute this properly.您应该正确执行此操作。 Eg, Your script should look something like this instead:例如,你的脚本应该看起来像这样:

<?php
$con = mysqli_connect("localhost", "root", "password", "test");
$sql = "DELETE FROM markers";
$con->exec($sql);
echo "Record deleted successfully";
$sql = "INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);"
$con->exec($sql);
$con = null;

You can simply use Your Hostinger Panel for doing the job.您可以简单地使用您的 Hostinger 面板来完成这项工作。

https://www.hostinger.com/tutorials/cpanel/#How-to-Create-a-Cron-Job https://www.hostinger.com/tutorials/cpanel/#How-to-Create-a-Cron-Job

Please check it out and you can help yourself from There.请检查一下,您可以从那里帮助自己。

Question of bounty poster:赏金海报问题:

I too have a php script that creates/modifies folders and files.我也有一个创建/修改文件夹和文件的 php 脚本。 It works perfectly when its address is loaded through a browser.当它的地址通过浏览器加载时,它工作得很好。 The output from the cron job is accurate, but it's not making any of the directory changes it should be. cron 作业的输出是准确的,但它没有进行任何应有的目录更改。 I'm also using Hostinger like OP.我也像 OP 一样使用 Hostinger。

My take on it probably you are getting permission issue on cron it is working for server user but not for cron try with below command to work with your apache-user I am assuming apache server is what you are using.我的看法可能是您在 cron 上遇到了权限问题,它适用于服务器用户,但不适用于 cron 尝试使用以下命令与您的 apache 用户一起使用我假设 apache 服务器是您正在使用的。

crontab -eu apache-user

Or you can give the permission to required user or group to let them maniuplate directories and files something like below.或者您可以向所需的用户或组授予权限,让他们操作如下所示的目录和文件。

chmod -R 655 /directory

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

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