Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
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.
Turn Your Knowledge into Earnings.
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"; } REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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.
current working directory
~/
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.
myscript.php
require 'config.php';
/myscript/
One of the easiest ways to remedy this is to change your current working directory to the directory containing your script files.
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';
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.
/var/log/syslog
Add the shebang header and make your initial script executable from the commandline. This will provide the functionality for a convenient log file redirection.
Cron : */15 * * * * /myscript/myscript.php >> /myscript/script.log 2>> myscript.err
#!/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.
chmod +x
/myscript/myscript.php
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.
>>
>
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.