Cron not working (Ubuntu 20.04)

Cron not working (Ubuntu 20.04)

by Javier Tejera -
Number of replies: 20

I am using Moodle 3.9 in a Digital Ocean's Ubuntu 20.04 server.

I am trying to set up the cron every minute, but I am not being successful. I used the web based cron script http://myurl.com/moodle/admin/cron.php and it worked fine.

However, I use the command crontab -u www-data -e as root, and then I add * * * * * /usr/bin/php /opt/moodle/admin/cli/cron.php to the last line and saved, but it is not running as it should be.

Any advice? Thanks in advance.

Average of ratings: -
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Try running the cron script manually: at the command line try the command:

# sudo -u www-data /usr/bin/php /opt/moodle/admin/cli/cron.php

(If you need to, switch to root first so you get the # prompt with sudo su -).

Do you get output that looks correct or an error?

In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
Possibly file permission related. Try:

sudo chmod -R 655 /opt/moodle

Failing that, what happens when you run this in shell?

sudo -u www-data /usr/bin/php /opt/moodle/admin/cli/cron.php
In reply to Matt T

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
Hi Matt and Leon,
I got an error:



It does not make sense to me as the file is there:


In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

The error is saying it cannot open /opt/moodle/admin/cli/../../config.php, that is: /opt/moodle/config.php. Does this file exist?

If it doesn't then maybe /opt/moodle isn't the Moodle source code folder being used.

If it does then maybe your web server isn't running PHP scripts as www-data and www-data doesn't have access to see this file. Where is your site's Moodledata? This is specified in $CFG->dataroot in config.php. Find out which user owns muc/config.php in Moodledata (this is a different config.php to the one above!). For example if $CFG->dataroot is '/var/moodledata' run:

 # ls -l /var/moodledata/muc/config.php

The owner of this file is probably the user to run cron as, with sudo -u user manually, or crontab -u user -e for the crontab.

In reply to Leon Stringer

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -

Thanks a lot for your time, I really appreciate it and apologies for my lack of knowledge...!

So, that's $CFG->dataroot, which is 'var/moodledata':


That's muc/config.php:


Finally, I have added * * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php > /dev/null to crontab -u www-data -e.

Not working yet.

Edit ls -l /opt/moodle:


In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

The file we need to see in ls -lh /opt/moodle is config.php which isn't in that screenshot. Can you share that?

Since muc/config.php is owned by www-data, presumably that is the correct user. So I'm a bit puzzled as to how the web site could work as that user but the cron doesn't.

In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
Can you run ls -l /opt/moodle and screenshot it?
In reply to Matt T

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
Hey Matt, see the previous comment with screenshot attached.
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
Per Leon's message above, we can't see the config file in that screenshot. If you're on a server console with a limited screen resolution, pipe it through less and scroll up with the arrow keys.

ls -lh /opt/moodle | less
In reply to Matt T

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
Oh sorry, haven't spotted that limitation as I am new to this thing.

Unfortunately piping it through less and scrolling up with the arrow keys does not work. It seems that it is Digital Ocean's web console limitation. I am trying to access via my terminal but I am stuck with ssh: connect to host [myIP] port 22: Operation timed out.

Hope to fix it soon and will share screenshot.
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
Alternatively, in that case

ls -lh /opt/moodle | grep -i config.php
In reply to Matt T

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
Had to sudo ufw disable and now I can access via SSH.
So, there is no config.php file after running ls -l /opt/moodle...


In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

If there's no config.php then that's not your Moodle source code directory.

From this reply your Moodle source code directory is /var/www/html/moodle. So presumably the command for your cron should be: /usr/bin/php /var/www/html/moodle/admin/cli/cron.php.

In reply to Leon Stringer

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
Hey Leon, yes, I was using the incorrect path (I am a true idiot!). The config.php file is in /var/www/html/moodle.

Anyway, have added the new script using crontab -u www-data -e and restarted apache, so Moodle's notifications say that cron has been ran 20+ hours ago, so it seems that does not work.

As root I've  sudo -u www-data /usr/bin/php /var/www/html/moodle/admin/cli/cron.php and the message !!! error/generalexceptionmessage !!! is displayed.

Have also changed permissions using sudo chmod -R 655 /var/www/html/moodle as per this reply and tried again with the same result.

I definitely need to learn more about Ubuntu...
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

In Moodle enable debugging under Site administration ▸ Development ▸ Debugging:

Debug messages: DEVELOPER; Display debug messages: Yes

This should show more information with the error.

If you run /usr/bin/php -v what do you get?

In reply to Leon Stringer

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
No errors are displayed.
When running /user/bin/php -v this is what I get:

PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
That output indicates that the PHP CLI is using PHP 8.0. Moodle 3.9 doesn't support PHP 8.0. You need to use PHP 7.4 (or 7.2/7.3, but PHP 7.4 is in the relevant Ubuntu 20.04 repository). 

Please run:
sudo update-alternatives --config php

and change it to PHP 7.4 using the prompt. Enter the number to the left of the option that indicates PHP 7.4.  Then run /usr/bin/php -v and check it changed, and finally try running the cron script. 

If PHP 7.4 isn't listed, that's a problem.

You should also be using PHP 7.4 for serving web requests (as opposed to CLI requests). I'd be a bit surprised if you managed to install Moodle using PHP 8.0. However, you can verify you are using PHP 7.4 for serving web requests by creating a file called 'somethingrandom.php' in /var/www/moodle with the content:

<?php phpinfo(); ?>  

(change somethingrandom to anything)

Open your site with http://yoursite/somethingrandom.php. The version number will be prominently listed. Delete this file when finished.

If it says PHP 8.0, that's also a problem. 

If you're using Apache, solve these problems with:

sudo apt install php7.4 php7.4-curl php7.4-gd php7.4-intl php7.4-json php7.4-mbstring php7.4-mysql php7.4-soap php7.4-xml php7.4-xmlrpc php7.4-zip 
sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart
sudo update-alternatives --config php
In reply to Matt T

Re: Cron not working (Ubuntu 20.04)

by Javier Tejera -
And that was it. Changed PHP version to 7.4 and now cron working fine.

Sorry for wasting everybody's time and thanks a lot for your support. It has certainly been a learning experience!
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Matt T -
Good to hear. Remember to check PHP 7.4 is serving your web requests if you haven't already. 
In reply to Javier Tejera

Re: Cron not working (Ubuntu 20.04)

by Ken Task -
Picture of Particularly helpful Moodlers

Pardon intrusion ... been following this ... and know you've 2 persons with extensive knowledge of Moodle and are getting good advice ... but that advice is only as good as description of the problem! :|

So with that in mind (I might be way off, but ....)

in one screen shot I see the root prompt.

ubuntu-s-ivcpu-1gb-1on1-01

Those usually mean something ... 1 VCPU?  1 Gig Memory?  1on1-01 ... why would Digital Ocean have 1on1-01?   Kinda looks like an old 1&1 hosting designation?


Yet you start the entire thread with

"Digital Ocean's Ubuntu 20.04 server"

https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-20-04

Screen shot listing files for moodle shows no config.php file ...
thus setup of cron will fail - 100% of the time.

How did you acquire the moodle code?
/opt/moodle is a strange place for Ubuntu's 20.04 server!!!

Web server running?
ps aux |grep apache2

IF it is, then where is document root for the apache service?

find / -name apache2.conf

should show the main config file for apache's path.

Use the path in following command:

fgrep 'DocumentRoot' /path/to/apache2.conf

Am a CentOS person myself, but have been into Ubuntu boxen ... they are quite similar since they both are to supposed to be following Linux Standards Base (LSB) when it comes to web.  Defining documentroot in the main config of the httpd/apache service is in both CentOS and Ubuntu server OS's.

'SoS', Ken