Cannot run cron job for a php script

Nirmik

I want to run a php script using cron job on localhost. My script is located at:

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

Since opt is under root, I tried adding the cron job for root, as well as normal user, but it is not executing. This is the cron job for executing the php script every 2 minutes:

*/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 I've used every two minutes. Ive searched on the internet, and AU, but I cannot get it to run. I saw something related to setting a PATH but I didn't 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, however it does execute when executed or called through the browser on localhost!

PHP Information:

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
AzkerM

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot run cron job for a php script

From Dev

Run Cron Job on PHP Script, on localhost in Windows

From Dev

Run a php script on localhost using cron job

From Dev

Cron job to check if PHP script is running, if not then run

From Dev

Run Rails script in cron job?

From Dev

Concerned someone will run a php script only intended for a cron job

From Dev

PHP script does not run as cron job on Ubuntu Server

From Dev

Cron job to run a php script for 24 hours then stop it

From Dev

Execute PHP script in cron job

From Dev

How to set a cron job to run a shell script?

From Dev

Initialize WordPress environment in a script to be run by a cron job

From Dev

How to set a cron job to run a shell script?

From Dev

Cron job to run python script raises error

From Dev

Cron job fails to run NodeJS script

From Dev

How to Run a JMeter Script as a Windows Cron Job

From Dev

Cron job of multiple php files with a php script

From Dev

CasperJS and PHP In Cron Job Cannot Open Files, Works Fine When Run Manualy

From Dev

is setting up a cron job in cpanel all you need to do to run a php script

From Dev

Jelastic Cron job to call PHP script

From Dev

Python script elicits error when run in cron job but at no other time

From Dev

Basic Cron job to run a shell script to append date in a log, not working

From Dev

Cron job to run python script at reboot does not work

From Dev

Cron job does not run

From Dev

Not sure if cron job is run

From Dev

How can i run a php script on a server every month without running a cron job. e.g. using .htaccess

From Dev

Unable to run php script using cron

From Dev

Cron job script not working

From Dev

shell script for cron job

From Dev

Cron job script not working

Related Related

HotTag

Archive