Cron jobs doesn't work

Hello!

I have problem to setup cron jobs on my Nextcloud. I read the doc, read topics here and called my Webhoster but I can’t get out of it…

Nextcloud version : 25.0.4
Operating system and version : linux
Apache or nginx version : Apache 2.4.55
PHP version (eg, 7.4): 8.1 On cPanel I used the utility PHP Selector to upgrade to 8.1 because the native PHP version is 7.3.33. However when looking to the server info it’s still 7.3 which appears…

The issue you are facing: Error message on my Nextcloud Background jobs page telling : The task last ran for an hour ago. Something seems to be malfunctioning.
It’s suppose to run every 5 min since I selected Cron.

Is this the first time you’ve seen this error? : No

Steps to replicate it:

  1. I followed Nextcloud Doc and added this command : /5 * * * php -f /public_html/nextcloud/cron.php
    I used the cPanel utility Cron Jobs to add it. Because I don’t have permission to use -u on Terminal (my Webhoster don’t allow it…) However my Webhoster confirm me that the server call/execute the cron file correctly each 5 min :

Mar 14 19:00:01 world-211 CROND[6421]: (user) CMD (php -f public_html/nextcloud/cron.php)
Mar 14 19:05:01 world-211 CROND[10326]: (user) CMD (php -f public_html/nextcloud/cron.php)
Mar 14 19:10:02 world-211 CROND[13986]: (user) CMD (php -f public_html/nextcloud/cron.php)
Mar 14 19:15:02 world-211 CROND[17549]: (user) CMD (php -f public_html/nextcloud/cron.php)
Mar 14 19:20:01 world-211 CROND[21209]: (user) CMD (php -f public_html/nextcloud/cron.php)
Mar 14 19:25:01 world-211 CROND[25170]: (user) CMD (php -f public_html/nextcloud/cron.php)

But the Nextcloud error message is still there.

  1. My Webhoster noticed that shell_exec was in PHP disable_functions while it’s needed for Nextcloud. So I made it enable.
    Nextcloud error message is still there.
    My Webhoster told me they don’t see error message on the server side, so they stop to help me with it…

  2. I appended --define apc.enable_cli=1 to the cron command : /5 * * * php -f /public_html/nextcloud/cron.php --define apc.enable_cli=1
    Nextcloud error message is still there.

  3. I tried : /5 * * * php-cli -f /public_html/nextcloud/cron.php --define apc.enable_cli=1
    Nextcloud error message is still there.

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => ,
  'passwordsalt' => 
  'secret' => ,
  'trusted_domains' => 
  array (
    0 => my domain,
  ),
  'datadirectory' => '/home/user/nextclouddata',
  'dbtype' => 'mysql',
  'version' => '25.0.4.1',
  'overwrite.cli.url' => 'https://my domain/nextcloud',
  'dbname' => 'user_next563',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'user_next563',
  'dbpassword' =>',
  'installed' => true,
  'theme' => '',
  'loglevel' => 2,
  'maintenance' => false,
  'updater.release.channel' => 'stable',
  'memcache.local' => '\\OC\\Memcache\\APCu',
);

Output errors in nextcloud.log in /var/www/ or as admin user in top right menu, filtering for errors. Use a pastebin service if necessary.

There is no error in the log file since I selected Cron in Nextcloud…

I can not answer to all of your questions but this is wrong:

The –define apc.enable_cli=1 is not an argument of cron.php but from php.
So the command must look like this:

php --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

Furthermore, if you set the log level to 1, the cronjob log messages will be visible in the Nextcloud log

2 Likes

Thanks ernolf for your response.

I made the switch, but the error is still there on the Nexcloud background job page. And I can’t see nothing concerning cron in the Nextcloud log…?

Sometimes adding the excact php version can help. Try: php8.1

/5 * * * php8.1 --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

You also needs to enable CLI in the default php8.1 service or add that as one more --define value.

Last option to use the full path to the PHP library. Assuming you are running Ubuntu or other Debian based:

/5 * * * /usr/bin/php8.1 --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

or to specifically use the CLI version - if installed:

/5 * * * /usr/bin/php8.1-cli --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

1 Like

Thanks Kerasit for your response.

I tried different path that I found for PHP library :
/opt/alt/php81
/opt/alt/php81/usr/share/php
The Nextcloud error persist…

I don’t know how to enable CLI or to verify if it’s installed. I looked in cPanel PHP Selector and there is no options referring to CLI. How can I make it?

If you executes the command manually, then what?
And are you creating this cron under the user who also owns the nextxloud folders, typically www-data?

Yes, I’m the admin user of cPanel and Nextcloud.
My Nextcloud folders are in user/public_html/nextcloud
And also there user/nextclouddata

