Cron (PHP) not running properly - php

I have a cron
0 21 * * * php /var/www/html/site/cron.php/batch 1>>/dev/null 2>&1
However it is not working.
When I try
php /var/www/html/site/cron.php/batch
or
/usr/bin/php /var/www/html/site/cron.php/batch
It says,
"Could not open input file:"
I see, cron.php is considering cron.php file as a folder and trying to open it. So giving me the error.
Also tried
* * * * * nobody wget -O http://example.com/cron.php/batch
P.S: http://example.com/cron.php/batch works when i access it in the browser.
Update:
I had 755 permission to the file. After getting "Could not open input file:" message, even I thought file permission was the error.
I'm still facing the issue

When you run a PHP script from the command line, you can't use additional pathname elements after the script name. Allowing parameters to be put into the pathname is a webserver feature, it doesn't work with ordinary pathnames.
php /var/www/html/site/cron.php batch
The script should then get the batch parameter from $argv[1], not $_SERVER['PATH_INFO'].
Maybe at the beginning of the script you can do:
if (!isset($_SERVER['PATH_INFO']) && isset($argv[1])) {
$_SERVER['PATH_INFO'] = '/' . argv[1];
}

Related

Executing PHP script by cronjob - A path to a data directory in /var/www/html/ directory is not being read in the script

Here is a PHP script:
<?php
$pathToDirectory = "/var/www/html/Tests/Workspace/data";
echo "pathToDirectory: " . $pathToDirectory . "\n";
?>
In my Ubuntu 16.04 OS, I am trying to execute this script every minute as a part of a cronjob. I have added the cronjob like so to my root's crontab file:
* * * * * /var/www/html/Tests/Workspace/phpScript.php >/var/www/html/Tests/Workspace/logs/cronScriptOutput.out
And the cronjob runs successfully. The problem is that once the cronjob has run, following are the contents of the cronScriptOutput.out file.
pathToDataDirectory:
And that's it. That actual path string is NOT printed.
The question is why? How do I fix this.
This might not seem serious here, but the problem is that I need to read a file from that path and go further from there. So I need this fixed. I need that path to be read in my PHP script.
May be you need add php before script name?
* * * * * php /var/www/html/Tests/Workspace/phpScript.php >/var/www/html/Tests/Workspace/logs/cronScriptOutput.out
Of course, better use full path to php - /usr/bin/php - or what you have

Setting up a Cron Job on CPanel to executes a PHP script

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

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.

PHP fopen can not read file if called with cronjob [duplicate]

This question already has answers here:
Crontab - Run in directory
(2 answers)
Closed 8 years ago.
I have a php script that sends out a email report with an attached pdf document. I use fopen. If I call the php file manualy it works without any problems and the mails are beeing send as expected.
But:
If the same file should is call by cron job, it does not work at all. The error is:
Warning: fopen(filename.pdf): failed to open stream: No such file or directory in /home/public_html/util.php on line 29
Any ideas how to fix this?
Every cron runs in a minimal environment, which can be a different shell and a different profile than your user account. Also scripts are started not from the containing directory themselves.
In most cases it works, if you just change your directory to the one containing the script like you do when execute the script on the command line shell. Instead of
* * * * * /usr/bin/php /path/to/your/file.php
write this:
* * * * * cd /path/to/your/ && /usr/bin/php ./file.php
If this is not sufficient, you can load your default user shell and your PATH variable with writing
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
at the top of your crontab (with the right settings of course, which might be /bin/bash and the output of echo $PATH).
Cron jobs run without any environment, including a current directory. That means that you'll need to use the full path to any files that you want to work with.
If you add a valid email address at the top of your crontab:
MAILTO=your-email#example.com
you'll get any output the cron job does sent to your email. This includes error messages, which can be useful for debugging.

Cron Job PHP Script Fails but Script will run via CLI or through browser

I have several php scripts I am trying to set up (recently moved to a new server) they will run from the command line and through the browser but only one will run through cron the other seems to have a permissions problem, if the file is set to 644 I get this message from cron:
/bin/sh: /home/xyz/public_html/scripts/update-script.php: Permission denied
If I set permissions to 777 I get this message:
/home/xyz/public_html/scripts/update-script.php: line 1: ?php: No such file or directory
/home/xyz/public_html/scripts/update-script.php: line 2: syntax error near unexpected token `"includes/clsDatabase-list.php"'
/home/xyz/public_html/scripts/update-script.php: line 2: ` require_once("includes/clsDatabase-list.php");'
yet the script runs from the command line and via browser AND i have another script that is almost identical to this one (calls for the same include on line 1, is located in exactly same folder) that will run through cron! So I know my paths and cron job that I setup in Cpanel are right. If I copy the working one in the command line, the copied version also fails to run via cron. Thanks!
You need to add a shebang to your file to execute it directly:
#!/usr/bin/env php
<?php
//...
Another option would be calling it like this in your cronjob:
* * * * * php /home/xyz/public_html/scripts/update-script.php
Of course you'd replace the * * * * * with the actual crontab data unless you want it to run every minute.

Categories