Why would crontab PHP script execution differ from CLI? - php

I have a PHP script that begins as follows:
#!/usr/local/bin/php -q
<?php
require_once 'vendor/autoload.php';
//set up google api client
$client = new \Google_Client();
When I execute directly from the command line using:
php /Users/xxxx/app.php
It executes normally. However when I include in crontab in the form:
30 7 * * * php /Users/xxxx/app.php
I get the following error:
Fatal error: Uncaught Error: Class 'Google_Client' not found in
/Users/xxxx/app.php:8
Stack trace:
#0 {main}
thrown in /Users/xxxx/app.php on line 8
I haven't used cron much previously - any idea how I might rectify this?

IMO the first thing to do is to use the complete path to the php executable and maybe also try setting the cronjob to run as the same user from when you execute from cli:
From cli run
which php
and insert the path in your cronjob (usually /usr/bin/php):
30 7 * * * YOUR_USER /PATH_TO_EXEC/php /Users/xxxx/app.php

Related

aws cli gives access denied when running shell_exect

On the command line I can run:
aws s3 sync s3://my_bucket . --dryrun
just fine.
However, I'm trying to run it as a command in Laravel:
public function handle()
{
$command = 'aws s3 sync s3://my_bucket . --dryrun';
shell_exec($command);
}
and am getting:
fatal error: An error occurred (AccessDenied) when calling the
ListObjectsV2 operation: Access Denied
Why does the same command work on the command line but not within my PHP application?
I followed the advice of #Sammitch, and used
--profile my_config
where my_config was a named set of configuration parameters created using:
aws configure --profile my_config
Source: AWS creating a named profile

Cron Job on cPanel not running

I'm trying to run a cronjob in cPanel which gives me the following error:
Fatal error: Uncaught Error: Class 'mysqli' not found in /home/example/public_html/new_facebook_csv/csv_generator1.php:180
If run that file through browser by using the link example.com/new_facebook_csv/csv_generator1.php i get the right resaults without any errors, but if i try to run it with a cron job i get the error "Class 'mysqli' not found".
The cron job I'm running is the following:
php /home/example/public_html/new_facebook_csv/csv_generator1.php > /home/example/public_html/new_facebook_csv/facebook.log
and here is the logs i get after the cron job:
X-Powered-By: PHP/7.3.22
Content-type: text/html; charset=UTF-8
<br />
<b>Fatal error</b>: Uncaught Error: Class 'mysqli' not found in /home/example/public_html/new_facebook_csv/csv_generator1.php:180
Stack trace:
#0 {main}
thrown in <b>/home/example/public_html/new_facebook_csv/csv_generator1.php</b> on line <b>180</b><br />
I added the mysqli extension in the php.ini (extension=php_mysql.dll) but not luck with that.
Also check if mysqli is installed to my server with this commaned:
php -m|grep mysql
and that was my output
mysqli
mysqlnd
pdo_mysql
which means that mysql is installed to my server
try with curl like /usr/bin/curl example.com/new_facebook_csv/csv_generator1.php
or you can use wget wget -q -O- example.com/new_facebook_csv/csv_generator1.php
This is how I scheduled a cron job in cPanel through GoDaddy that runs the script and includes MYSQLI:
curl -s "https://yourwebsite.com/phpscript.php"
I tried many variations including cPanel's example on running a PHP script with a cron job. I tried php in front of the script path, and the same format you used above, as well as the suggested answers and I received the same errors. Just using curl -s and then the url of your php script will do the trick.

Class from PHAR not found

I want to use the PHAR from nette-tester V1.7.1 ... and build a simple script that runs a test, but i fail at a very basic level.
The class Tester\Runner\CliTester is correct and available within the PHAR, but is not found.
Composer is no option and including all files without the phar would we the worst option.
Input:
<?php
include_once 'tester.phar';
$tester = new Tester\Runner\CliTester;
die($tester->run());
Output:
Fatal error: Class 'Tester\Runner\CliTester' not found in
C:\webdev\testrunner.php on line 14 Call Stack: 0.0000 136168 1. {main}()
C:\webdev\testrunner.php:0
If you want to simply run CliTester you just need to run php tester.php in your command line.
If you include tester.phar into your application some files are not loaded. Look at stub.php in tester.phar.

running Cassandra from php shell command (using datastax php-driver)

I am able to run it through the browser but not from command line
ie php test.php
$raw = Cassandra::cluster()
->withContactPoints('localhost')
//->withCredentials($this->username, $this->password)
->build();
var_dump($raw);
die;
from browser:
object(Cassandra\DefaultCluster)#2 (0) { }
command line:
PHP Fatal error: Class 'Cassandra' not found in /var/www/html/test.php on line 2
Is it possible to get same from command line as well?
The cli uses a different php.ini file (e.g. /etc/php/7.0/cli/php.ini & /etc/php/7.0/fpm/php.ini). You are probably not including the extension in the cli one.

How to Convert HTML/CSS to PDF in PHP on Linux

I have been trying to convert an html page to PDF with out any luck, I have tried to use wkhtmltopdf (http://code.google.com/p/wkhtmltopdf/) and the PHP class provided by the same developers (http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp) although the following command executed correctly in terminal:
wkhtmltopdf http:/www.googl.com test.pdf
I was unable to execute the command in PHP because of the following error:
cannot connect to X server.
Can any one provide me with a step-by-step tutorial in order to be able to convert html/css to PDF file, whether using wkhtmltopdf or any other decent solution.
Thanks in advance
here is the class that i have used http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp
and below is my code
$html = file_get_contents("test.html");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
Below is the error:
Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return any data.
/usr/bin/wkhtmltopdf: /opt/lampp/lib/libxml2.so.2: no version information available
(required by /usr/lib/libgstreamer-0.10.so.0) No protocol specified wkhtmltopdf: cannot
connect to X server :0 </pre>' in /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.class.php:211 Stack trace: #0 /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.php(68): WKPDF->render() #1 /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.php(50): html2pdf() #2 /var/www/c4/index2.php(114): require_once('/var/ww...')
#3 {main} thrown in /var/www/c4/components/com_wkhtmltopdf/wkhtmltopdf.class.php on line 211
I found the solution, The following error "No protocol specified wkhtmltopdf: cannot
connect to X server :0" was basically caused by on old version of "wkhtmltopdf" which was installed using apt-get. The solution was to download the static binary from the code.google using the following link:
http://code.google.com/p/wkhtmltopdf/downloads/list
and then perform the following actions:
tar -jxvf wkhtmltopdf-0.11.0-static.tar.bz2
sudo aptitude install ia32-libs
Next, you’ll have to make a symbolic link pointing to WKHTMLTOPDF in /usr/bin.
sudo ln -s /full_path/WKHTMLTOPDF /usr/bin/WKHTMLTOPDF

Categories