TurnKey Linux Virtual Appliance Library

proper use of cron job for php file

larnold's picture

I want to run a "cache cleaning" script daily on a Magento TKL.

Normally I would run from a browser. and wondering if I should have my cron ap read:




How to structure this in WebMin - Scheduled Cron Jobs?

Want to run every day at 3:pm..

Any way to loc the .php so it has to be run from the back end.

Thanks for any and all help.

L. Arnold


Jeremy Davis's picture

A guess...

AFAIK cron requires that scripts run locally as root. As such anything you want cron to do, you will need to be able to do it from the commandline yourself.

So first step would be getting the process to work manually from the commandline, then adding it as a cron job will be easy (it can be done in Webmin).

With php5-cli php files can supposedly be run from the commandline (can be installed via apt-get install if not included by default - don't recall if it is). But I have only tried it once and it failed miserably (although in fairness it was most likely user error!) As the script will run from the commandline, you will need to use the filesystem location for the script (not the web location).

You'll need to do some testing and see if you can get it to work as expected and like I say once you have it working from the commandline, it should be a breeze to get it running with cron.

If you are successful be great if you can post back.

[an afterthought] Perhaps it's worth posting on the Magento forums (assuming they have forums) as perhaps there's a better/easier way to acheive what you want to do?

Landis Arnold's picture

Permission denied in SSH

when I go to run the .php page from whithin SSH I get "permission denied"

I can do so from the front end "web" however.

How might I run it with the correct permissions?  i would think Root has all permissions (which is how I logged into SSH).

thanks in advance.

Landis A.

Jeremy Davis's picture

Does the php script have execute permissions?

I'm not sure how php scripts run within Apache/php, perhaps they only require execute permissions to run via commandline? I don't know enough about the subject - guess you'll have to research and/or test. Worth a try IMO.

AFAIK root has permissions to access anything, however by default files won't have execute permission (which is what lead me to my above idea).

Landis Arnold's picture

Finally seems to be working

Made a new Cron Job to run every 5 minutes except for "on the hour" which is when the Built In TKL/Magento Cron call should be running.

www-data (user)

php -q /var/www/magento/cron.php

I tried to set the TKL/MAG Magento File to run every 5 minutes but was returning errors so it may have a sintax error.  It reads like this and I have no understanding


/usr/bin/php /var/www/magento/cron.php > /dev/null 2>&1

I do wish I understood the difference in thes statements.  More reading I suppose.

Anyway this really allows some additional capabilities that are pretty cool.

Landis A.

Jeremy Davis's picture

From my reading there is no difference in them

They should give you exactly the same result. The only difference is that the first is a commandline argument, whereas the second is a script. If you wanted you could set the second command as a cron tab on it's own (rather than as a script), although it shouldn't really matter (as long as the script is set as executable).

Guest's picture



Check this page out for Magento Cron, it was helpful for me.


The your crontab should have something like one of these:

*/5 * * * * /bin/sh /[magento_path]/cron.sh
*/5 * * * * /bin/bash /[magento_path]/cron.sh
*/5 * * * * /usr/bin/php /[magento_path]/cron.php
*/5 * * * * /usr/local/bin/php -f /[magento_path]/cron.php
Landis Arnold's picture

I think I got it to work now (run as root)

Using the following in the cron job

php -q /magento_patho/cron.php  

thanks for posting.  I will study the link more for sure.

Landis A.

Landis Arnold's picture

Works for a while then ???

Magento tells me that Cron is not working again.   Very agravating working through this.  I need to look at CRON logs I suppose next.

On the web there are about 20 ways to configure a CRON file.  Webmin seems to think they all work but I suspect there are rights and path issues involved as well with this.

Landis A.

Rick Kipfer's picture

wget for php cron

I spent DAYS trying to figure out cron with php, ran into SOOO much stuff with rights, and this is what i came up with and it works like a charm for me. I needed my script to run every 5 seconds, and as we know, cron is only a 1 minute minimum cycle. so...


This is how I set up a script of mine to run in cron. In the beginning of the script I set


(my script will run for days at a time without a problem, as it loops with a 5 second sleep)
Then in cron, using
crontab -e
I set:

 * * * * * wget --no-check-certificate https://localhost/somepath/script.php -O /dev/null

you could use (for 3pm)


 0 15 * * * wget --no-check-certificate https://localhost/somepath/script.php -O /dev/null



the -O is an uppercase oh

the --no-check-certificate is to bypass wget issues with SSL (not needed with http)

in this case, I run it every minute. It checks to see if it is already running, and immediately terminates if it is so I don't get more than one instance. The result is the script starting up again at the turn of the minute if it terminates for some reason.

Hope this helps. It probably took me a solid week of crud to figure this out. haha.

Landis Arnold's picture

Where are you editing the Script? Webmin or Shell?

I have been in WebMin trying to edit but I am wondering if you are doing this another way?

Are you setting an "external script" first (you follow by saying "then in Cron") or is this all in Cron.

What Folders (if you are in Shell) are you placing the Scripts into. 

I will look at mine.  Fundamentally Magento occassionally thinks Cron is working well, but then normally does not...  All the while data is exchanged with 6 hour delays.

Noses to the grindstone.  Persistence matters. 


Landis A.

Rick Kipfer's picture

Shell - crontab -e

Yes, it is an external script (ie: accessable by a web browser from anywhere, but with a pretty garbled path)

So I could initiate it from a web browser using the whole path name of the URL

(say, www.example.com/aaa/bbb/ccc/script.php from an internet cafe in china)

or, it works with wget in a cron job on that server with localhost like...

http://localhost/aaa/bbb/ccc/script.php (as in the example I gave with all the extra stuff to pipe any output into null so it doesn't create files of any output) I did it this way so I can make a clone of the server and the script will run automatically on the clones as well without any editing.

Many would say this is a security concern, as all scripts other than public should be inaccessable from the real world, but

a) If someone did find it, there are checks that, if it's already running, it just terminates instead of making a new instance. (I do this by updating a database value with the current time and check against that)

b) If they don't have a certain value in a variable (such as ?security=7293847293) it just terminates.

So fair warning, if this is something that is a high secure script, maybe you don't want to go this "external" route.


The benefits of doing it this way is that the cron job requires no security whatsoever (wget is essentially triggering the script just like a web browser would).

I used SSH logged in as root by putty and at the command line typed:

crontab -e

This allows you to edit the crontab file directly, and does not requre a service restart or anything after you save the file. It just looks at each entry in the file at the turn of every minute (like, exactly one second after), and if any of the criteria of those first 5 fields satisfy it should run, it does.


This link is a good one for getting a lot of basic info about cron, in particular the last section shows exactly how to add the entries as you want them.

I have never used any other method than crontab -e at a shell prompt. It has always worked flawlessly for me and as long as it works, I just don't trust any other way.

If you need any help on editing the crontab and setting up a job, let me know, I'll help if I can.


Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <strike> <caption>

More information about formatting options

Leave this field empty. It's part of a security mechanism.
(Dear spammers: moderators are notified of all new posts. Spam is deleted immediately)