03 Jan

Magento Cron not Working?

More than you ever need to know about Magento’s cron and how to setup in Kloxo Control Panel

We field a lot of questions about Magento’s cron and how to setup. There are quite a few things that can go wrong in setting up Magento’s cron. But hopefully a little understanding in how it works will help you set it up so that it works right the first time.

If want to skip all the explanations of the cron, file ownerships, and permissions click here.

Kloxo User Account

The first thing to take into account is how is the domain was setup in your Kloxo control panel. When we first started using Kloxo we setup everything for you under “admin” user account.  If this is the case you would want to setup the cron from the admin account. But our later clients we setup separate client accounts for the different domains to allow for more customized access and backup schemes. If you are the latter then you need to go to the client panel, which means either sign into Kloxo from with the client credentials, or from the admin panel click on “Clients” button, and then choose the client.

What does user account mean?

Its important to understand what client means in the scope of file permissions.

client username = file owner

For this example we will setup a user account “prohost”.

When we setup the new client in Kloxo, it will setup a new user in Linux and corresponding “home” directory. Like your own home everything that belongs in that directory belongs to you. With few exceptions every file and directory in your home folder should have you as the owner. In keeping with the Linux default, when a user is created a folder is created in the /home directory with the username. So in our case a new directory would be created called/home/prohost.  The important thing to know about file ownership is that all your magento files should now have ownership of the Kloxo “username” otherwise when PHP runs in your directory it may not have permission to correctly read or write files. We will set correct ownership when we migrate or install a new Magento, but you will want to keep those permission the same by always using the correct SFTP or FTP account to access that directory, ie… avoid using “root” account.

Understanding Permissions

File permissions means who can read, write, or execute your files. We won’t get into the full list of file permissions but just understanding the basics as it applies to cron.  There are 2 cron files in your Magento root directory “cron.sh” & “cron.php”. I will explain the first cron.sh just for you information, but you should ignore this file. Magento just included it to make something that could be used across different OS’s, but with our system you should use the second file “cron.php”.

Do php files need to be executable?

You would assume that since php script must be executed that the file permissions must specify that the file should be executable.  This however would be incorrect, and the reason has to do with what executable means. Executable means that the file can be run without being called by an interpreter because the script calls its own interpreter.  However, when a php file is called by your webserver it is called through the php interpreter, meaning that the only file permission that is required on php files is the “read” permission.  So assuming correct ownership is set all php files on your system could be set to “chmod 400 (readable only by owner)” and php could execute successfully.  However you would really need to know what your doing and need  super secure system to use this permission, instead you should set most files on your system to “chmod 644 (readable & writeable by owner, readable to group and public)”.

So when should a file be set to executable?

Usually the only files that would be set to executable would be a bash script like “cron.sh” or the “mage” file in your Magento root. The simple way to tell is to look at the first line of these files.

#!/bin/sh

What this does is tells linux what interpreter to use to interpret the file. Meaning that the file can be called without specifying the interpreter, since the script specifies its own interpreter. So if I setup the following command in my Kloxo cron there should be no issues assuming file ownership, path and permissions are correct.

/home/prohost/public_html/cron.sh

Notice that it is just a filepath, no command.  The reason why this works is that the top line of the file tells linux what interpreter to use to call it.  So in order for this command to work the cron must be setup either under “root” user or “prohost” user. Because Kloxo cannot setup “root” cron, only an experienced system admin should setup a cron as “root”.  Instead setup the cron under the “prohost” user in Kloxo, which we will go over step by step later. Since it is in the “prohost” directory the file owner needs to be “prohost”, and file permission needs to be at least “chmod 500 (readable and executable by owner)” and maximum “chmod 555 (readable and executable by everybody)”.

Do I need to make the file executable?

You don’t actually need the file to be executable if you specify the interpreter. Like in most examples of setting up the Magento cron you can call the file from the using bash.

/bin/bash /home/prohost/public_html/cron.sh

To run the above file, just like I explained with php earlier it would only need “read” permission as the interpreter is reading in the file interpreting it and executing the code.  The file itself can, but doesn’t have to be “self-executable”.

