PHP script failing with cron

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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} 
\n"
; print "Type de valeur {$channel->Unit}
\n"
; print "$date
\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}
\n"
; } else { print "Temperature
\n"
; $valeur = $channel->Value * 0.10; $bdd->exec("UPDATE MesureCapteurTemp SET Mesure15='$valeur' WHERE TagCapteur ='$channel->Tag'"); print "Valeur: {$valeur}
\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}
\n"
; }
0 views
0 shares
profilepic.png
manpreet 2 years 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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.