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";
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";
199if (function_exists(
'posix_kill') && function_exists(
'posix_get_last_error')) {
200 $sql =
"SELECT rowid, pid";
201 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob";
202 $sql .=
" WHERE processing = 1";
203 $sql .=
" AND datelastresult IS NULL";
204 $sql .=
" AND pid IS NOT NULL";
205 $resql =
$db->query($sql);
207 while ($obj =
$db->fetch_object($resql)) {
208 $pid = (int) $obj->pid;
213 $isalive = @posix_kill($pid, 0);
215 $errno = posix_get_last_error();
218 $msg =
'Cron job unlocked: stale PID '.$pid;
220 $sqlu =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob";
221 $sqlu .=
" SET processing = 0, pid = NULL, datelastresult = '".$db->idate($nowcleanup).
"', lastresult = '-1', lastoutput = '".
$db->escape($msg).
"'";
222 $sqlu .=
" WHERE rowid = ".((int) $obj->rowid).
" AND processing = 1 AND pid = ".$pid.
" AND datelastresult IS NULL";
225 dol_syslog(
"cron_run_jobs.php unlocked stuck job id=".$obj->rowid.
" (stale pid ".$pid.
")", LOG_WARNING);
226 echo
"cron_run_jobs.php unlocked stuck job id=".$obj->rowid.
" (stale pid ".$pid.
")\n";
234dol_syslog(
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter), LOG_DEBUG);
235echo
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter).
"\n";
237$result =
$object->fetchAll(
'ASC,ASC,ASC',
't.entity,t.priority,t.rowid', 0, 0, 1, $filter, ($forcequalified ? -1 : 0));
239 echo
"Error: ".$object->error;
246$nbofjobs = count(
$object->lines);
247$nbofjobslaunchedok = 0;
248$nbofjobslaunchedko = 0;
254 foreach (
$object->lines as $line) {
256 '@phan-var-force Cronjob $line';
258 dol_syslog(
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label, LOG_DEBUG);
259 echo
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label;
262 if ((empty($line->entity) ? 1 : $line->entity) !=
$conf->entity) {
263 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);
264 echo
" -> we change entity so we reload mysoc, langs, user and conf";
268 if (($line->entity ?: 1) !=
$conf->entity && isset($extrafields) && !empty($extrafields->attributes)) {
271 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
277 print
"Canceled - Module Scheduled jobs (cron) not activated into entity ".$line->entity.
"\n";
278 dol_syslog(
"cron_run_jobs.php: Canceled - Module Scheduled jobs (cron) not activated into entity ".$line->entity, LOG_INFO);
283 if (
$conf->entity != $user->entity) {
284 $result = $user->fetch(0, $userlogin,
'', 1);
286 echo
"\nUser Error: ".$user->error.
"\n";
287 dol_syslog(
"cron_run_jobs.php: User Error:".$user->error, LOG_ERR);
291 echo
"\nUser login: ".$userlogin.
" does not exist for entity ".
$conf->entity.
"\n";
292 dol_syslog(
"cron_run_jobs.php: User login: ".$userlogin.
" does not exist", LOG_ERR);
296 $user->loadRights(
'', 1);
301 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
302 $langcode = $user->conf->MAIN_LANG_DEFAULT;
304 if ($langs->getDefaultLang() != $langcode) {
305 $langs->setDefaultLang($langcode);
306 $langs->tab_translate = array();
307 $langs->loadLangs(array(
'main',
'admin',
'cron',
'dict'));
316 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
317 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
318 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
319 if ($forcequalified || ($datenextrunok && $datestartok && $dateendok)) {
325 $result = $cronjob->fetch($line->id);
327 echo
" - Error cronjobid: ".$line->id.
" cronjob->fetch: ".$cronjob->error.
"\n";
328 echo
"Failed to fetch job ".$line->id.
"\n";
329 dol_syslog(
"cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
333 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
334 $reshook = $hookmanager->executeHooks(
'beforeRunCronJob', $parameters, $object);
337 $result = $cronjob->run_jobs($userlogin);
339 echo
" - Error cronjobid: ".$line->id.
" cronjob->run_job: ".$cronjob->error.
"\n";
340 echo
"At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
341 echo
"You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
342 dol_syslog(
"cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
343 $nbofjobslaunchedko++;
344 $resultstring =
'KO';
346 $nbofjobslaunchedok++;
347 $resultstring =
'OK';
351 echo
"Result of run_jobs ".$resultstring.
" result = ".$result;
354 $result = $cronjob->reprogram_jobs($userlogin, $now);
356 echo
" - Error cronjobid: ".$line->id.
" cronjob->reprogram_job: ".$cronjob->error.
"\n";
357 echo
"Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
358 dol_syslog(
"cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
362 echo
" - Job re-scheduled\n";
364 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
365 $reshook = $hookmanager->executeHooks(
'afterRunCronJob', $parameters, $object);
367 echo
" - not qualified (datenextrunok=".($datenextrunok ?: 0).
", datestartok=".($datestartok ?: 0).
", dateendok=".($dateendok ?: 0).
")\n";
375 echo
"cron_run_jobs.php no qualified job found\n";
380print
"***** ".$script_file.
" end - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
382if ($nbofjobslaunchedko) {
400 print
"***** ".$script_file.
" (".$version.
") pid=".dol_getmypid().
" - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
401 print
"Usage: ".$script_file.
" securitykey userlogin|'firstadmin' [cronjobid] [--force]\n";
403 print
"The script return 0 when everything worked successfully.\n";
405 print
"On Linux system, you can have cron jobs ran automatically by adding an entry into cron file.\n";
406 print
"For example, to run this script each day at 3:30, you can add this line:\n";
407 print
"30 3 * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
408 print
"For example, to run this script every 5mn, you can add this line:\n";
409 print
"*/5 * * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
411 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.