通过Cron作业运行PHP [英] Run PHP through Cron job

查看:104
本文介绍了通过Cron作业运行PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上有一个小的备份脚本,我一直在试图运行。不幸的是,它没有执行备份。



首先,这里是我的crontab看起来像

  * / 30 * * * * / usr / bin / php /var/www/mybackup.php 
pre>

上面的cron应该调用这个脚本: mybackup.php

 <?php 
include('myfunctions.php');

theBackup();

?>

主脚本是这样的。虽然在我手动运行它时它可以正常工作,但它不会与cron一起运行。

 <?php 
/ *
*备份数据库的脚本
*
*
* /

函数getAllFiles($ directory,$ recursive = false){
$ result = array();
$ handle = opendir($ directory);
while($ datei = readdir($ handle))
{
if(($ datei!='。')&&($ datei!='..'))
{
$ file = $ directory。$ datei;
if(is_dir($ file)){
if($ recursive){
$ result = array_merge($ result,getAllFiles($ file。'/'));
}
} else {
$ result [] = $ file;
}
}
}
closedir($ handle);
return $ result;
}

函数getOldestTimestamp($ directory,$ recursive = true,$ display ='file'){
$ allFiles = getAllFiles($ directory,$ recursive);
$ highestKnown = time();
$ highestFile ='';
foreach($ allFiles as $ val){
$ currentValue = filemtime($ val);
$ currentFile = $ val;
if($ currentValue< $ highestKnown){
$ highestKnown = $ currentValue;
$ highestFile = $ currentFile;
}
}
if($ display =='file'){
return $ highestFile;
} else {
return $ highestKnown;
}
}


函数theBackup(){

$ sendfrom =系统备份< admin@domain.com> ;

$ headers ='Admin< admin@domain.com>'。 \\\
;
$ headers。='MIME-Version:1.0'。 \\\
;
$ headers。='Content-type:text / html; charset = iso-8859-1'。 \\\
;

$ filename = getOldestTimestamp('./ app / db /',true,'file');
$ filename = str_replace(./ app / db /,,$ filename);

$ backupfile ='/var/www/app/db/'.$filename;
$ handle = fopen($ backupfile,'w')或die('Can not open file:'。

$ dbhost =localhost;
$ dbuser =user;
$ dbpass =password;
$ dbname =db;

if(system(mysqldump -h $ dbhost -u $ dbuser -p $ dbpass $ dbname> $ backupfile)== false){
mail('email @ yahoo。 com','我的备份','备份成功完成',$ headers);

} else {
mail('email@yahoo.com','我的备份','备份没有成功完成,请检查文件/文件夹

permission',$ headers);

}
}
?>

上面的代码中缺少什么?就像我说的,当我从浏览器运行mybackup.php,它的工作完美,但不是通过cron。



任何帮助将非常感激。 b $ b

解决方案

我想你需要完整的路径到你的include,你说:

  include('myfunctions.php'); 

应该像

  include('/ var / www / myfunctions.php'); 

或任何其他地方。
也检查你的日志,看看你得到什么错误消息


I'm on Ubuntu and have a small backup script I've been trying to run. Unfortunately, it's not executing the backup. I've included the two PHP scripts here in case there's something I'm missing.

First, here's how how my crontab looks like

*/30 * * * * /usr/bin/php /var/www/mybackup.php

The cron above supposed to call this script: mybackup.php

 <?php
include('myfunctions.php');

   theBackup();

?>

The main script is this. Although it works perfectly when I manually run it, but it does not run with cron.

<?php
/*
 * Script to back up the database
 * 
 *
*/

function getAllFiles($directory, $recursive = false) {
     $result = array();
     $handle =  opendir($directory);
     while ($datei = readdir($handle))
     {
          if (($datei != '.') && ($datei != '..'))
          {
               $file = $directory.$datei;
               if (is_dir($file)) {
                    if ($recursive) {
                         $result = array_merge($result, getAllFiles($file.'/'));
                    }
               } else {
                    $result[] = $file;
               }
          }
     }
     closedir($handle);
     return $result;
}

function getOldestTimestamp($directory, $recursive = true, $display ='file') {
     $allFiles = getAllFiles($directory, $recursive);
     $highestKnown = time();
     $highestFile = '';
     foreach ($allFiles as $val) {
          $currentValue = filemtime($val);
          $currentFile = $val;
          if ($currentValue < $highestKnown){
                $highestKnown = $currentValue;
                $highestFile = $currentFile;
          }
     }
    if($display=='file'){
        return $highestFile;
    } else {
        return $highestKnown;
    }
}


function theBackup(){

$sendfrom = "System Backup <admin@domain.com>";

$headers = 'Admin <admin@domain.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";

$filename = getOldestTimestamp('./app/db/',true,'file');
$filename = str_replace("./app/db/", "", $filename );

$backupfile = '/var/www/app/db/'.$filename;
$handle  = fopen($backupfile, 'w') or die('Cannot open file:  '.$backupfile); 

$dbhost  = "localhost";  
$dbuser  = "user";
$dbpass  = "password";
$dbname  = "db";

if(system("mysqldump -h $dbhost -u $dbuser  -p$dbpass  $dbname  > $backupfile") == false){
    mail('email@yahoo.com','My Backup','Back Up successfully completed',$headers );

  }else {
    mail('email@yahoo.com','My Backup','Back Up did NOT complete successfully please check the file/folder 

permission',$headers );

   }   
 }
?> 

Is there anything I'm missing from the code above? Like I said, when I run mybackup.php from the browser, it works perfectly, but not through cron.

Any help would be highly appreciated.

解决方案

I think you'll need the full path to your include, where you say:

include('myfunctions.php');

should be like

include('/var/www/myfunctions.php');

or wherever that is. Also check you logs to see what error messages you are getting

这篇关于通过Cron作业运行PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