error in Run PHP code with command line linux - php

My system is :
Ubuntu 20.04
Php 7.4
Nginx
I have a php file with following content in /var/www/hamrah.com/create.php path :
<?php
$output = shell_exec('bash flutter create test');
echo "<pre>$output</pre>";
i run this command in terminal and And this command executes correctly (creates a flutter project for me)
cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php
BUT !
But when I put this command in Crontab, the command is not executed properly and gives an error
In Crontab :
* * * * * cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php
Note: If I put shell_exec(' bash ls') instead of shell_exec('bash flutter creat test') in the php file, it will run well and some commands like the flutter create command will not be executed.
error : bash: flutter: No such file or directory

You can test
<?php
$output = shell_exec('bash /opt/flutter/bin/flutter create test');
echo "<pre>$output</pre>";

Cron doesn't read user's environment variables, the default value of PATH is /usr/bin:/bin, usually your flutter SDK won't be located in these 2 folders, so you need explictly specify the location of the flutter SDK. Add this line to the crontab.
PATH=/usr/bin:/bin:/path-to-flutter-sdk-bin
Use the following command if you don't know where flutter is.
which flutter

Related

OVH cron jobs / Symfony Command

I'm developing a Symfony project and I've 4 commands which allows me to update telephony and emails data thanks to OVH' Api. When I use my terminal on local ( php bin/console converseo:updateTelephony ) the command works fine.
Now, I want to put these commands in crontab with the cron interface from Ovh. I made a php file test.php :
<?php
shell_exec("sh test.sh");
?>
And test.sh :
#!/bin/bash
/usr/local/php5.6/bin/php /homez.number/mysite/www/bin/console converseo:updateBilling
And I get the error :
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "converseo:updateBilling" is not defined.
Did you mean one of these?
converseo:updateBilling
converseo:updateEmailCount
converseo:updateTelephony
converseo:updateEmail
The lines are EXACTLY the sames, I don't understand why I get this error.
Thanks a lot for yours answers !

Run gradle from PHP file

I am trying to compile an android app from a PHP file:
<?php
$command = "sh build.sh";
$output = shell_exec($command);
print $output;
?>
My file build.sh:
#!/bin/bash
export JAVA_HOME=/var/www/vhosts/mydomain.com/android-sdk/jdk1.8.0_73/
export PATH=${PATH}:${JAVA_HOME}/bin
export GRADLE_HOME=/var/www/vhosts/mydomain.com/android-sdk/gradle-2.11/
export PATH=${PATH}:${GRADLE_HOME}/bin
export GRADLE_USER_HOME=/var/www/vhosts/mydomain.com/workspace/gradle_user_home/
gradle --info --debug build 2>&1
When I open the PHP file in the browser, I get this error:
* What went wrong: 00:45:56.317 [ERROR] [org.gradle.BuildExceptionReporter] Failed to load native library 'libnative-platform.so' for Linux amd64.
If instead of Shell_exec I use exec() it shows just this:
[ERROR] [org.gradle.BuildExceptionReporter]
If I run gradle from the command line, everything works fine, I think the issue must be with the user, with shell_exec() the gradle command is exectuted with apache user, but I can not find a solution.
Thanks!

update crontab file using php script

I have to create a crontab file using php script.This is my code :
<?php
file_put_contents('/tmp/crontab.txt','* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>
the file crontab.txt is created.But the next command 'crontab /tmp/crontab.txt' is not executed.When I type the command 'crontab -l',i get the output : 'no crontab for root'.But when I manually execute the command in terminal,the crontab is installed correctly.Why cant I execute the same command using my php script?
The server is not (should not be) running as root. Check the crontab for the HTTPd user (on debian/ubuntu running Apache, it's www-data).

system command run from console but not from crontab in linux

I have tried by writing the below command in php file and then run that php file from console as well as crontab.The code which i have in php file is like
#!/usr/bin/php -q
<?php
system ("/usr/local/sbin/googletts-cli.pl -o /var/lib/asterisk/sounds/asdapp/test.wav -s 0.9 -l en -t 'Some text is written here'");
?>
If i run this php file from consol then it is working fine and generate the wav file.But if i put that php file in crontab like
* * * * * /var/lib/asterisk/agi-bin/asdapp/jagu_test.php
Then it is not generating wav file.
I have also tried the solution which is given in below links:
Why is crontab not executing my PHP script?
PHP script works from command line, not from cron
I have tried with various way for run this php file from crontab but can not get any success.Can anybody know what is the exactly issue or any solution?
Commands executed from crontab and commandline have different environment variables, working path and user (if you don't specify otherwise).
Also, you are invoking PHP interpreter just to invoke a system command. It would be much easier to put this line in the crontab itself:
* * * * * /usr/local/sbin/googletts-cli.pl -o /var/lib/asterisk/sounds/asdapp/test.wav -s 0.9 -l en -t 'Some text is written here'

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

Categories