I installed PM2 and node.js on the Windows server to run JSMTProxy-master script.
The command for the run proxy is: (pm2 start "JSMTProxy-master\mtproxy.js" -i max) and it executed successfully in the CMD window with no problem.
Now I want to execute this command by PHP and exec.
I tried this:
exec ('pm2 start "JSMTProxy-master\mtproxy.js" -i max');
Or this: (just showed up cmd.exe in taskmgr)
exec ('c:\WINDOWS\system32\cmd.exe /c START "pm2 start c:\mypath\JSMTProxy-master\mtproxy.js -i max"');
Or this:
exec ('c:\WINDOWS\system32\cmd.exe /c START "JSMTProxy-master\mtp-run.bat"');
These don't work and nothing happens. no error, no process showed up in taskmgr.
How is it supposed to be to run by exec?
Note. I have PHP installed and working perfectly.
After trying different ways many times, Finally i found the problem.
"PM2" doesn't execute under PHP exexc. It has to be with full path of exe files.
I changed my command as below and it worked:
shell_exec('C:\Users\Administrator\AppData\Roaming\npm\pm2.cmd start "C:\mypath\JSMTProxy-master\mtproxy.js" -i max')
Related
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.
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/
I try to execute this command with the PHP command exec:
exec ("gphoto2 --capture-image-and-download --filename ".$path, $output, $returnVal);
I use a valid filename and when I execute the command with the parameters of the variables in bash, it works. However, with php it doesn't. I don't get a reply of the exec command.
I'm using raspbian with lighty and the php5 module. That works with other samples fine.
I tryed shell_exec without success, too.
Can someone help me?
on my local machine I execute the following:
exec('sudo nohup '.PHP_BINDIR.'/php '.ROOT.'/index.php controller=imports action=import id=12 > /dev/null 2>&1 &');
where "ROOT" is a constant with the complete path.
Everything works fine and the script is executed asyncronously with no problems.
When I run the same script on production environment (a bitnami LAMP stack running on AWS) instead: if I run it directly from the terminal it works, when I run it from php it doesn't.
I have nothing in the apache error_log and if I run it with shell_exec the returned value is NULL.
If I try to execute a different script from php (like exec('whoami')) it works, so I don't think is a permissions thing.
I also tried replacing php command with php-cli, nothing changes.
Hope someone can help.
Thanks in advance.
I have a bash script:
run.sh
#!/bin/sh
cd /var/www/project/bin/
CMD="./executable <full_path_to_file>;
$CMD
When I run this program from the terminal. (i.e. ./run.sh, it works fine)
However when I call it from PHP:
system("full_path_to_sh_file", $out);
It calls the script successfully, and even runs the executable, but now the executable throws an error saying that the supplied file can't be found.
Any ideas?
How do you run PHP script, from webserver or command line?
If from webserver, what user is it run (httpd or apache?)
Make sure the environment as same as when you are running from terminal (for example: same user)
Try this if running as different user:
sudo -u apache /fullpath/run.sh