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.
Related
I am trying to write a script in php to run the following bash script using apache2+php7
#!/bin/bash
#cd /home/cwc/http/www/html/admin/web/
nowfile=$(date +"%Y%m%d-%H%M%S")
nohup tcpdump -w $nowfile.pcap -i enp2s0 >> /dev/null 2>&1 &
Now I understand I might have to use full paths
The above code works with a non sudo user using bash because I added a user the the pcap group.
I'm trying to figure out why this will not work with php
<?php
$command = "/pathtoscript/tcpdmp.sh
shell_exec($command); //not working?
?>
Firstly, we wanna move the changes from one system to another system and for this, we have a shell script in synchfolders.sh file as follows
rsync -av --delete -e 'sshpass -p Wordpress#123 ssh -p 22' root#192.168.2.94:/var/www/html/prosync/wp-content/plugins/ /var/www/html/devsync/wp-content/plugins >> /var/www/html/devsync/wp-content/mysynclog.txt
and we want to execute this shell script in PHP file by shell_exec()
and while executing this PHP file from a browser other than rsync command, all are executing but the rsync is not executing. We have searched the stuff in SO and we got the link php exec() rsync ssh to remote server not working
as said here, we have tried the PHP file execution from the command line and works perfect but not through the browser. Why, Please let us know where we did a mistake. Thanks in advance
Enter the full path of rsync command:
/usr/bin/rsync -av --delete -e 'sshpass -p Wordpress#123 ssh -p 22' root#192.168.2.94:/var/www/html/prosync/wp-content/plugins/ /var/www/html/devsync/wp-content/plugins >> /var/www/html/devsync/wp-content/mysynclog.txt
I am a beginner in using Docker, and I am trying to run docker from php, specifically I need to run openface code. I used the command lines provided here https://cmusatyalab.github.io/openface/setup/ to make sure that docker is running on my pc correclty and it works. but now I need to call it from PHP, I wrote the same commands in batch file as follows
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash
cd /root/openface
./demos/compare.py images/examples/{lennon*,clapton*}
pause
and tried to execute it in php by calling
echo shell_exec ("test.bat");
but when I run the batch file directly, only the first line is executed. as it seems the next command is not being executed inside the docker container.
how can I make all commands execute?
any help will be very much appreciated,
thank you
The problem is the first bash won't exit before you exit it and the rest of the commands are interpreted by host bash.
What you need is that your work is done and then you get inside a bash (inside the container)
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash -c "cd /root/openface && ./demos/compare.py images/examples/{lennon*,clapton*} && exec bash"
First "bash -c" is to execute the commands and last command exec bash override the main bash and gives you a shell
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.
Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}#{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");
For example:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa user#192.168.0.19 '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");
All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.
I think you are missing an & at the end of your command for sending the execution to the background.