简体   繁体   English

通过Crontab Qnap NAS执行PHP脚本

[英]Execute PHP Script through Crontab Qnap NAS

Im trying to run a Script with Cron: 我正在尝试使用Cron运行脚本:

The Cron I used: 我使用的Cron:

ssh: crontab /test.txt

test.txt: test.txt:

* * * * * /path/to/script/cron/dashboard.php

(I set the Interval to every minute to check if it works) (我将间隔设置为每分钟以检查是否有效)

dashboard.php: dashboard.php:

<?php
$con=mysqli_connect("localhost","myuser","mypw","mydb");
$randomnumber = rand(1000,3000);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO dashboard (count, date)
VALUES ('$randomnumber', NOW())");


mysqli_close($con);

?>

I inserted the crontab via crontab /test.txt (located in root). 我通过crontab /test.txt (位于根目录中)插入了crontab。

Then executed this line (I followed this tutorial: Qnap Turorial for Crontabs ): 然后执行此行(我遵循了本教程: Crontabs的Qnap Turorial ):

/etc/init.d/crond.sh restart

And I tried to open the Script in a Browser and it worked (I had a new row in my table with a random number and the current date. But If I check my Database every minute nothing new is added through the cron tab.. 然后,我尝试在浏览器中打开脚本,并且脚本起作用了(我的表中有一个新行,其中包含一个随机数和当前日期。但是如果我每分钟检查一次数据库,则cron选项卡不会添加任何新内容。

I run the System through my QNAP NAS... 我通过QNAP NAS运行系统...

You could try to edit the default crontab directly: 您可以尝试直接编辑默认的crontab:

  1. Edit /etc/config/crontab via WinSCP or open it with crontab -e 通过WinSCP编辑/etc/config/crontab或使用crontab -e打开它

  2. Add your entry * * * * * /path/to/script/cron/dashboard.php 添加您的条目* * * * * /path/to/script/cron/dashboard.php

  3. Execute the command crontab /etc/config/crontab 执行命令crontab /etc/config/crontab

  4. And finally restart the cron-service with /etc/init.d/crond.sh restart 最后使用/etc/init.d/crond.sh restart cron服务

After those steps, the cron-service should execute the cron-jobs. 完成这些步骤后,cron服务应执行cron-jobs。

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

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