Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Frank Walter -
回帖数:8
Hello

I have on my Desktop Computer Ubuntu 22.04, and Moodle 3.11.14 with Apache 2.4 and PHP 8.0 for some development, a client has the same versions.

Now I just tried to activate cron, with:

sudo crontab -u www-data -e

put in:

* * * * * /usr/bin/php /home/f/htdocs/moodle3/admin/cli/cron.php >/dev/null

and nothing is seen in the log (https://localhost/moodle3/admin/tasklogs.php).

When I run the php command manually, it works, fills the log in moodle with entries. But a refresh after some minutes (or hours) doesnt show new entries, so the cron command is not running successfully, I guess. I sort the log for start time desc, so new entries should show.

The cron itself is running, in syslog there are entries for it, each minute, without error:

...
May  5 10:48:01 franc-comp-ubuntu CRON[184746]: (www-data) CMD (/usr/bin/php /home/f/htdocs/moodle3/admin/cli/cron.php >/dev/null)
May  5 10:49:01 franc-comp-ubuntu CRON[184785]: (www-data) CMD (/usr/bin/php /home/f/htdocs/moodle3/admin/cli/cron.php >/dev/null)
May  5 10:50:01 franc-comp-ubuntu CRON[184836]: (www-data) CMD (/usr/bin/php /home/f/htdocs/moodle3/admin/cli/cron.php >/dev/null)
...

The moodle and moodledata folders are with owner www-data:www-data.
Could anybody give me a hint, what could be the problem?

Thank, frank

回复Frank Walter

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
You could take out the '>/dev/null' and read the mailbox www-data, '# mailx -f /var/mail/www-data'.
回复Visvanath Ratnaweera

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Frank Walter -
OK then, installed mailutils (and postfix, for local use only) but still nothing:

#mailx -f /var/mail/www-data
"/var/mail/www-data": 0 Nachrichten

回复Frank Walter

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
Na, cron.php wird angestossen und bricht irrgendwie ab. Was sind die  Ausgaben von:

$ sudo -u www-data php /home/f/htdocs/moodle3/admin/cli/cron.php

$ which php

$ `which php` --version
回复Visvanath Ratnaweera

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Frank Walter -
Sonderbar:

#sudo -u www-data /usr/bin/php /home/f/htdocs/moodle3/admin/cli/cron.php
Could not open input file: /home/f/htdocs/moodle3/admin/cli/cron.php
PHP (CLI, im Apache aber auch) version ist 7.4, ist in /usr/bin/php

cron.php kört aber durchaus dem www-data:

#ls -al /home/f/htdocs/moodle3/admin/cli/cron.php
-rw-rw-r-- 1 www-data www-data 5,0K Jan 14 07:16 /home/f/htdocs/moodle3/admin/cli/cron.php

Ich hab die CLI-PHP Version mal auf 8.0 gestellt, ändert aber nichts.
回复Frank Walter

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Visvanath Ratnaweera -
Particularly helpful Moodlers的头像 Translators的头像
That is odd. (Back to English ;) )

Can root run PHP CLI, e.g.
$ sudo -i
[sudo] Password for ...:
# /usr/bin/php /home/f/htdocs/moodle3/admin/cli.php
Or, can www-data run other PHP CLI, e.g.
# sudo -u www-data /usr/bin/php /home/f/htdocs/moodle3/admin/cli/reset_password.php
Is the /home partition mounted a special way? Does '# mount' have a line /home? If so, what are its params, e.g; (rw,nosuid,nodev,noexec,relatime)?
回复Visvanath Ratnaweera

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Frank Walter -
No, you are right, www-data seems to be not allowed to run any php script.
root does, but not www-root.

Ahh, now I have it: I used as path the symlinked path, (/home/f/htdocs/moodle3) not the original, which is: /var/www/htdocs/moodle3
I have a symlink from my home dir to the /var/www/htdocs dir:

#ls -al htdocs
lrwxrwxrwx 1 root root 15 Mär  9 11:22 htdocs -> /var/www/htdocs
Now I put just the real path into the command and now it works:

sudo -u www-data php /var/www/htdocs/moodle3/admin/cli/cron.php
I wonder what is the difference.
Thank for yours help!!!
回复Frank Walter

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Ken Task -
Particularly helpful Moodlers的头像
touch /var/log/moodlecron.log
Then redirect the cron run with > /var/log/moodlecron.log

Can watch it real time with:
tail -f /var/log/moodlecron.log

The log file is overwritten each run so it will not grow large nor need to be rotated like other log files.

Mr. V asked for 'which php' and then using the path seen in which
/path/to/php -v
to see if your system is finding php-cli - the right version - 8.0.
I'll add one more ... ls -l /path/to/php to see if that is really a simlink pointed to wrong version of PHP.

Also, kinda a weird path ... /home/f/htdocs/moodle3/ but that might be 'normal' for a development system.

'SoS', Ken

回复Ken Task

Re: Moodle 3.11 - cron.php on Ubuntu 22.04 not running

Frank Walter -
I have a switch to change the PHP CLI version, I have it in my .bashrc (in .bash_functions, actually, linked from .bashrc):

    # 2023-03-23: die CLI PHP Version umschalten, siehe:
# https://gridpane.com/kb/setting-the-cli-php-version/
phpcliv () {
PPCV='/root/preferred_php_cli.version'
if [ $# -lt 1 ]
then
CAT='/usr/bin/cat'
PHPCLIVERSION="$($CAT $PPCV)"
echo "
phpcliv (aus bashfu)
Aktuelle PHP-CLI-Version (nicht die im Apache!) mit php -v:
"
php -v
echo "
Inhalt von ${PPCV}: $PHPCLIVERSION

phpcliv schaltet die PHP CLI Version um. PHP 7.4 (Kommando hier: phpcliv 74), PHP 8.0, PHP 8.1, PHP 8.2 stehen zur Verfügung.
Aufruf: phpcliv
= 74, 80, 81 oder 82
Beispiel: phpcliv 81
Die PHP CLI Version wird dann in /root/preferred_php_cli.version geschrieben.
Ohne Parameter wird nur diese Hilfe angezeigt und beendet.
"
elif [ "$1" == "74" ]
then
echo "
Schaltet auf PHP CLI 7.4 um.
"
update-alternatives --set php /usr/bin/php7.4
echo "7.4" > /root/preferred_php_cli.version
elif [ "$1" == "80" ]
then
echo "
Schaltet auf PHP CLI 8.0 um.
"
update-alternatives --set php /usr/bin/php8.0
echo "8.0" > /root/preferred_php_cli.version
elif [ "$1" == "81" ]
then
echo "
Schaltet auf PHP CLI 8.1 um.
"
update-alternatives --set php /usr/bin/php8.1
echo "8.1" > /root/preferred_php_cli.version
elif [ "$1" == "82" ]
then
echo "
Schaltet auf PHP CLI 8.2 um.
"
update-alternatives --set php /usr/bin/php8.2
echo "8.2" > /root/preferred_php_cli.version
fi
}
with that I easily switched from 8.2 to 8.0 which made cron work on my server (where cron did not run neither).
So now all is good. Thanks.