Call to undefined function mysqli_connect() [Cron job only] - php

When running a php script by navigating to the file directly on the server (via the web), everything runs fine.
However, when setting up the same file to run via a cron job, I get the error message:
Call to undefined function mysqli_connect()
The cron job is set up to run this file, with permission 744:
php -q /home/username/public_html/seafood/php/lobsteremail.php
Any idea why a file might run fine manually but not as part of a cron job?

Is your cron job set to run on boot?
If so, it cant find mysqli_connect() because mysql is not running yet.
Before calling the php script, you need to wait.
Sleep 10 # wait for mysql
php -q /home/username/public_html/seafood/php/lobsteremail.php

Related

Tell PHP to enable or use GD when running script via cron

I am attempting to run a php script via cron and I'm hitting a brick wall.
When I run the php script via the command line as root, everything works correctly.
When I run the php script via the command line as the user, everything works correctly.
The error that I am getting is:
PHP Fatal error: Call to undefined function imagecreatefromstring()
To test I created a php file that has...
<?php
var_dump(gd_info());
?>
When that file is executed via cron it again says that I have a fatal error. "Call to undefined function gd_info()"
So at this point, I've narrowed it down to GD not loading in the cron environment, but at this point, I don't know what to do to fix the issue.
My guess is you have multiple copies of PHP. You and root use one built with GD, cron uses another due to PATH environment variable inconsistencies.
As a working user (you or root), run
which php
That will give you a full path like /usr/bin/php. Use that path in your cron entry, eg
0 0 * * * /usr/bin/php /path/to/your/script.php

Crontab on AWS EC2 and $_SERVER PHP variables

I have some PHP scripts for database maintenance in my server that requires its periodically execution. Obviously the easiest solution is to schedule its running with system cron.
The scripts require some server variables accessed from $_SERVER, like database hostname, cron parameters, etc.
I can run the scheduled cron commands from command line without any problem, and everything seems to be working fine (calling something like php filename.php). However, when the same commands are executed from cron, the scripts fails and the error reported is like the following:
PHP Notice: Undefined index: RDS_DATABASE in
/var/app/current/app/xx/Db/ConnectionFactory.php on line 8 PHP
Seems that the $_SERVER variable is not correctly initialized when running from cron, but it works from command line. I have tried with crontab -u ec2-user -e but without luck.
I do not want to use wget to run the script as it adds some overhead, and the scripts are hidden from being accessed from HTTP.
Any hint about successfully accessing $_SERVER from command line, but failing when running from crontab?
Had the same issue. Found a solution:
echo "InstanceID: ".get_cfg_var('INSTANCE_ID')."\n";
For some reason it was working fine on my ec2 user but not as a root cron job. Using the function instead of accessing the $_SERVER array solved my problem.
$_SERVER only works if you will run PHP using any web server. If you will use crontab and execute PHP via command line it will not work. You may refer to PHP documentation http://php.net/manual/en/reserved.variables.server.php#refsect1-reserved.variables.server-indices
As #Baminc and #Ankur says the solution is to use get_cfg_var function to get the information because $_SERVER only works when you access it from a web browser.
What I do is the following for example with SERVER_NAME :
if (isset($_SERVER['SERVER_NAME'])) {
$myServerName = $_SERVER['SERVER_NAME'];
} else {
$myServerName = get_cfg_var('SERVER_NAME');
}
Hope this helps!

Why am i getting Undefined index: HTTP_HOST error?

I am using Facebook SDK to post some test wall post on my own facebook page. It works fine when i run the script on my browser but when i run it from terminal it gives me as error as below, i don't know what's wrong please help. I want to post on my facebook page using php CRON scripts like every 6 hours.
Undefined index: HTTP_HOST error in Facebook/src/base_facebook.php
The cron executes the PHP not like a module of apache, so many environment variables are not set by the server. When executing from cron your PHP script is like GCI one, more precisely its CLI (command line interface - php-cli). So as you can imagine, there is no web server and there is no HTTP_HOST.
PS: You can transfer data (urls, hostname or whatever you like) as command line arguments (environment variables) to PHP: Command line usage
Addition:
$php -f cronjob.php HTTP_HOST=www.mysite.com #example
<?php
// cronjob.php
$host = $_GET['HTTP_HOST']; // Get the host via GET params
?>
If you run your script from a terminal, or a cron job, there is no HTTP environment.
A possible solution to this is to run the script with a wget http://.../parameters instead of with php scriptname.

When does a PHP script stop executing when called from CLI?

I basically have a cron job calling one script every minute. Script immediately stops, if previous script is still running (checks previous script's activity time).
So I made a bug, and the script went in to an infinite loop (I know it was called from by cron atleast one time). I created a fix and uploaded it to the server, but I'm still wondering:
How long will the bugged script run?
How can I know if it is still running?
What does terminate a script and why?
The script just echoes out the same text over and over again.
P.S. PHP's max execution time within the script is set to 0 (infinite) and I don't have a direct access to the server, only FTP.
How can I know if it is still running?
Just set up a new cron job, but have the cron command be a something that helps you debug:
a useful one would be
ps -af | grep php > /some/path/to/mylogfile.txt
the ps command lists info on running processes. with those flags, part of the output will be the original linux command that started the process, and so we can grep the line and look for php because the origional command was probably something like:
php myscript.php
the output is redirected to mylogfile.txt for you to manually read after the cron job runs.
the process id should be part of the output. you can then use the kill command on that process id, again by just entering the command as a fake cron job.
Until the script runs into an timeout(max_execution_time defined in php.ini file or set_time_limit method)
Have a look at the running processes
send kill command to the script or wait till a timeout occurs
PS: you have to php.ini files - one for command line and one for Apache - be sure to Change the max_execution_time in the commandline ini file

Running a SQL Server PHP Script in background WAMP

I've got a script that connects to SQL server to pull some data over to a MySQL database, and I'm wanting to set up scheduled tasks to run this on a regular basis. Ideally it should run in the background, but I'm not too fussed if it opens/closes something.
I've made a bat file with the script
C:\wamp\bin\php\php5.3.0\php.exe "C:\wamp\www\data\import.php"
However it doesn't seem to like the fact it's connecting to SQL Server and it erroring with
PHP Fatal error: Call to undefined function sqlsrv_connect() in c:\wamp\www\includes\connection.php on line 4
Which it doesn't throw when running it in a browser. Any ideas?
It looks like your command line PHP (CLI) does not have the SQL Server extension activated.
Try to:
Check your php.ini file (the CLI-specific one, not the Apache one)
Run php -m to see what modules are activated in command line.

Categories