Hi all.

Bored of manually deleting the "cron_semaphore" register from the "variable" table (or doing it manually with the Unstick module and every 24 hours, finally I did a workaround to fix it automatically. I tested it for 1 week now and it is working well, I can forget now to do it manually.

It is a simply shell script executed by the cron system every 12 hours (my website Idolium is new and, actually, there is few traffic, so I run cron every 6 hours), that deletes automatically the "cron_semaphore" register, if it exists in the "variable" table. You can add a "delete cache" commands, but for me isnt necessary.

You only need permission to execute crontab (to program the script every X hours), the wget command (or something similar) and in my case, a MySQL database (I supose you can do something similar for Postgre)

This is the script "runcron.sh":

#!/bin/bash

echo "Running Cron..."
/usr/bin/wget -O - -q -t 1 http://www.YOURWEBSITE.com/cron.php
echo "Running mySql..."
mysql -h YOUR_DATABASE_HOST -u YOUR_DATABASE_USER -pYOUR_DATABASE_PASSWORD YOUR_DATABASE_NAME < cron.sql
echo "Running Cron for 2nd time..."
/usr/bin/wget -O - -q -t 1 http://www.YOURWEBSITE.com/cron.php

IMPORTANT NOTES:
- Yes, the -pYOUR_DATABASE_PASSWORD parameter is all thogether (see MySQL documentation)
- Yes, we need to execute cron 2 times, dont know why but if you only execute it 1 time it dont works.
- I'm executing an sql script rather than a command directly.

The "cron.sql" script im executing is like that:

DELETE FROM variable WHERE name = 'cron_semaphore';
commit;

You can save this 2 files where you want and program to execute it in crontab. This is my crontab:

30 */12 * * * sh runcron.sh

which means that I'm executing this at 0.30 and at 12.30 (every 12 hours)

I hope this can help someone to avoid the need of check the Logs every day to control the cron job.

Comments

mdupont’s picture

You shouldn't have to do it, since cron_semaphore is automatically removed if it's older than 1h.

Modules like Unstick may be useful only when running cron more often than every hour. The real fix would be to find why the cron_semaphore variable is still set after 6h: either one of the cron tasks crashes badly and cron_semaphore is not removed, or you're running cron from php-cli and one cron task takes more than 6h to complete.

If you've scheduled cron.php correctly, here is what happens:
1. cron is launched
2. finds that cron_semaphore exists and that it's older than 1h
3. deletes cron_semaphore and ends cron
4. 6h later the next cron is launched
5. this time there is no cron_semaphore
6. cron_semaphore is set to the current time
7. cron tasks are invoked
8. ??? crash
9. cron_semaphore is not removed
10. 6h later the next cron is launched
11. repeat

As you can see, if scheduling cron to run every 6h, it will really run cron tasks every 12h, since if cron_semaphore is set it deletes it but doesn't run cron tasks.

pokepasa’s picture

Hi mdupont.

Thanks for your comments. In my case, I've tested cron with multiple variants: every 2 hours, 4, 6 and 8. In all cases cron stop to work after some time (I leave cron to work for more than 48 hours to test if it can autorecover by itself). It is certain that sometimes it launched the message of "cron fail" and later it starts to work well, but usually it fails for 3 or more launches. In example:

1.- Run cron at 0.00
2.- Fails at 6.00
3.- Fails at 12.00
4.- Fails at 18.00

So, bored of this situation, I decided to do this. I saw that too much people have the same problem (and thats is why the Unstuck module exists too I supose), so there must be some other circumstances that makes cron fail repeatedly (I saw people with 3 days cron not working).

In any case, since I did this, I dont have to worry about cron anymore, all is working well.

P.d.- I call cron.php always through wget in crontab, no php-cli

philmck’s picture

I just had the same "cron run failed" problem and deleting cron_semaphore didn't help. Nothing in the logs. However, switching the site from mod_fcgid mode to mod_php and running cron manually did fix it. After switching back to mod_fcgid it still works. So I reckon it's a file permission or ownership issue somewhere, although I couldn't find anything obviously wrong.

Anyway, just posting here in case it helps someone else.

haclong99’s picture

hello

I believe i'm facing the same issue : deleting the cron_semaphore did not help me. Neither any of the solutions presented in the online documentation.

What do you mean by "switching the site from mod_fcgid to mod_php" ?

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.