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.
manpreet
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 :
Second cron :
Here's the first script: