How to execute Windows10's Bash command from PHP? - php

Windows 10 just released Anniversary Update today. Now you can use Ubuntu flavored bash command from Linux subsystem.
The question is: How to execute Windows10's Bash command from PHP?
I tried
<?php
exec('bash',$out1,$result1);
exec('ls -l',$out2,$result2);
var_dump($out1);
var_dump($result1);
var_dump($out2);
var_dump($result2);
It doesn't work. All $out are empty array, and both $results are 1.
Any idea?
Just found out that I can run web server directly from subsystem.
e.g.
$ sudo apt-get install apache2
$ sudo service apache2 start
Then put all web contents inside subsystem's directory located at %localappdata%\Lxss\rootfs
At this point I can execute bash script however I want.

You can not. Ubuntu on Windows is implemented as a different subsystem directly below the Windows Kernel. So it runs separate to normal Windows processes and can not interact with them. (At least that is how I understand it). But maybe you just have to use C:\Windows\System32\bash.exe as the command.

Bash is not meant to be used to run a server but you can run PHP-CLI on bash without an issue.
Instead of running the PHP script in Windows and then accessing bash it would be easier to create a command to run the PHP script directly in bash.
So you would run `bash.exe -c "php path/to/php-script.php"
https://blogs.msdn.microsoft.com/commandline/2016/10/19/interop-between-windows-and-bash/

Related

Linux $PATH is different for the same user, depending on how it is accessed

I have a php script running in a cronjob on the server.
However, I am unexpectedly getting different $PATH from the same user, depending on how I execute the command.
I log in as user ubuntu:
ubuntu#:$ echo $PATH
/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/.nvm/versions/node/v12.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I then sudo su bitbucket:
bitbucket#:$ echo $PATH
/home/bitbucket/.nvm/versions/node/v12.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
I execute a script from a cronjob running as bitbucket and output the following debug to a log file:
$ whoami
bitbucket
The above proves the user is bitbucket, then:
$ echo $PATH
/usr/bin:/bin
Please note I am not running as sudo. I am utilising sudo to switch user, but not using sudo to echo $PATH.
How is it that the same user has 2 different $PATH variables?
You didn't say which shell you're using so I'm going to assume it's bash. The first, and perhaps most important, thing to note is that when you run sudo su bitbucket you're getting an interactive shell. Which means that ~/.bashrc will be sourced. Lots of people modify PATH in that script. Something that tends to cause problems. Why? Because non-interactive shells, such as the one launched by cron to run your command, won't read ~/.bashrc.
Your cron job gets a PATH equivalent to running this command: sudo su bitbucket -c 'echo $PATH'. Play around with that to get a better understanding of how this works. For example, instead of echo $PATH try env.

How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good,
but I can't run exec('gitbook xxxx').
gitbook is a npm package I installed by
npm install gitbook -g
I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code?
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && gitbook build);
after I added export PATH, everything works fine.
I tried once like this on UNIX-based OS:
You can run shell commands via the exec() function:
// make an php file to execute shell script
exec("node yourscript.js &", $output);
Well output here become array of each line of output along with process id. You can kill process by processid also.
exec("kill " . $processid);
This how I was did. Other then this you can use node supervisor. Hope it will help you. Try your also with node command.

Execute PHP script on linux with PHP LAMPP service

So, I have that question. I've installed xampp on my ubuntu, and I run my web system from there ( /opt/lampp/htdocs ). I can open my php web systems from navigator with no troubles. So now I want to run a php script from linux console, like this:
php -f file.php
but ubuntu tells me to install php7.0-cli or hhvm packages to run a php script on console. The real question is, that I've installed lampp running php there. Is there any chance to run a php script on console with the lampp's php service? or I have to install those packages for ubuntu anyway?
When you run:
php -f file.php
from the linux terminal you are excecuting the system php enviroment (in ubuntu you install it using apt-get install), if you need run the script with the LAMPP php environment you must run:
/opt/lampp/bin/php -f file.php
If you run this command repeatedly, you can set alias by appending the following command in the "~/.bash-aliases" file or by directly running the command in the terminal:
alias php='/opt/lampp/bin/php'

running docker request from php

I have encapsulated pdftk (PDF tool kit) as program that can be run inside of a fedora docker image. This allows me to run pdftk on a newer AWS AMI image that does not support pdftk. When replacing pdftk in our PHP program with the docker run command, it works great when running the PHP program from the command line, but when running it as a cron job or as a mailbox command, docker will not run. I have made the cron and mailbox users part of the docker group, checked permissions and paths, but still no joy.
The exec PHP command looks like:
$command = 'docker run -i -t --privileged -v /var/www/mbx/forms/inbox:/workdir -w /workdir/ brnlab/aultman-pdftk ./'.basename($files[$i]).' burst output ./pg_%03d.pdf';
Oddly the return on the $command when running this as a cron or mailbox command is blank, so I am not sure if there is an error or not. It does not appear to hang up. Any ideas on the best way to diagnose the issue further?

Run GruntJs task from php exec function

I would like to perform some grunt task by running command from php exec function.
I've already:
Install grunt globally: npm -g install grunt-cli
Setup apache and windows to allow running CLI from php (I use
wampserver)
I ran this script from php:
<?php
exec('grunt --gruntfile Gruntfile.js');`
And I found in apache log that grunt command is not found whereas I can run this command from CLI and grunt is in my env path.
So I've decided to create a batch file that contains that script and launch the batch instead of the grunt command directly.
<?php
exec ('gruntTask.bat'); // I know that it can execute script because I can
// create folder if I put mkdir command in gruntTask.bat`
gruntTask.bat contains:
grunt --gruntfile Gruntfile.js
But why it couldn't execute this script? Have I missed something?
Need helps guys,
Regards,

Categories