0

I'm trying to run cron.php via a cron job set up in crontab on my server via the following command (running every 2 minutes purely for testing while I try and get it working - it will be every 6 hours when fixed);

*/2 * * * * /usr/bin/php -q /var/www/www.mysite.com/crontab-cron.php >>/var/log/server-cron.log 2>&1

The crontab file was needed to work around an error with the include file locations and looks like this;

<?php
$doc_root = dirname($_SERVER["SCRIPT_FILENAME"]);
chdir($doc_root);


include_once './cron.php';
?>

However now I'm receiving the error (shown in the cron log file)

PDOException: SQLSTATE[28000] [1045] Access denied for user &#039;root&#039;@&#039;localhost&#039; (using password: NO) in lock_may_be_available() (line 167 of /var/www/www.mysite.com/includes/lock.inc).

I've come across a few suggestions mentioning creating a new database user with the same permissions and changing those details in settings.php but that's not making any difference.

Any ideas how to get this working? I just need to run the cron job via crontab.

  • Drupal has its own cron.php. I am not sure why you aren't using that. – kiamlaluno Jul 10 '15 at 22:44
  • 1
    Because that has to have a user invoke it. This way no site interaction is needed to run the cron job. Using a real cron job to do this task is much more efficient. I am still using drupals cron.php BTW, I just want to force it to run without the need of user interaction. – Novocaine Jul 11 '15 at 8:27
  • 1
    cron.php doesn't require any user interaction. You could use curl --silent --compressed http://example.com/cron.php?cron_key=<key> and obtain the same. – kiamlaluno Jul 11 '15 at 15:37
  • @kiamlaluno You've just replaced wget with curl. Fundamentally, your solution is the same. – Blake Frederick May 15 '17 at 15:10
  • @BlakeFrederick When I wrote my comment, the question didn't contain any solution, which is not what questions are for, though. – kiamlaluno May 15 '17 at 16:20
5

You could run the cron URL that Drupal provides in Administration > Configuration > System > Cron.

And in your crontab, instead of invoking the cron.php file individually (won't work), you just have to invoke a URL. This can be done with wget, lynx, curl, etc. E.g.:

With wget:

0 * * * * wget -O - -q -t 1 http://www.example.com/cron.php?cron_key=<key>

Configuring cron jobs using the cron command

| improve this answer | |
  • I've tried this with and without >>/var/log/server-cron.log 2>&1 that I used for logging the call, but it's not visibly doing anything. The cron job page in Drupal does not update the last time it was ran and nothing is added to the log using wget. – Novocaine Jul 10 '15 at 16:21
  • 2
    Make sure you have wget in your server. If you have console access in your hosting, try running wget http://www.example.com/cron.php?cron_key=<key> and see if that updates your Drupal "cron run last time...". – typologist Jul 10 '15 at 17:22
  • That worked, and I think it's shown me the problem with the cron job. This is currently on a dev site that has a self signed ssl certificate. The initial wget failed as it could not validate the ssl, adding --no-check-certificate made it run successfully. – Novocaine Jul 13 '15 at 8:47
  • Yeah, adding the no check for the certificate in the cron job now successfully runs using wget. Thanks! – Novocaine Jul 13 '15 at 9:23
  • You can use curl also, search about. I recommend to test the command directly before configuring in crontab. – khaled_webdev Jan 10 at 9:02
3

If you go to Configuration > Cron you can see a URL that can be used to trigger cron from outside the plattform without being logged in.

Use this URL in crontab so you do not need care about users etc.

| improve this answer | |
  • I'm aware of this URL, however the problem with using it is that crontab does not send GET variables in the same was as a browser would, and therefore cron.php will not be able to interpret the key to validate it. I could edit cron.php to allow this key to be interpreted but it's not good practice to modify core drupal files like that is it? – Novocaine Jul 10 '15 at 14:38
  • Try the wget command for calling the URL as typologist mentions in his answer. – schlicki Jul 10 '15 at 15:14
1

See the documentation at http://www.drush.org/en/master/cron/

The relevent line:

10 * * * * /usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin COLUMNS=72 /usr/local/drush/drush --root=/path/to/your/drupalroot --uri=your.drupalsite.org --quiet cron
| improve this answer | |
  • Looks promising, I'll give that a good look later. – Novocaine Jul 10 '15 at 15:06
  • The documentation is no more (404). – khaled_webdev Jan 10 at 9:04
1

I'm using simple command

*/2 * * * * cd /path/to/your/drupal && drush cron 
| improve this answer | |
  • This is a pretty good solution: in automated environment you don't have to play around with the key. – ssibal May 15 '18 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.