Home > Enterprise >  Cron-Job does not execute the php phar
Cron-Job does not execute the php phar

Time:07-04

I am totally helpless and sheer despair.

After I created the following crontab:

SHELL=/bin/bash
PHAR="/usr/bin/php -f /home/heimathafen/customfiles/scripts/7d2d.phar"
# m h  dom mon dow   command
*/15 * * * * ${PHAR} maintenance
*/5 * * * * ${PHAR} monitor
#50 3,7,11,15,19,23 * * * ${SDTD} reboot
#0 * * * * ${PHAR} backup
#55 23 * * 6 ${PHAR} creategifts
* * * * * ${PHAR} gameevents 0
* * * * * ( sleep 10; ${PHAR} gameevents 1)
* * * * * ( sleep 20; ${PHAR} gameevents 2)
* * * * * ( sleep 30; ${PHAR} gameevents 3)
* * * * * ( sleep 40; ${PHAR} gameevents 4)
* * * * * ( sleep 50; ${PHAR} gameevents 5)

I found that none of these jobs are executed. When I run the command on the CLI:

/usr/bin/php -f /home/homeport/customfiles/scripts/7d2d.phar monitor

everything works fine. No matter if I put an sh -c "..." around it or just run it like that.

The phar file has the right permissions ( chmod a x ) and the path is correct. Also the crontab is in the usercontext (homeport).

What am I doing wrong?

CodePudding user response:

*/15 * * * doesn't look like a valid interval.

Assuming that you want to execute your command at every 15th minute, you should also specify the day of the week.

Crontab entry structure:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

This might be useful: https://crontab.guru/

CodePudding user response:

I found it and would love to whip myself. After countless debugs I came across this line:

define('SERVER_USERNAME', trim(getenv('USER')));

Of course, this works beautifully if you don't run it with cron, because $USER is empty there.

I replaced it with:

define('SERVER_USERNAME', trim(exec('whoami')));

and lo and behold. Everything works.

  • Related