How to launch bat file on server with PHP (From Website) - php

I am hosting a website and minecraft server off of my windows computer, with IIS. They both work perfectly except for that I'd like a way to start the minecraft server from the website. I have a batch file I run to start it regularly so I thought perhaps I could launch that batch file. Some things I've tried:
exec("cmd.exe /C start server.bat");
shell_exec('start server.bat');
I've given IUSR and IIS_IUSRS all permissions on cmd and on the server folder. If it's easier I could also try just running the code within the batch file, from php too. Here is what's inside the server.bat:
cd C:\Users\username\Desktop\_MCServ
:begin
java -Xms3072m -Xmx3072m -Dlog4j.configurationFile=log4j2.xml -jar spigot-1.14.jar
timeout 5
echo resuming server...
goto begin
The loop is so if the server stops, it can automatically start itself again.

In order for this to work you need to run it using cmd and give it an exact path to the bat file. The following code should work for what you need.
system("cmd /c C:[path to file]");

Related

PHP exec() and command line switches

So I am using PHP exec() to run a batch file on my server:
echo exec("printCountries.bat");
The batch file opens Microsoft Access 2007 and runs a macro to print a form and then close Access:
START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
I can run the batch file from the command line successfully and the form prints to our network printer (this is for a company intranet) and closes Access. But when I run the PHP script, the batch file only runs the Access Process but does not perform the command line switches or open the database file.
I have configured PHP to have the right privileges on IIS, I am just stumped as to why the command line switches don't work and the batch file won't open the database.
Any ideas?
here is the echo output:
C:\inetpub\wwwroot\scale>START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
It looks like you have some access rights problem.
Did you try to run in PHP this echo exec("whoami");
What username you get?
Something like "nt authority\iusr" ?
You have to give to the default IIS user (i.e. IIS_IUSRS) full access to the path that your scripts lives.
In addition you can change (only TEMPORARILY huh?) the default application pool user from ApplicationPoolIdentity to LocalService or LocalSystem to test the results...
If I were you, I would created a new Application in IIS then I would created a new Application Pool and I would assigned it to this app. Then I created a new user that will have access to all the appropriate directories (web dir and where the bat is located).
If nothing of the above worked I would test my php application from my command line (locally) using the php command in my local command prompt.
I am neither good with PHP, nor with Access. However, when you invoke the batch file from PHP, please create an instance of cmd.exe and pass "/c printCountries.bat" to cmd.exe. In other words, the command run by PHP exec should look something like this (assuming PATH is set correctly):
cmd.exe /c printCountries.bat
Also, inside printContries.bat, I think you will need to wait until the access process terminates. So it should look like
START /WAIT /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries

command working in cmd but not through php shell_exec

I have a command in cmd which looks like below
C:\wamp\www\editor\DocTo-master\exe\docto -f C:\wamp\www\editor\uploaded\uploaded_files_21_original\AffidavitinDIR-4.docx -O "C:\wamp\www\editor\uploaded\uploaded_files_21_original\pdf\21.pdf" -T wdFormatPDF
When I run this in cmd, it works absolutely fine and gives output(doc to pdf) as expected.
However when I put the same command in php shell_exec like
shell_exec('C:\wamp\www\editor\DocTo-master\exe\docto -f C:\wamp\www\editor\uploaded\uploaded_files_21_original\AffidavitinDIR-4.docx -O "C:\wamp\www\editor\uploaded\uploaded_files_21_original\pdf\21.pdf" -T wdFormatPDF');
I dont get the desired output with the above code in php.
Any help will be appreciated
Seems like most of the problems have to do with permissions or redirection.
Your current user credentials allow you to create files in that directory but does the webserver username?
Redirect stderr to stdout to get error information.
Is the quoting right?
Look in the webserver error logs.
I am the author of docto.
This is most probably a permissions issue in that php is not necessarily given the correct permissions to run word on your server.
I am running this myself on a Windows 2012 server running Word 2010, it works fine. DocTo is installed on a subdirectory above the Webserver root.
PHP is running as NTSystem Authority
Also I am using exec rather than shell_exec not sure if that makes any differnce
For fix this problem you must create Desktop folder in these paths:
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop
Then go to (Control Panel\Administrative Tools\Services) and right click on your apache service. go to "Log On" tab and enable "Allow service to interact with desktop" option.
Then reset your apache service.
http://pmlearning.info

How to run a php script from windows .bat

This is what i want to do in a batch file:
write a file to ftp folder
run php script (http://mylocation.url/script.php)
download a file from ftp folder
FTP i have read before, it is possible to do it in a batch file.
But i don't found a way to execute my php script on my linux server.
Cronjob is not possible for this solution cause the uploaded file will be changed from
Scipt and after i need it downloaded again.
Any one has a solution?
Thanks for help
Phil
plink tool can be used to execute commands in Linux server from Windows through SSH. Below is the syntax
plink.exe root#10.0.0.1 -pw password "<ur script execution command here>"
Say if I want to run a shell script I would type in the command "sh myscript.sh". To achieve this the Linux server should have sshd running.
The command can be used along with other commands part of the batch file.

Can't run "cd" command in PHP shell_exec()

I recently got my laptop with Apache setup on my university's Ethernet connection. Now I can connect to my computer from anywhere as long as I have either the IP address or host name (which I can choose). Now I want to create a Web-based command prompt that will let me run commands on my laptop from any device.
One problem is that I can't run the "cd" command. I have my PHP script setup so it can run a series of commands delimited by a newline character. So I run "cd ../" and then "pwd" but it's still in the root directory of my Web app. How do I fix this?
If you do this:
shell_exec("cd ..");
shell_exec("pwd");
Then the second command will be executed with a new shell, which has the same starting directory as the first had, because it's a subprocess of the current PHP.
The changing of the current directory with the first shell exec won't last to the second one. Such a series of depended shell commands only works by executing all at once:
shell_exec("cd .. ; pwd");
I think you have to change the directory of the current process/script. You do this with chdir. Then you can run shell_exec.
I assume you realize the severe security concerns your solution creates...

Php : running ssh from Windows to login to a Linux and run a script

Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.

Categories