如何用php编写Cron?
<?php
class Baddriver{
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli("localhost", "root", "password", "dbname");
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function bad() {
if(isset($_POST["plakano"]) || isset($_POST["secim"]) || isset($_POST["sehir"])){
$sehir=$_POST["sehir"];
$plakano=$_POST["plakano"];
$aciklama=$_POST["aciklama"];
$secim=$_POST["secim"];
$plaka = str_replace(",","",$plakano);
$stmt = $this->db->prepare("INSERT INTO veri (plakano,aciklama,tarih,sehir,secim) VALUES (?, ?, CURDATE(),?,?)");
$stmt->bind_param("ssss", $plakano,$aciklama,$sehir,$secim);
$stmt->execute();
$stmt->close();
$this->db->commit();
}
}
}
$api = new Baddriver;
$api->bad();
?>
这是我的 php 文件,我搜索了 2 天,现在我明白了两件事,首先是我需要放在
#!/usr/bin/php -q
顶部(老实说不知道为什么),我需要连接到我的网络服务器,打开 shell 并编写 cron 代码,如下
PATH=/usr/local/sbin:/usr/local/bin:/home/user1/bin
[email protected],[email protected]
0 2 1-10 * * du -h --max-depth=1 /
所示这一切都比 cron 有效还是我缺少的东西?感谢您的耐心和时间。
<?php
class Baddriver{
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli("localhost", "root", "password", "dbname");
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function bad() {
if(isset($_POST["plakano"]) || isset($_POST["secim"]) || isset($_POST["sehir"])){
$sehir=$_POST["sehir"];
$plakano=$_POST["plakano"];
$aciklama=$_POST["aciklama"];
$secim=$_POST["secim"];
$plaka = str_replace(",","",$plakano);
$stmt = $this->db->prepare("INSERT INTO veri (plakano,aciklama,tarih,sehir,secim) VALUES (?, ?, CURDATE(),?,?)");
$stmt->bind_param("ssss", $plakano,$aciklama,$sehir,$secim);
$stmt->execute();
$stmt->close();
$this->db->commit();
}
}
}
$api = new Baddriver;
$api->bad();
?>
This is my php file i search for 2 days now i understand 2 things first is i need to put
#!/usr/bin/php -q
at the top (honestly don't know why) and i need to connect to my webserver open the shell and write the cron code like
PATH=/usr/local/sbin:/usr/local/bin:/home/user1/bin
[email protected],[email protected]
0 2 1-10 * * du -h --max-depth=1 /
is it all than cron will work or something that i am missing? thanks for patiance and time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
#!/usr/bin/php -q
告诉您的服务器该脚本应该使用 php 运行。您添加到 crontab 的 cron 代码将按指定的时间间隔运行您的脚本。
但这还不是全部。您的脚本使用 $_POST,但如果您将其作为 cron 作业运行,则无法发布到 PHP 脚本。所以这些值将不可用。
#!/usr/bin/php -q
tells your server that this script should be run with php.The cron code which you add to crontab will run your script at the specified intervals.
But that's not all. Your script uses $_POST, but you cannot post to a PHP script if you run it as a cron job. So those values won't be available.
Cron 是一个按计划运行命令行工具的工具。因此,只需以不需要 Web 服务器的任何功能的方式编写 php 脚本即可。即:setheaders,$_GET,$_POST,$_COOKIE
#!/usr/bin/php -q
是如果您使脚本可执行并直接从命令行运行它,它会提示如何来运行它。在本例中,'在 /usr/bin 中使用 php 并使用选项“-q”'-q 告诉脚本“安静”运行,并避免打印一些 http 标头。
另外,请参阅
man 5 crontab
了解有关 crontab 条目格式的更多详细信息。您可以跳过设置脚本可执行文件和 #!如果您在 crontab 条目末尾使用
/usr/bin/php -q badDriver.php
,则该行。将在服务器时间每周日晚上 11:53 以用户“www-data”运行该脚本。
Cron is a tool to run command line tools on a schedule. As such, a php script just needs to be written in a way that it doesn't require any functions of the web server. ie: setheaders, $_GET, $_POST, $_COOKIE
The
#!/usr/bin/php -q
is if you make your script executable and run it directly from the command line, it hints at how to run it. In this case 'use php in /usr/bin with the option "-q"'The -q tells the script to run 'quiet', and avoids printing some http headers.
Also, see
man 5 crontab
for more details about the formatting of a crontab entry.You can skip setting the script executable, and the #! line if you use
/usr/bin/php -q badDriver.php
at the end of your crontab entry.Would run the script, as user 'www-data', every Sunday at 11:53pm server time.