Calling php.exe process via psexec and hiding window - php

I'm trying to call a php script via psexec (I need to fork/detach the process, and in Windows it's the only way I managed to do it). It all works fine, but it pops up a console window that I'd like to hide. I've tried both using the start command, and also downloaded nircmd, neither of which worked (meaning, command runs fine, but window always pops up). I'm looking for any other way of making this work. Or maybe my calls are just wrong. Here are some examples of calls I tried:
nircmdc exec hide psexec.exe -i -accepteula -w "c:\MyApp" -d "C:\Program Files (x86)\PHP\v7.0.4\php.exe" app.php start test
I then tried putting the command in a batch file and using nircmd to call that:
nircmdc exec hide startWorker.bat "c:\MyApp" "C:\Program Files (x86)\PHP\v7.0.4\php.exe" test
--startWorker.bat--
psexec.exe -i -accepteula -w %1 -d %2 app.php start %3
Finally, I tried the start command:
start "Worker" /d "c:\MyApp\lib\bin" /b psexec -accepteula -w "c:\MyApp" -d "C:\Program Files (x86)\PHP\v7.0.4\php.exe" app.php start test
Again, all of the commands above successfully run the app, they just don't hide the window. I'm trying this on Windows 7.

Related

Unable to change working directory using shell_exec() in php

Iam working on my own php MVC. And now, I wanted to create a cli tool for starting a live server which can be used for development purposes. I'm using laravel-zero for building my cli. All the commands are working fine except the command that is supposed to start a server. To start my server, first I need to change my current working directory to public and then start a php server using 'php -S localhost:9000'
I have tried the below code, but it doesn't work:
shell_exec("cd public");
shell_exec("php -S localhost:9000");
I have also tried this, but this too doesn't work:
shell_exec("cd public ; php -S localhost:9000");
Finally after lots of google searches, I tried this, but the same result:
chdir("public");
shell_exec("php -S localhost:9000");
All It shows is just 'The system cannot find the path specified.'
If your current directory when running shell_exec() is your installation root directory, you can just add the path after php
shell_exec("php -S localhost:9000 -t public/");
But since you are using laravel, why dont you just run php artisan serve

Run MTProxy remotely by PHP exec on Windows server

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')

Running SQLCMD from bat file loops infinitely

I have the following SQLCMD which connects to a remote database executes a query ands saves it as a csv file.
sqlcmd -S tcp:0.0.0.0,1433 -U user -P pass -d mydb -W -w 999 -s "," -Q "SELECT * FROM dbo.CSVData" -o "C:\wamp\sqlcmd.csv"
This works fine when i run it manually from command prompt.
I need to run the sqlcmd command from php and i have tried several execution commands in php but none works. The only option is using .bat files and call them through php but when i do so the sqlcmd command runs continuously (for several hundred times) and quits. I have tried using /wait and exit but this makes it worse by opening hundreds and hundreds of command prompt windows and it freezes the system. Below is the code i tried. Please let me know what i am doing wrong. Thanks.
#echo off
echo Running sqlcmd
start /wait sqlcmd -S tcp:0.0.0.0,1433 -U user -P pass -d mydb -W -w 999 -s "," -Q "SELECT * FROM dbo.CSVData" -o "C:\wamp\sqlcmd.csv"
:exit
I found what the issue was. I named the .bat file as sqlcmd.bat and that caused the program to run infinitely. I renamed the .bat file and it is working perfectly. A stupid mistake. My bad.

Running Linux DAEMON PHP files, does not execute exec, but does when ran from console

I have a php script that works perfectly when ran form command line; including the exec().
The exec I do are calling some .sh scripts.
These .sh script are of type bash
So I call them:
exec('/bin/bash /home/user/soft/bin/mybash.sh > /dev/null');
It works great when I run my php from command line I see all the instructions; works also when use with an alias. but when I it via a daemon:
sudo -u myuser php -f /home/user/bin/serverwait.php eveything that should be done in the .sh are not being done. but the rest of the file is doing what it is suppose to do (it checks for open ports and write to a log file)
I tried looking at my php.ini for cli and all seems right.

exec/system() - script being called works until called from PHP

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

Categories