user2417713
user2417713

Reputation: 167

Crontab failing to run on AWS ec2 Ubuntu Instance

Trying to set up a crontab to run a php script on my AWS ec2 ubuntu instance:

* * * * * /usr/bin/php /var/www/html/tides/get_tides.php?id=PASSWORD

Cannot get it to execute.

The script executes when I run in a web browser:

http://URL_NAME/tides/get_tides.php?id=PASSWORD

When I run 'which php' on the command line it returns /usr/bin/php Directory/file permissions (tides & get_tides.php): 755

If I run sudo /usr/bin/php /var/www/html/tides/get_tides.php?id=PASSWORD on the command line I get: Could not open input file:

/var/www/html/tides/get_tides.php?id=PASSWORD

Any suggestions?

Upvotes: 1

Views: 523

Answers (2)

user2417713
user2417713

Reputation: 167

The problem is caused by not using absolute paths in my cron scripts:

<?php
$path = $_SERVER["DOCUMENT_ROOT"];
$file = "$path/cron-test.txt";

I replaced $_SERVER["DOCUMENT_ROOT"] with /var/www/html

The crantab command is: * * * * * /usr/bin/php /var/www/html/cron-test.php

Upvotes: 1

BMW
BMW

Reputation: 45223

There is no directly way to run php scriping with ?id=PASSWORD, you can adjust the cronjob to run as

* * * * * /usr/bin/curl -o temp.txt http://URL_NAME/tides/get_tides.php?id=PASSWORD

or

* * * * * /usr/bin/wget -q -O temp.txt http://URL_NAME/tides/get_tides.php?id=PASSWORD

Since you didn't show the content in get_tides.php, maybe you can pass command line arguments to a PHP script

php /var/www/html/tides/get_tides.php PASSWORD

and update the php to :

<?php
// $argv[0] is '/var/www/html/tides/get_tides.php'
$id = $argv[1];
?>

Upvotes: 1

Related Questions