30if (!defined(
'NOTOKENRENEWAL')) {
31 define(
'NOTOKENRENEWAL',
'1');
33if (!defined(
'NOREQUIREMENU')) {
34 define(
'NOREQUIREMENU',
'1');
36if (!defined(
'NOREQUIREHTML')) {
37 define(
'NOREQUIREHTML',
'1');
39if (!defined(
'NOREQUIREAJAX')) {
40 define(
'NOREQUIREAJAX',
'1');
42if (!defined(
'NOLOGIN')) {
43 define(
'NOLOGIN',
'1');
45if (!defined(
'NOSESSION')) {
46 define(
'NOSESSION',
'1');
50if (!defined(
'USESUFFIXINLOG')) {
51 define(
'USESUFFIXINLOG',
'_cron');
54$sapi_type = php_sapi_name();
55$script_file = basename(__FILE__);
59if (substr($sapi_type, 0, 3) ==
'cgi') {
60 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
64require_once $path.
"../../htdocs/master.inc.php";
74require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
75require_once DOL_DOCUMENT_ROOT.
"/cron/class/cronjob.class.php";
76require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
79$version = DOL_VERSION;
83if (!isset($argv[1]) || !$argv[1]) {
89if (!isset($argv[2]) || !$argv[2]) {
96$hookmanager->initHooks(array(
'cli'));
107print
"***** ".$script_file.
" (".$version.
") pid=".dol_getmypid().
" - userlogin=".$userlogin.
" - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
110$ini_path = php_ini_loaded_file();
111print
'TZ server = '.getServerTimeZoneString().
" - set in PHP ini ".$ini_path.
"\n";
115 print
"Error: securitykey provided ".substr($key, 0, 5).
"... does not match securitykey in setup.\n";
119if (!empty($dolibarr_main_db_readonly)) {
120 print
"Error: instance in read-only mode\n";
125if ($userlogin ==
'firstadmin') {
126 $sql =
'SELECT login, entity FROM '.MAIN_DB_PREFIX.
'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
127 $resql = $db->query($sql);
129 $obj = $db->fetch_object($resql);
131 $userlogin = $obj->login;
132 echo
"First admin user found is login '".$userlogin.
"', entity ".$obj->entity.
"\n";
140$user =
new User($db);
141$result = $user->fetch(0, $userlogin,
'', 1);
143 echo
"User Error: ".$user->error;
144 dol_syslog(
"cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
147 if (empty($user->id)) {
148 echo
"User login: ".$userlogin.
" does not exists\n";
149 dol_syslog(
"User login:".$userlogin.
" does not exists", LOG_ERR);
159if ($langs->getDefaultLang() != $langcode) {
160 $langs->setDefaultLang($langcode);
161 $langs->tab_translate = array();
164$langs->loadLangs(array(
'main',
'admin',
'cron',
'dict'));
168if (isset($argv[3]) && $argv[3]) {
172if (isset($argv[4]) && $argv[4] ==
'--force') {
181 if (!is_numeric(
$id)) {
182 echo
"Error: Bad value for parameter job id\n";
183 dol_syslog(
"cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
186 $filter[
't.rowid'] =
$id;
191$sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob set processing = 0";
192$sql .=
" WHERE processing = 1";
193$sql .=
" AND datelastrun <= '".$db->idate(
dol_now() -
getDolGlobalInt(
'CRON_MAX_DELAY_FOR_JOBS', 24) * 3600,
'gmt').
"'";
194$sql .=
" AND datelastresult IS NULL";
197dol_syslog(
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter), LOG_DEBUG);
198echo
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter).
"\n";
200$result =
$object->fetchAll(
'ASC,ASC,ASC',
't.entity,t.priority,t.rowid', 0, 0, 1, $filter, ($forcequalified ? -1 : 0));
202 echo
"Error: ".$object->error;
209$nbofjobs = count(
$object->lines);
210$nbofjobslaunchedok = 0;
211$nbofjobslaunchedko = 0;
217 foreach (
$object->lines as $line) {
219 '@phan-var-force Cronjob $line';
221 dol_syslog(
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label, LOG_DEBUG);
222 echo
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label;
225 if ((empty($line->entity) ? 1 : $line->entity) != $conf->entity) {
226 dol_syslog(
"cron_run_jobs.php: we work on another entity conf than ".$conf->entity.
" so we reload mysoc, langs, user and conf", LOG_DEBUG);
227 echo
" -> we change entity so we reload mysoc, langs, user and conf";
231 if (($line->entity ?: 1) != $conf->entity && isset($extrafields) && !empty($extrafields->attributes)) {
234 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
235 $conf->setValues($db);
240 print
"Canceled - Module Scheduled jobs (cron) not activated into entity ".$line->entity.
"\n";
241 dol_syslog(
"cron_run_jobs.php: Canceled - Module Scheduled jobs (cron) not activated into entity ".$line->entity, LOG_INFO);
246 if ($conf->entity != $user->entity) {
247 $result = $user->fetch(0, $userlogin,
'', 1);
249 echo
"\nUser Error: ".$user->error.
"\n";
250 dol_syslog(
"cron_run_jobs.php: User Error:".$user->error, LOG_ERR);
254 echo
"\nUser login: ".$userlogin.
" does not exist for entity ".$conf->entity.
"\n";
255 dol_syslog(
"cron_run_jobs.php: User login: ".$userlogin.
" does not exist", LOG_ERR);
259 $user->loadRights(
'', 1);
264 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
265 $langcode = $user->conf->MAIN_LANG_DEFAULT;
267 if ($langs->getDefaultLang() != $langcode) {
268 $langs->setDefaultLang($langcode);
269 $langs->tab_translate = array();
270 $langs->loadLangs(array(
'main',
'admin',
'cron',
'dict'));
279 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
280 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
281 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
282 if ($forcequalified || ($datenextrunok && $datestartok && $dateendok)) {
288 $result = $cronjob->fetch($line->id);
290 echo
" - Error cronjobid: ".$line->id.
" cronjob->fetch: ".$cronjob->error.
"\n";
291 echo
"Failed to fetch job ".$line->id.
"\n";
292 dol_syslog(
"cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
296 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
297 $reshook = $hookmanager->executeHooks(
'beforeRunCronJob', $parameters, $object);
300 $result = $cronjob->run_jobs($userlogin);
302 echo
" - Error cronjobid: ".$line->id.
" cronjob->run_job: ".$cronjob->error.
"\n";
303 echo
"At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
304 echo
"You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
305 dol_syslog(
"cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
306 $nbofjobslaunchedko++;
307 $resultstring =
'KO';
309 $nbofjobslaunchedok++;
310 $resultstring =
'OK';
314 echo
"Result of run_jobs ".$resultstring.
" result = ".$result;
317 $result = $cronjob->reprogram_jobs($userlogin, $now);
319 echo
" - Error cronjobid: ".$line->id.
" cronjob->reprogram_job: ".$cronjob->error.
"\n";
320 echo
"Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
321 dol_syslog(
"cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
325 echo
" - Job re-scheduled\n";
327 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
328 $reshook = $hookmanager->executeHooks(
'afterRunCronJob', $parameters, $object);
330 echo
" - not qualified (datenextrunok=".($datenextrunok ?: 0).
", datestartok=".($datestartok ?: 0).
", dateendok=".($dateendok ?: 0).
")\n";
338 echo
"cron_run_jobs.php no qualified job found\n";
343print
"***** ".$script_file.
" end - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
345if ($nbofjobslaunchedko) {
363 print
"***** ".$script_file.
" (".$version.
") pid=".dol_getmypid().
" - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
364 print
"Usage: ".$script_file.
" securitykey userlogin|'firstadmin' [cronjobid] [--force]\n";
366 print
"The script return 0 when everything worked successfully.\n";
368 print
"On Linux system, you can have cron jobs ran automatically by adding an entry into cron file.\n";
369 print
"For example, to run this script each day at 3:30, you can add this line:\n";
370 print
"30 3 * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
371 print
"For example, to run this script every 5mn, you can add this line:\n";
372 print
"*/5 * * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
374 print
"The option --force allow to bypass the check on date of execution so job will be executed even if date is not yet reached.\n";
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage Dolibarr users.
usageCron($path, $script_file, $version)
script cron usageCron
dol_now($mode='gmt')
Return date for now.
verifCond($strToEvaluate, $onlysimplestring='1')
Verify if condition in string is ok or not.
getDolUserString($key, $default='', $tmpuser=null)
Return Dolibarr user constant string value.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.