Not able to run php script from cron - php

I am running LAMP system on ubuntu 14.04. I have an rss feed using php which runs every 1 hour and MySQL stores data. My php file used to run perfectly using php in cron some 15 days but its not working anymore
My cron is as below
0 * * * * /usr/bin/php /var/www/html/rss.php >/dev/null
My php script is working perfectly from browser(firefox/chrome)
When I run the php script using wget in cron it works fine
0 * * * * wget http://www.mywebsite.com/rss.php >/dev/null

Your script is using relatives pathes.
When you open this script in a browser it tries to find files in /var/www/html/.
When you do it in the cron it tries to find files in /.
Put this in the beginning of your script:
define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
Now change every relative include or opener:
include ROOT . "db.php"
...
if (($handle = fopen(ROOT . "tbcatlist.csv", "r")) !== FALSE)

Related

PHP code not executing in cron job

I've a php script which will get all network card mac address and store first one in a text file.
Here is the code which I used to get MAC of system:
<?php
$mycom = shell_exec("ifconfig | grep HWaddr");
$findVal = "HWaddr";
$mac_arry = array();
while (strpos($mycom, $findVal) > 0) {
$start_pos = strpos($mycom, $findVal);
$mac=substr($mycom,($start_pos+7),17);
$mac_arry[] = $mac;
$mycom = substr($mycom, ($start_pos+10));
}
$fileHandle = fopen("/tmp/mac.txt", "a+");
fwrite($fileHandle, $mac_arry[0]);
fclose($fileHandle);
?>
If I execute this file directly in browser or through terminal, it works perfectly. But when I add this to cron for every minute, it doesn't work. Here is my cronjob:
* * * * * /usr/bin/php -f /var/www/html/test.php
by axiac
The cron job executes in an environment that is very different than the one you get in a terminal. The content of the $PATH variable is shorter and it's possible that /sbin (where ifconfig usually stays) is not included. That makes the shell_exec() call fail and the rest of the script doesn't matter. Use which ifconfig (in a terminal) to find out where ifconfig resides on your system and invoke it using its absolute path in shell_exec(). The same for grep (it probably can be found even without full path because it usually stays in /bin.)

Shell script runs php files over and over again

I have a ridiculously simple shell script, nothing more than a few instructions to run some php files ...
#!/bin/bash
clear
cd /home/************** // Just for privacy here
php cron-cpt.php
php cron-lvt.php
php cron-plots.php
php cron-m.php
php cron-a.php
The script is called metrics.sh which is chmod'd and just sits in my local binary folder.
If I run the script from the command line, it works perfectly.
If I add the same script to the cron tab to run once a day, it runs over and over. I assumed the cron was the same as invoking it manually from the command line?
I'm using the same user to invoke in cron as logged on cmd line and have tried as root and a standard user, but the same results prevail.
Google has not been helpful with this. Any suggestions?
Add this to your cronTabs:
0 1 * * * /home/metrics.sh
Change the location to your metrics.sh's location.

Run php script at scedule using Linux server shell command

My Linux server runs PHP and Apache. I want to run a .php file every day at a certain time. (Which will be run automatically in the server)
I have two files: mail.php which sends mail to myEmail#gmail.com. And bash.php which contains some codes which calls mail.php.
bash.php contains :
<?php
#!/usr/bin/php
$command="52 14 * * * ./mail.php";
$result=shell_exec($command);
echo "<pre>$result</pre>"
?>
Then I run bash.php in the browser.
I get no error message. But don't receive any email. Where is wrong?
You are trying to write a cron job in php.
Throw your bash.php away and convert it to a cron task (crontab -e).
php_mailer.cron:
52 14 * * * /usr/bin/php /path/to/mail.php
You shouldn't do it this way, All you have to do is to open the Cronjob config file using the command crontab -e, and then add the command line in it:
52 14 * * * <path to>php <Full absolute Path>/mail.php
To know your php path use the command:
Which php
For more details you may refer to the link: Crontab Command

How can I make a cron to execute a php script?

I want execute a php script each five minutes at my server, it is a php script which works fine alone, I tried since my browser and since console using "php -f php_file.php". But I need execute it automatically every days. I was searching on Google and here to make it but any solution worked for me:
First I edit my crontab and I also restart the cron to make sure that it was updated correctly.
*/5 * * * * /usr/bin/php /var/www/myscript.php
and I tried the following too:
*/5 * * * * /usr/bin/php -f /var/www/myscript.php
I made the script executable too, review my system log (where I can see that my cron is executing correctly, but it doesn't execute php script) and I also try to redirect the output of cron to a file, but it leaves the file empty.
Anyone can help me?
Best regards
You were on the right track by making your script executable.
Do it again if needed
$ chmod +x script.php
Add this to the very top of the file:
#!/usr/bin/php
<?php
// here goes your script
you can test if the script executes by running it like this
$ ./script.php
set-up your cron job like below to set-up some logging but make sure you output something from your script, use print or echo and a relevant message.
*/5 * * * * /var/www/script.php >> /var/www/script.log 2>&1
we are redirecting both standard output and errors into the script.log file.
check every 5 min for activity.
Update:
Try this in your php script
$file = '/var/www/script.txt';
for($i=0;$i<9;$i++){
$entry = date("Y-m-d H:i:s") . " " . $i . PHP_EOL;
echo $entry; // this should write to the log file
file_put_contents($file, $entry, FILE_APPEND | LOCK_EX);
// and this should write to the script.txt file
}
basically we are giving the full path to the file and passing the FILE_APPEND flag so we don't overwrite every time.
Run the script and check if the file is created, the default behavior is to create the file if it doesn't exist.

Reasons a cronjob won't run every minute

I've looked over many SO threads on cronjobs and can't figure out why my simple cronjob only runs once and doesn't repeat after each minute.
here's my cronjob setup:
html$ crontab -e //directory where my web application resides
*/1 * * * * php index.php cronjob everyMinute //command inside crontab
html$ php index.php cronjob everyMinute //run script
Here's my PHP script (my web application uses the Codeigniter framework but that doesn't seem relevant):
function everyMinute(){
if($this->input->is_cli_request()){
$count="i can count everyMinute".time();
echo " {$count}!".PHP_EOL;
file_put_contents("test.txt","Counted $count\n", FILE_APPEND);
}
}
(from our comments)
Make sure you put the complete path of the file. Crontab does need absolute references.

Categories