Errors while executing a PHP script with crontab - php

I'm making a website with PHP5, here's my setup: the website is hosted on a Raspberry Pi running the Raspbian (Debian) OS. I'm using Nginx as a web server with php5-fpm 5.4.39. My website is made using the Atomik Framework and my script is made with the ConsoleKit plugin (it's a little specific but the problem may be not).
So I have my script, and I run it with php /path/to/index.php <command>. It works really great when using it within my shell, but when I try to make cron execute it, it prints me an error saying that $_SERVER['REQUEST_METHOD'] is not defined but that's something I can solve by forcing this variable to some value. After that, just nothing happen and I don't know what to do. I tried everything: different commands, running the command in an outside bash script, with PHP she-bang, none of them works.
Do anyone have an idea of what could cause this? Maybe a different command that cron could run could fix the problem? Is the fact that "no user" (I guess, I'm not a Linux expert) is running the script a problem?

You could trigger the php code through http like this in your cron:
30 11 * * * /usr/bin/wget http://www.example.com/cron.php

To debug the problem, you will need to get access to the logs.
*/1 * * * * full_path_to_your_php_script.php >> full_path_to_your_log_file.log 2>&1
Then check the full_path_to_your_log_file.log file

Related

Cron job keeps failing in cPanel

My cron job keeps failing in cPanel:
/bin/sh: /opt/cpanel/ea-php73/root/etc: is a directory
I have changed the code, previously it was /usr/local/bin/php
This is the full code:
0 0,12 * * * /opt/cpanel/ea-php73/root/etc /home4/***/****.org/api/fetch.php
Does anyone know the reason? The file fetch.php works fine otherwise. How do I know where the PHP command is?
If you want to run a PHP file in a cron job, you should run with the PHP interpreter. But your error shows that the cron job is running by the Bash interpreter.
This means you have issue with this part /opt/cpanel/ea-php73
You should either revert your interpreter back to /usr/local/bin/php
Or you can use the curl command to run the webpage from an external source.
Eg: 0 0,12 * * * curl ****.org/api/fetch.php
In the PHP script, you're directly executing an incorrect shell command. Probably a missing / prefix in a path.

PHP script not running from cron

I have the following command:
php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This runs perfectly from the command line. However, I want it to run as a cron job every 15 mins, so I added the following entry to my crontab:
15 * * * * php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This however, does not seem to work at all. It does not even show up in my system logs (all my other cron jobs do show up).
Any ideas?
There's not really enough information to answer your question, but here are some things that might help.
Sometimes cronjobs don't have context for where to find these programs, so do which php and replace the full path with the php command.
/var/log/cron should have a record of it running, but if there's a problem, you might not know why. Try appending the following to the end of the cron line to aid in debuging:
>> /tmp/fetch_events.log 2>&1
This file will probably contain some hints as to what's going on when the cron does fire.

Running PHP script from cron

I am trying to run a PHP script through a cronjob. I already did this hundred of times, but now it's not working and I cannot figure out why.
I created a script called update_db.php in /var/www/html/ When I run the script by hand:
php /var/www/html/update_db.php
everything works fine. When I put this into a cronjob, it does nothing. My cronjob:
* * * * * /usr/bin/php /var/www/html/update_db.php
I tried to put a bash script in front of it that calls the PHP script, but, again, it only works when calling by hand, not from a cron.
There are no errors in the syslog. Also no mail in /var/mail. I restarted cron already, but no effect.
I use ubuntu 14.04.
Can anyone help me?
Is * * * * * php /var/www/html/update_db.php not working? You shouldn't need to use /usr/bin/php.
Also, check to make sure crons are running on your current system and that your files/directories have the appropriate permissions to be run by the cron.
CRON "should" be logging.
Check the /var/log/cron, looking for your script errorrs or otherwise.
some implementations of cron need a full on restart - I have personanlly never had this issue, but I know fellow admins who have spent far too much time chasing fault, when a simple restart did the trick.

perl script is not running through cron

I need your help here.
I wrote one PERL script for PHP application which needs to be run for every 5 mins.
This script will call PHP program, which will fetch data from MySQL DB and will generate a excel report and will mail those reports to specific users.
Every thing seems to be fine when I ran this script manually with the command (perl reports.pl).
But when I set this Perl in a cron tab, nothing works and reports are not getting generated.
Details: perl script path /opt/app/deweb/web/EDI/Microsoft/reports.pl
this script will call PHP program (/opt/app/deweb/web/EDI/Microsoft/reports.php)
content of script
#!/usr/local/bin/perl
use Net::FTP;
use File::Copy;
use POSIX;
#errorreport = `php /opt/app/deweb/web/EDI/Microsoft/reports.php`;
print "#errorreport\n";
exit;
It is working perfectly when running Manually using command - perl reports.pl
No results, when set in CRON:
*/5 7-19 * * * /usr/local/bin/perl /opt/app/deweb/web/EDI/Microsoft/reports.pl
Please note that this crontab is under super user account named webserv and my login is having access to edit under this super user account.
I'm editing this cron tab using command :: sudo -u webserv crontab -e
I would check the following:
Does it run using sudo -u webserv perl reports.pl? If not, fix the problem for the webserv user (permissions or whatever) and it should work via cron too.
Does which perl using your login give you /usr/local/bin/perl? If not, change the path to Perl in crontab to what you got in which perl to fix the problem.
I found myself to be in the same situtation. After trying to find out the reason, I am almost sure about the reason this happens. Crontab does not have the same environment variables as you when running the script. You must be sure about paths. Try for example run your script like /perl-path /path-to-perl-script/script.pl outside the parent directory of the script and I am almost sure that your programm will not find some files. And as you call one php script from the perl script, it's possible to have the same problem with paths to your php script too.
So the solution is to use absolute paths and no relative.
Also at your perl script don't use php but /full-path-to-php for example:
#errorreport = /usr/bin/php /opt/app/deweb/web/EDI/Microsoft/reports.php;

PHP from commandline starts gui programs but apache doesn't

First, I read some threads by people with similar problems but all answers didn't go beyond export DISPLAY=:0.0 and xauth cookies. So here is my problem and thanks in advance for your time!
I have developed a little library which renders shelves using OpenGL and GLSL.
Last few days I wrapped it in a php extension and surprisingly easy it works now.
But the problem is it works only when I execute the php script using the extension from commandline
$php r100.php(i successfuly run this from the http user). The script is in the webroot of apache and if I request it from the browser I get ** CRITICAL **: Unable to open display in apache's error_log.
So, to make things easier to test and to be sure that the problem is not in the library/extension, at the moment I just want to start xmms with following php script.
<?php
echo shell_exec("xmms");
?>
It works only from the shell too.
I've played with apache configuration so much now that I really dont know what to try.
I tried $xhost + && export DISPLAY=:0.0
In the http.conf I have these
SetEnv DISPLAY :0.0 SetEnv XAUTHORITY /home/OpenGL/.Xauthority
So my problem seems to be this:
How can I make apache execute php script with all privileges that the http user has, including the environment?
Additional information:
HTTP is in video and users groups and has a login shell(bash).
I can login as http and execute scripts with no problem and can run GUI programs which show up on display 0.
It seems that apache does not provide the appropriate environment for the script.
I read about some difference between CLI/CGI but cant run xmms with php-cgi too...
Any ideas for additional configuration?
Regards
Sounds bit hazard, but basically you can add even export DISPLAY=:0.0 to apache start-up script (like in Linux /etc/init.d/httpd or apache depending distro).
And "xhost +" need to be run on account which is connected to local X server as user, though I'm only wondering how it will work as php script should only live while apache http request is on-going.
Edit:
Is this is kind of application launcher?, you can spawn this with exec("nohub /usr/bin/php script.php &"); .. now apache should be released and php should continue working in background.
In your console, allow everyone to use the X server:
xhost +
In your PHP script, set the DISPLAY variable while executing the commands:
DISPLAY=:0 glxgears 2>&1

Categories