Why /bin/bash?

Most people use /bin/bash instead of /bin/sh which is actual interpreter specified at the top on the file.  They will both work, I will skip the explanation on why, but basically using /bin/bash is the same as making the file executable. But you could also just as easy setup like this.

/bin/sh /home/prohost/public_html/cron.sh

Can I run php this way?

PHP files can be run in the same way if you put the full path to the php interpreter, but there would be very few reasons to do this unless you wanted a script that was only callable from the command line, the reason that it would almost never be done is that the location of the php interpreter can vary across different systems, and even multiple interpreters could be present. If you ever want to know the location of the default php interpreter just execute the following command on your server.

which php

Then you would put the path to the php interpreter on the very first line of the file like this:

#!/usr/local/bin/php

Why cron.sh?

This file does a couple things:

  1. It find the file-path to the php interpreter
  2. It uses that file-path to create a  to the php interpreter and run cron.php.

They use a shell script because it is a basic  script that is understood across the different OS systems, meaning that you can use the same syntax to call the cron no matter the system.

How should you setup on our servers?

That was a lot of explaining for something that we will ignore. Because we don’t need a cross-platform command, we just need one that works on our systems. For this we will just go straight to the actual script which is “cron.php”. The following command should work in most all cases.

php /home/prohost/public_html/cron.php

This is different from the early command because we aren’t specifying the full path to the interpreter.  The reason we don’t need to do this is that when php is installed on most systems,  it creates an alias “php” for the full file path the php interpreter. This should be the case on most servers.

However if you are paranoid and want exact syntax this is how you would do it. First you need to find the php interpreter, so execute this command.

which php

This will output the full path to php interpreter which in many cases is something like “/usr/local/bin/php”,  you would then just put the full path in front of the cron.php filepath.

/usr/local/bin/php /home/prohost/public_html/cron.php

What permissions should the file have?

Like explained earlier, since the file is being read into the interpreter it only need read permissions. So the default file permissions work just fine “chmod 644”.

What ownership should the file have?

Since it is in the “prohost” directory it should be owned by “prohost”.

Using php flags

There are lots of “flags” or “parameters” that can be specified to tell the php interpreter how to behave. The only one that deserves any explanation would be the “quiet” flag. In the cron screen in Kloxo you can put in your email address so that you can receive output of cron jobs. However, if the cron is running every 5 minutes like recommended then it could be a lot of emails. You can tell php to quiet its output by using the “-q” flag.  So assuming the above examples we can do:

php -q /home/prohost/public_html/cron.php

or

/usr/local/bin/php -q /home/prohost/public_html/cron.php

How to setup in Kloxo?

First you need to know what user to setup the cron as. To determine that, just look at the full path to the cron file. If it is:

/home/admin/prohost/cron.php

Then you need do it through the Kloxo “admin” user. Because “/admin” directory is directly under the “/home” directory. Which should mean that all files in that directory are owned by “admin” user.

If the file path is:

/home/prohost/public_html/cron.php

Then you would need to go to the “prohost” user account. Because the “/prohost” directory is directly under the “/home” directory.

1) Assuming the latter you either should sign in as the Kloxo username “prohost” or if you are signed in as “admin”, then just find the clients button.

Kloxo Client Button

2) From the client list choose the client by clicking on its name:

Kloxo Choose Client

Kloxo Choose Client

The next steps will be the same no matter the user.

3) The easiest way to get to the cron screen is to type in “cron” to the search box on the left hand side, then click on “Cron Scheduled Tasks”.

 

Kloxo Finding Cron Button

Kloxo Finding Cron Button

4) Enter your email in the “Mail to” box so that you can receive cron output. You can delete after you are sure that the cron is working properly.

Cron email

Cron email

5) Add standard cron make sure when adding minutes that you add every minute of the hour you want the cron to run. ie… for the cron to run every five minutes you will need to add 0,5,10,15,20,25,30,35,40,45,50,55 & all the rest you should add “all”.

Kloxo Add Cron

Kloxo Add Cron

 

Once you save, your cron should be setup successfully.

Share this