I don’t know how to execute the command manually… sorry I’m not very advanced in IT. However, when using AJAX on Nextcloud, cron works. And when I go on this link https://mydomain.com/nextcloud/cron.php, I notice that a cron job is executed. However the webpage displays this:
|data||
|message|Backgroundjobs are using system cron!|
|status|error|

sudo -u www-data php8.1-cli --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

However what is the output of
ls -l /public_html/nextcloud/
It will reveal which user owns the nextcloud files and folders. It is as this user you needs to do:

sudo -u [the owner of the nextcloud files and folders] php8.1-cli --define apc.enable_cli=1 -f /public_html/nextcloud/cron.php

2 Likes

I made some small progress.
In terminal when I enter : ls -l /public_html/nextcloud/
Answer is : ls: cannot access /public_html/nextcloud/: No such file or directory
However ls -l public_html/nextcloud/ gave me the content of my Nextcloud file. And the username is the same (cPanel & Nextcloud). :+1:

So I changed my cron command by removing the first slash : php --define apc.enable_cli=1 -f public_html/nextcloud/cron.php
But it’s not solving the problem… Make no different on Nextcloud side :expressionless:
I tried different path for PHP, php8.1, php-cli etc. without success.

About terminal commands

sudo -u www-data php8.1-cli --define apc.enable_cli=1 -f public_html/nextcloud/cron.php
Output is : bash: sudo: command not found

crontab -u www-data php8.1-cli --define apc.enable_cli=1 -f public_html/nextcloud/cron.php
Output is : must be privileged to use -u

Seems my Webhoster doesn’t allow me to use those commands :roll_eyes:
So I’m stuck…

Which username is it showing to be owner of …/nextcloud/data?

Actually it is revealed by the GUI:

Sorry that it is in Danish. However at the very bottom right it on my system states: Cron.php must be executed by the system user “www-data”
I expect Nextcloud to get this from the file system, hence you will be able to see which user needs to be used to run the cron. In my system:

sudo crontab -u www-data -e

If you are not allowed to do this, then only asking your webhoster support to set it up, or run a webcron.

Yes it’s the same username in GUI on Background jobs page. So it’s not a username issue.

I found some new stuff today. An error_log file appears in my Nextcloud folder. Here’s what it says :

[19-Mar-2023 15:00:03 America/Toronto] PHP Warning: Undefined array key "SCRIPT_FILENAME" in /home/*user*/public_html/nextcloud/lib/base.php on line 155 [19-Mar-2023 15:00:03 America/Toronto] PHP Warning: Undefined array key "SCRIPT_NAME" in /home/*user*/public_html/nextcloud/lib/base.php on line 162 [19-Mar-2023 15:00:03 America/Toronto] PHP Warning: Undefined array key "SCRIPT_FILENAME" in /home/*user*/public_html/nextcloud/lib/base.php on line 163 [19-Mar-2023 15:00:03 America/Toronto] PHP Fatal error: Uncaught Error: Call to a member function getLogger() on null in /home/*user*/public_html/nextcloud/cron.php:192 Stack trace: #0 {main} thrown in /home/*user*/public_html/nextcloud/cron.php on line 192 [19-Mar-2023 15:05:02 America/Toronto] PHP Fatal error: Uncaught Error: Call to a member function getLogger() on null in /home/*user*/public_html/nextcloud/cron.php:188 Stack trace: #0 {main} thrown in /home/*user*/public_html/nextcloud/cron.php on line 188 [19-Mar-2023 15:15:02 America/Toronto] PHP Fatal error: Uncaught Error: Call to a member function getLogger() on null in /home/*user*/public_html/nextcloud/cron.php:188 Stack trace: #0 {main} thrown in /home/*user*/public_html/nextcloud/cron.php on line 188

Any idea on what this mean? :man_shrugging:

Hello, i’ve run with similar issue and i solved it by using the cPanel utility Cron Jobs.

First in the common settings section select the “once per 5 minutes” options.
Then, in the command section, you need to specify the path to php and the path to the cron.php file from nextcloud.

Assuming you (or your webHoster/webManager) enable php-fpm per account and you need to specify the php version for this account, your command line looks like this

/usr/local/bin/ea-php81 -f /home/username/public_html/nextcloud/cron.php

Here,

  • /usr/local/bin/ea-php81 is the path to your php version (8.1 in this exemple).

  • The -f to access specified file (regarding to the nextcloud documentation).

  • Finally, /home/username/public_html/nextcloud/cron.php is the path to the cron.php file from nextcloud. In this last part, you need to specify the all path to the file. It always begin by “/home” then it’s the username of your cpanel account and the public_html folder or any other folder or subdomain on witch you have install your nextcloud (you can verify your “/home/username” in the filemanager at the top of the left tree).

This can be done in command line to, for those have access to the cPanel terminal.

Hope this helps.