29if (!defined(
'NOTOKENRENEWAL')) {
30 define(
'NOTOKENRENEWAL',
'1');
32if (!defined(
'NOREQUIREMENU')) {
33 define(
'NOREQUIREMENU',
'1');
35if (!defined(
'NOREQUIREHTML')) {
36 define(
'NOREQUIREHTML',
'1');
38if (!defined(
'NOREQUIREAJAX')) {
39 define(
'NOREQUIREAJAX',
'1');
41if (!defined(
'NOLOGIN')) {
42 define(
'NOLOGIN',
'1');
44if (!defined(
'NOSESSION')) {
45 define(
'NOSESSION',
'1');
49if (!defined(
'USESUFFIXINLOG')) {
50 define(
'USESUFFIXINLOG',
'_cron');
53$sapi_type = php_sapi_name();
54$script_file = basename(__FILE__);
58if (substr($sapi_type, 0, 3) ==
'cgi') {
59 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
63require_once $path.
"../../htdocs/master.inc.php";
64require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
65require_once DOL_DOCUMENT_ROOT.
"/cron/class/cronjob.class.php";
66require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
77if (!isset($argv[1]) || !$argv[1]) {
78 usage($path, $script_file);
83if (!isset($argv[2]) || !$argv[2]) {
84 usage($path, $script_file);
91$version = DOL_VERSION;
94$hookmanager->initHooks(array(
'cli'));
105print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" - userlogin=".$userlogin.
" - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
108$ini_path = php_ini_loaded_file();
109print
'TZ server = '.getServerTimeZoneString().
" - set in PHP ini ".$ini_path.
"\n";
112if (!isModEnabled(
'cron')) {
113 print
"Error: module Scheduled jobs (cron) not activated\n";
119 print
"Error: securitykey provided ".substr($key, 0, 5).
"... does not match securitykey in setup.\n";
123if (!empty($dolibarr_main_db_readonly)) {
124 print
"Error: instance in read-only mode\n";
129if ($userlogin ==
'firstadmin') {
130 $sql =
'SELECT login, entity from '.MAIN_DB_PREFIX.
'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
131 $resql = $db->query($sql);
133 $obj = $db->fetch_object($resql);
135 $userlogin = $obj->login;
136 echo
"First admin user found is login '".$userlogin.
"', entity ".$obj->entity.
"\n";
144$user =
new User($db);
145$result = $user->fetch(
'', $userlogin,
'', 1);
147 echo
"User Error: ".$user->error;
148 dol_syslog(
"cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
151 if (empty($user->id)) {
152 echo
"User login: ".$userlogin.
" does not exists\n";
153 dol_syslog(
"User login:".$userlogin.
" does not exists", LOG_ERR);
163if ($langs->getDefaultLang() != $langcode) {
164 $langs->setDefaultLang($langcode);
165 $langs->tab_translate = array();
168$langs->loadLangs(array(
'main',
'admin',
'cron',
'dict'));
172if (isset($argv[3]) && $argv[3]) {
176if (isset($argv[4]) && $argv[4] ==
'--force') {
185 if (!is_numeric(
$id)) {
186 echo
"Error: Bad value for parameter job id\n";
187 dol_syslog(
"cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
190 $filter[
't.rowid'] =
$id;
195$sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob set processing = 0";
196$sql .=
" WHERE processing = 1";
197$sql .=
" AND datelastrun <= '".$db->idate(
dol_now() -
getDolGlobalInt(
'CRON_MAX_DELAY_FOR_JOBS', 24) * 3600,
'gmt').
"'";
198$sql .=
" AND datelastresult IS NULL";
201dol_syslog(
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter), LOG_DEBUG);
202echo
"cron_run_jobs.php search qualified job using filter: ".json_encode($filter).
"\n";
204$result =
$object->fetchAll(
'ASC,ASC,ASC',
't.priority,t.entity,t.rowid', 0, 0, 1, $filter, ($forcequalified ? -1 : 0));
206 echo
"Error: ".$object->error;
213$nbofjobs = count(
$object->lines);
214$nbofjobslaunchedok = 0;
215$nbofjobslaunchedko = 0;
221 foreach (
$object->lines as $line) {
222 dol_syslog(
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label, LOG_DEBUG);
223 echo
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label;
226 if ((empty($line->entity) ? 1 : $line->entity) !=
$conf->entity) {
227 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);
228 echo
" -> we change entity so we reload mysoc, langs, user and conf";
230 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
231 $conf->setValues($db);
232 $mysoc->setMysoc(
$conf);
235 if (
$conf->entity != $user->entity) {
236 $result = $user->fetch(
'', $userlogin,
'', 1);
238 echo
"\nUser Error: ".$user->error.
"\n";
239 dol_syslog(
"cron_run_jobs.php: User Error:".$user->error, LOG_ERR);
243 echo
"\nUser login: ".$userlogin.
" does not exist for entity ".
$conf->entity.
"\n";
244 dol_syslog(
"cron_run_jobs.php: User login: ".$userlogin.
" does not exist", LOG_ERR);
253 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
254 $langcode = $user->conf->MAIN_LANG_DEFAULT;
256 if ($langs->getDefaultLang() != $langcode) {
257 $langs->setDefaultLang($langcode);
258 $langs->tab_translate = array();
259 $langs->loadLangs(array(
'main',
'admin',
'cron',
'dict'));
268 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
269 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
270 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
271 if ($forcequalified || ($datenextrunok && $datestartok && $dateendok)) {
277 $result = $cronjob->fetch($line->id);
279 echo
" - Error cronjobid: ".$line->id.
" cronjob->fetch: ".$cronjob->error.
"\n";
280 echo
"Failed to fetch job ".$line->id.
"\n";
281 dol_syslog(
"cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
285 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
286 $reshook = $hookmanager->executeHooks(
'beforeRunCronJob', $parameters, $object);
289 $result = $cronjob->run_jobs($userlogin);
291 echo
" - Error cronjobid: ".$line->id.
" cronjob->run_job: ".$cronjob->error.
"\n";
292 echo
"At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
293 echo
"You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
294 dol_syslog(
"cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
295 $nbofjobslaunchedko++;
296 $resultstring =
'KO';
298 $nbofjobslaunchedok++;
299 $resultstring =
'OK';
302 echo
"Result of run_jobs ".$resultstring.
" result = ".$result;
305 $result = $cronjob->reprogram_jobs($userlogin, $now);
307 echo
" - Error cronjobid: ".$line->id.
" cronjob->reprogram_job: ".$cronjob->error.
"\n";
308 echo
"Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
309 dol_syslog(
"cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
313 echo
" - Job re-scheduled\n";
315 $parameters = array(
'cronjob' => $cronjob,
'line' => $line);
316 $reshook = $hookmanager->executeHooks(
'afterRunCronJob', $parameters, $object);
318 echo
" - not qualified (datenextrunok=".($datenextrunok ?: 0).
", datestartok=".($datestartok ?: 0).
", dateendok=".($dateendok ?: 0).
")\n";
326 echo
"cron_run_jobs.php no qualified job found\n";
331print
"***** ".$script_file.
" end - ".
dol_print_date($now,
'dayhourrfc',
'gmt').
" - ".gethostname().
" *****\n";
333if ($nbofjobslaunchedko) {
348 print
"Usage: ".$script_file.
" securitykey userlogin|'firstadmin' [cronjobid] [--force]\n";
349 print
"The script return 0 when everything worked successfully.\n";
351 print
"On Linux system, you can have cron jobs ran automatically by adding an entry into cron.\n";
352 print
"For example, to run pending tasks each day at 3:30, you can add this line:\n";
353 print
"30 3 * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
354 print
"For example, to run pending tasks every 5mn, you can add this line:\n";
355 print
"*/5 * * * * ".$path.$script_file.
" securitykey userlogin > ".DOL_DATA_ROOT.
"/".$script_file.
".log\n";
357 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";
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage Dolibarr users.
usage($path, $script_file)
script cron usage
verifCond($strToEvaluate, $onlysimplestring='1')
Verify if condition in string is ok or not.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
getDolUserString($key, $default='', $tmpuser=null)
Return Dolibarr user constant string value.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_clone($object, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...