PHP exec() and command line switches - php

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

Related

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

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]");

start a program from cmd with full permission

i have an application called app.exe it's create a file called account.txt in the same folder c:\ , the problem is that when i run the program from php exec function or cmd the account.txt file is not creating
i think the problem is from the permissions.
Run From: What happened
Manualy Executed and created the file account.txt
PHP exec Executed but it did't create account.txt
CMD Executed but it did't create account.txt
for php i use:
exec("C:\\windows\\system32\\cmd.exe /c START c:\app.exe");
for cmd i use:
START C:\app.exe
A program that requires elevation should specify so in its manifest. This would not help with the PHP issue though, especially if this is a web server using PHP. It is not possible to elevate without showing the UAC UI.
I'm not aware of a simple way to request elevation when executing something in Cmd. You might just have to start Cmd elevated (Right-click and "Run as Administrator").
PHP might be running as a different user but you gave us zero information about your setup so I don't know.
Without knowing anything about app.exe nor your Windows version or general configuration it is hard to give specific advise.
If you want to confirm that it as a permissions problem you can try running Process Monitor so you can see why the file operation fails.
You should be able to use the runas command
runas /profile /user:*admin user here* “*path to program here*”
you will then be prompted for that users password and after that it should run as admin.

Integrating php in shell scripts for a cronjob?

I would like to execute a cronjob for a routine task every X hours. The cronjob basically executes a shell script which in turn uses a WGET command to download files from a remote server. However, before I run this shell script I want the cronjob to execute a php script which will check whether the update's available (there's no point in wasting BW and downloading the same file over and over again) and if it is, it should pass on the update URL to the shell script which in turn uses the WGET command.
The cronjobs are set from the hosts Admin Panel. There is no other way around it. Being a shared hosting service, I am not allowed access to other functions on PHP which might do the task for me either.
Is this possible? I am Linux illiterate. I have installed a few RPM's on Fedora but that's about it. Please bear with me. Thanks!
Just pass --timestamping to your wget command.
Alternatively if you are more familiar with PHP's ways you can check this question for a usable method.
Use a curl HEAD request to get the file's headers and parse out the Last-Modified: header.
To use a php script as a regular command line executable use this as a starting point:
#!/bin/env php
<?php
echo "Hello World\n";
Save the file without the .php and tuck it somewhere that your server won't serve it.
Next, set the executable bit so that you can execute the script like a regular program
(u+x in the following command means grant the [u]ser e[x]ecute privileges for helloworld, and chmod is the command that unix variants use to set file permissions)
Omit the $ in the following sequence, as it represents the command prompt
$ chmod u+x helloworld
now you can execute your commandline script by calling it in the bash prompt:
$ ls
helloworld
$ ./helloworld
Hello World
$
From here you can get the full path of the executable script:
$ readlink -f helloworld
/home/SPI/helloworld
And now you can install the cronjob using the path to your executable script.

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...

How to run a php script through the command line (and keep it running after logging out)

I am trying to run a php script on my remote Virtual Private Server through the command line. The process I follow is:
Log into the server using PuTTY
On the command line prompt, type> php myScript.php
The script runs just fine. BUT THE PROBLEM is that the script stops running as soon as I close the PuTTY console window.
I need the script to keep on running endlessly. How can I do that? I am running Debian on the server.
Thanks in advance.
I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.
You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.
nohup php myscript.php >myscript.output 2>&1 &
This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.
The final & causes the script to run in the background so you can do other things whilst it is running or logout.
An easy way is to run it though nohup:
nohup php myScript.php &
If you run the php command in a screen, detach the screen, then it won't terminate when you close your console.
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.

Categories