Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to run a php script using cron job on localhost

my script is situated at -

/opt/lampp/htdocs/rrugd/myscript.php

now as opt is under root, i tried adding the cron job for root as well as normal user but its not executing.

here is my cron job for executing the PHP script every 2 mins.

*/2 * * * * php /opt/lampp/htdocs/rrugd/myscript.php

my script is basically meant to truncate a table from the database.It will actually be happening at midnight , so the actual variables of cron job will change to

00 00 * * * php /path/to/script.php

but for testing ive used every 2min.

Ive searched on the internet and ask ubuntu, but i cant get it to run. i saw something related to setting a PATH but i didnt understand what is to be done exactly in that method.

Also, i would like to specify that the PHP script myscript.php DID NOT execute on CLI with the PHP command. But it does execute when executed or called through the browser on localhost!

what am i exactly doing wrong? ive spent last 2/3 hours on this! i am using cron job 1st time.

ThankYou!


Edit

php version -

some@somepc$: php -v
PHP 5.5.3-1ubuntu2.2 (cli) (built: Feb 28 2014 20:06:05) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
share|improve this question
    
If you've installed php, then try */2 * * * * /usr/local/bin/php /opt/lampp/htdocs/rrugd/myscript.php instead & let me know whether it works.. or the location of where php is located.. :) –  AzkerM Mar 6 at 20:12
    
@AzkerMohamed that didnt work either! :( –  Nirmik Mar 6 at 20:24
    
is that php5 you've installed?? if so make sure you've installed php5-cli using sudo apt-get install php5-cli in order execute using terminal commands. And then you can try replacing php into php5. Let me know how it goes –  AzkerM Mar 6 at 20:31
1  
@AzkerMohamed yes its php5, i have php-cli installed and i tried replacing php with php5, and it still doesnt work :( –  Nirmik Mar 6 at 20:41
    
Firstly, could you please edit your question and post the output for php -v & whereis php... also as an alternative you can try to place your php script in localhost www dir (or htdocs or whatever) & try to call it as a url */2 * * * * wget -q http://localhost//myscript.php. Let me try to make it more clear after looking at the outputs. –  AzkerM Mar 6 at 20:51

1 Answer 1

up vote 1 down vote accepted

Assuming its php5; There are instance of using php into a cronjob. You may try one of these by following;

  • In order to use php5 in terminal you will need php5 cli installed. First, check whether php5 & cli functionality is available. for that;
johan@ubusrv:~$ php -v 
PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:44:49) 
Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, 
Copyright (c) 1998-2007 Zend Technologies

The command displays that your php5 version along with php5 (cli) installed. If not, install it using the following command;

sudo apt-get install php5-cli
  • Once installed you can either add php or its binary path to the cronjob as below;

using php or php5

*/2 * * * * php /opt/lampp/htdocs/rrugd/myscript.php

using php binary path

*/2 * * * * /usr/local/bin/php /opt/lampp/htdocs/rrugd/myscript.php

You may also use below commands to find where your php/php5 path is;

johan@ubusrv:~$ which php 
/usr/bin/php 

johan@ubusrv:~/cli$ whereis php 
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz 

johan@ubusrv:~/cli$ type -a php 
php is /usr/bin/php 
  • Elsewhere, if you need to call a php script using URL; you can simply use lynx, curl or wget. Make sure you've placed your php script within the www or public_html directory and call the path properly on the cronjob.
*/2 * * * * wget -q http://localhost//myscript.php

More on: PHP Cron Job: How to Execute PHP Script Using Crontab in Linux

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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