Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

PHP script failing with cron

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Sophia Chang

User

( 6 months ago )

 

I want to use cron with 2 php scripts.

When I run these scripts manually with php /soncourt/index.php (for example) on my destination server, it works, but when I want to use CRON it fails

First cron :

*/15 * * * * /maquette/index.php >> /maquette/index.log 2>> index.err

Second cron :

*/15 * * * * /soncourt/index.php >> /soncourt/index.log 2>> index.err

Here's the first script:

<?php
///////////////////////////////////////////// Preparation \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Recuperation des valeurs obligatoires
//Preparation de la DATE
date_default_timezone_set('Europe/Paris'); // Mise a la bonne heure (Timezone: Europe/Paris)
chdir("/maquette");
$date   = date('Y-m-d H:i');
$minute = date('i');
require 'config.php';
//$minute = '15';
// Test de connection a la base de donnes 
try {
    $bdd = new PDO('mysql:host=localhost;dbname=maquette;charset=utf8', DB_USER, DB_PASSWORD);
}
catch (Exception $e) {
    die('Erreur : ' . $e->getMessage());
}
//Telechargement et Chargement du fichier channels
file_put_contents("channels.xml", fopen('http://193.54.197.212/channels.xml', 'r'));
$Capteurs = simplexml_load_file("channels.xml");
///////////////////////////////////////////// Enregistrement des valeurs  \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Insertion des Capteurs
// REMOTE CHANNELS
foreach ($Capteurs->RemoteChannels->Channel as $channel) {
    print "Nom du capteur: {$channel->Tag} <br />\n";
    print "Type de valeur {$channel->Unit}<br />\n";
    print "$date <br />\n";
    //Si 15 minutes
    if ($minute == '15') {
        if ($channel->Unit == 'C' || $channel->Unit == '°C') {
            if ($channel->Tag == 'M4IST') {
                $valeur = $channel->Value * 0.01;
                $bdd->exec("UPDATE MesureCapteurTemp SET Mesure15='$valeur' WHERE TagCapteur ='$channel->Tag'");
                print "Valeur: {$valeur} <br />\n";
            } else {
                print "Temperature <br /> \n";
                $valeur = $channel->Value * 0.10;
                $bdd->exec("UPDATE MesureCapteurTemp SET Mesure15='$valeur' WHERE TagCapteur ='$channel->Tag'");
                print "Valeur: {$valeur} <br />\n";
            }
        }
        if ($channel->Unit == '%' || $channel->Unit == '%H') {
            if ($channel->Tag == 'M4ISH') {
                $valeur = $channel->Value * 0.01;
                $bdd->exec("UPDATE MesureCapteurTemp SET Mesure15='$valeur' WHERE TagCapteur ='$channel->Tag'");
                print "Valeur: {$valeur} <br />\n";
            } 
								 								 
							usr_profile.png
							
Sophia Chang

User

( 6 months ago )

Your problem is, as I mention, your environment. Looking at your php script and the crontab entry calling it, there isn't any reference to your changing to the directory containing dependency files. Your default current working directory is your home folder (~/). Your script folder isn't the same as the directory your cron is running.

Also your myscript.php file is using the require 'config.php'; directive, but it's not pointing to the /myscript/ folder where it's located.

One of the easiest ways to remedy this is to change your current working directory to the directory containing your script files.

Edit your file

Change from:

<?php
///////////////////////////////////////////// Preparation \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Recuperation des valeurs obligatoires
//Preparation de la DATE
date_default_timezone_set('Europe/Paris'); // Mise a la bonne heure (Timezone: Europe/Paris)
$date   = date('Y-m-d H:i');
$minute = date('i');
require 'config.php';

Change to:

<?php
///////////////////////////////////////////// Preparation \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Recuperation des valeurs obligatoires
//Preparation de la DATE
chdir("/myscript");
date_default_timezone_set('Europe/Paris'); // Mise a la bonne heure (Timezone: Europe/Paris)
$date   = date('Y-m-d H:i');
$minute = date('i');
require 'config.php';

You mentioned debug. I don't know the debug method you are using, but you may find a convenient way of debugging your cron scripts is to redirect it to a log file. By default, the output goes to your /var/log/syslog. Redirecting the output to a preferred file will give you a cleaner log.

Add the shebang header and make your initial script executable from the commandline. This will provide the functionality for a convenient log file redirection.

Considerations for your crontab entry and script file

Crontab entry:

Cron : */15 * * * * /myscript/myscript.php >> /myscript/script.log 2>> myscript.err

PHP Script:

#!/usr/bin/php
<?php
///////////////////////////////////////////// Preparation \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Recuperation des valeurs obligatoires
//Preparation de la DATE
chdir("/myscript");
date_default_timezone_set('Europe/Paris'); // Mise a la bonne heure (Timezone: Europe/Paris)
$date   = date('Y-m-d H:i');
$minute = date('i');
require 'config.php';

Don't forget to chmod +x your /myscript/myscript.php.

Final Resolution

The issue was resolved in a chat discussion (see the comments). The main problem(s) (and most likely the ultimate problem) was the location of files. Running the application from the commandline and addressing the errors resolved the issues.

The problem with two php scripts in cron was resolved by studying the log (the standard output) was showing just one instance of the two scripts running. That was due to confusion of the >> versus the > output redirection. The single greater than symbol was overwriting the previous instance. The >>redirect resolved that part.

what's your interest