Generate android apk using shell script from php? - php

I'm trying to generate Android apk files from shell script, I want to execute a shell script file from PHP. When I run the shell script in the terminal, it works perfectly. If I try to run the script using PHP, the shell script doesn't execute all the commands. The ls command in shell script works perfectly, but when executing using PHP, other commands doesn't work. I'm using xampp server in a Linux environment.
My shell script
cd /home/user/AndroidStudioProjects/msvep4247-inghamautogroup-pulse-and/
./gradlew assembleDebug
cp -fr app/build/outputs/apk/app-debug.apk /opt/lampp/htdocs/sample/apk
ls
Shell script ls output
app autolead_data_format.pdf build build.gradle cheek gradle
gradle.properties gradlew gradlew.bat lib local.properties
msvep4247-inghamautogroup-pulse-and.iml settings.gradle
My PHP script
<?php
echo shell_exec('ls');
echo shell_exec('./generateApk.sh');
?>
PHP script ls output
generateApk.sh generate.php APK
Note: ls outputs file names in the folder
I set all the file permissions for shell script in the xampp server. Can anyone describe where I'm mistaken? Awaiting responses...

Just use the full path to the script/executable, because the environment is different when running from php.

It seems that PATH environment variable in PHP code that is executed in the web server is more limited than the one in the shell you're working in. But you can change environment variables in PHP, and the commands you start from it will see those changes.
<?php
// set content type so the output is more readable in the browser
header('Content-Type: text/plain');
// set $PATH to some limited value
putenv('PATH=/bin:/sbin');
// verify, note that we have to use full path to 'env'
print(shell_exec("/usr/bin/env|grep '^PATH='"));
// this command won't run (assuming its full path is /usr/bin/id)
print(shell_exec("id"));
// add more directories to $PATH
putenv('PATH=/bin:/sbin:/usr/bin:/usr/sbin');
// verify again, we can use env without specifying the path this time
print(shell_exec("env|grep '^PATH='"));
// this command will
print(shell_exec("id"));
So you have to write putenv('PATH=<your_shell_PATH_contents>'); at the top of your PHP script. Using full path to the shell script alone won't help if the script itself uses relative paths to binaries it starts.

Related

Adb shell commands are not working in php

adb shell commands are not working in php but they are working in command prompt.
In window 7, I tried to execute 'adb devices' command via php as below. I also tried with shell_exec() and system(). But none of them are working. I ran 'ipconfig' command in php and it's working fine. It's just 'adb' that's not working.
<?php
exec('adb devices', $output);
echo '<pre>'.$output.'</pre>';
?>
I want it to return me the list of devices attached to the pc.
the issue you have is connected to the PATH variable. This variable contains all paths where the shell tries to look for executables.
Your personal terminal should have configured the PATH variable to contain the absolute path to Android tools. Unfortunately, it's not shared with PHP runtime.
One of the solutions can be to use absolute executable path in your code:
exec('C:\any_path\Android-SDK\bin\adb devices', $output);
You can either make the PATH variable used by PHP process to contain the absolute path to Android tools.

Execute a bash file located on a different VPS

I have a php webpage located on Webserver1, which is from Host1.
I also have a bash script located in Gameserver1 which is from Host2.
Is there any way to send a command from Webserver1 to Gameserver1 to execute the bash file? The webpage and file are on different VPSs. Both are running Debian 7.
The script is literally one line, to execute a java command via a screen, so the server can start if a player notices it's down. The command's available already so it doesn't need to be a secure way of hiding what the command is.
There are 2 ways I can think of. either create a bash file in Webserver1 that connects through ssh and executes the bash script you need on Gameserver1. then run it through php with exec() command.
Or you can create a php file in Gameserver1 that uses exec() to execute the bash script you need on Gameserver1 and call it using file_get_contents() on Webserver1, which is not that secure since anyone can call that file and run your script.

Using the `where` command through PHP

I am trying to get the path to certain exe's using the where command in the command prompt on windows.
Here is what i did in command prompt.
where g++
where java
where javac
where python
All of these are giving the correct output of the path in the console window which indicated that I have set the environment variables correctly.
But now When i try to run the commands using the shell_exec() function in PHP, only the call to where java and where python gives the correct output. I was even able to successfully execute a respective test file using these commands through PHP.
But strangely, where g++ and where javac give this error in the browser when run through PHP:
INFO: Could not find files for the given pattern(s).
Also if I get the outputs of these two commands on the console and then copy that into my script to compile a c++ or java file, it works perfect. But the where command returns the above INFO when run through the PHP script.
I am running the server on localhost using XAMPP. Any idea what is missing?

Why doesn't PHP exec() command doesn't run?

I have a very simple PHP script to try to use the exec command. The code is
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo("test<br>");
echo exec('whoami');
echo("test");
?>
However when I access the page, it just prints out:
test
test
It does not print the output from "whoami" command, which when run from command line displays
me
What is the problem here? I cannot work it out.
Thanks a lot!
(on a system with the "whoami" executable in the path)
PATH, like all environment variables, is not system-wide but ultimately per-process.
That is, though your typical bash session may well include the path to this executable in PATH, the environment in which your PHP script is running (say, an Apache CGI context) does not. This may be for any reason.
Provide an absolute path to whoami, instead.

Integrating php in shell scripts for a cronjob?

I would like to execute a cronjob for a routine task every X hours. The cronjob basically executes a shell script which in turn uses a WGET command to download files from a remote server. However, before I run this shell script I want the cronjob to execute a php script which will check whether the update's available (there's no point in wasting BW and downloading the same file over and over again) and if it is, it should pass on the update URL to the shell script which in turn uses the WGET command.
The cronjobs are set from the hosts Admin Panel. There is no other way around it. Being a shared hosting service, I am not allowed access to other functions on PHP which might do the task for me either.
Is this possible? I am Linux illiterate. I have installed a few RPM's on Fedora but that's about it. Please bear with me. Thanks!
Just pass --timestamping to your wget command.
Alternatively if you are more familiar with PHP's ways you can check this question for a usable method.
Use a curl HEAD request to get the file's headers and parse out the Last-Modified: header.
To use a php script as a regular command line executable use this as a starting point:
#!/bin/env php
<?php
echo "Hello World\n";
Save the file without the .php and tuck it somewhere that your server won't serve it.
Next, set the executable bit so that you can execute the script like a regular program
(u+x in the following command means grant the [u]ser e[x]ecute privileges for helloworld, and chmod is the command that unix variants use to set file permissions)
Omit the $ in the following sequence, as it represents the command prompt
$ chmod u+x helloworld
now you can execute your commandline script by calling it in the bash prompt:
$ ls
helloworld
$ ./helloworld
Hello World
$
From here you can get the full path of the executable script:
$ readlink -f helloworld
/home/SPI/helloworld
And now you can install the cronjob using the path to your executable script.

Categories