28

Magento provides two cron scripts in its root directory, cron.php and cron.sh.

Which one is the better to run and why?

32

It would be best to run cron.sh

As of Magento EE 1.13.x and CE 1.8.x, the cron mechanics have changed where Magento has introduced a new scheduling mode function.

There are 2 available modes: 1. default - runs scheduled crons. 2. always - As the name says these tasks will unconditionally be executed every time cron is triggered and don’t need an explicitly defined schedules.

Basically cron.php being called without any parameters uses shell_exec to execute two processes of cron.sh. Each with a different parameter (“default” or “always”). Cron.sh in turn passes this parameter back to cron.php which then executes the cron. Internally Magento uses its event infrastructure to process the two modes by dispatching events with the names “default” and “always”. Mage_Cron then implements two observer methods.

Looking at cron.php, you will notice the usage of the PHP function shell_exec. Apart from being a security concern, the function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. This means that at any given point when your script / code fails due to an error, the following happens: 1. The cronjob becomes stale, 2. No error is recorded, 3. and no one knows that any of this has happened.

To overcome this, the following cronjob's should be added:

*/5 *   * * *   www-data /bin/sh /path/to/magento/cron.sh cron.php -m=default
*/5 *   * * *   www-data /bin/sh /path/to/magento/cron.sh cron.php -m=always

This will ensure that the process modes will always run without using the fallback PHP function shell_exec and that the cron should not become stale as an exception is thrown if an error does occur.

| improve this answer | |
  • Just going to add that the sample cron expression above is pretty generic. www-data would change to whatever user runs web server processes. Also worth noting that for many CPanel/WHM hosting setups, shell_exec() is going to be disabled. – pspahn Apr 15 '15 at 18:02
  • */5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=default */5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=always and these commands give "command not found" error by nupur walia – Amit Bera Jul 24 '15 at 7:06
  • If you get that error, then you need to check 3 things: 1) www-data is the correct user on that server that can run this process, else change it to whatever the user is. 2) check the location of sh, so run "which sh" and it should output the location, this will replace "/bin/sh" if its a different path. 3) finally check that the path to cron.sh is in fact correct. – Shaughn Jul 24 '15 at 13:03
  • @AmitBera I was getting the same error message and couldn't get it working with www-data, root or any other user. I actually just omitted the user and it now works, so */5 * * * * /bin/sh /path/to/magento/cron.sh cron.php -m=default – Michael Oct 29 '15 at 10:28
  • @Shaughn can you explain what is use of default and always mode? – Rohan Hapani Mar 28 '18 at 5:17

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.