3

I want to bundle two simple windows bat files with my PHP app that can start and stop a PHP builtin web server to test the app on localhost.

This is what I've got on winlaunch.bat so far:

php.exe -S 127.0.0.1:80 -t path/to/my/app/webroot my_app_bootstrap_script.php

Things I don't know how to do:

  • How do I redirect the output to a log file?
  • How do I auto close the command window if invoked from the windows explorer, keeping the app running?
  • What do I put in the stop script?
  • How do I check if the app is already running?

1 Answer 1

4

The redirection character on Microsoft Windows as on other operating systems such as Linux, OS X, etc. is ">". So you could redirect the output as follows:

php.exe -S 127.0.0.1:80 -t path/to/my/app/webroot my_app_bootstrap_script.php > "C:\somedir\php.log"

Modify the directory path and file name to be whatever you like. You don't need double quotes around that information unless you have a space in the directory path or file name, but you do if there is a space and it doesn't hurt to include them in either case.

Note: when you use the ">" character, if any file already exists with the location and name you've specified, that file will be overwritten. If you, instead, want the log file to keep growing, i.e., you want to append to an existing file the next time you start php.exe, then use >>, instead of >. That will result in the file being created if it doesn't exist, but the output will be appended to an existing file.

You can use the taskkill command to terminate a process. E.g., you could use:

taskkill /f /fi "imagename eq php.exe"

The /f may not be needed, but with that option you can have the taskkill command force processes to close that might not otherwise close. The /fi filters the output of the taskkill command. In this case you would be filtering so you only see processes named php.exe. If the process isn't running you will see the following if you issue that command:

C:\>taskkill /fi "imagename eq php.exe"

INFO: No tasks running with the specified criteria.

You can check if php.exe is already running using the tasklist command:

tasklist /fi "imagename eq php.exe"

That will show you all instances of a process named php.exe that are running. In some cases you can have multiple processes with the same name, e.g., chrome.exe. In other cases, such as explorer.exe, you should only see one and will see something like the following:

C:\>tasklist /fi "imagename eq explorer.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
explorer.exe                 91484 Console                    1     37,424 K

You could also check on whether it is running by "piping" the output of the tasklist command to the find command with the "|" pipe character. E.g.:

C:\>tasklist /fi "imagename eq explorer.exe" | find /i /c "explorer.exe"
1

The /i instructs find to ignore the case of letters, e.g., uppercase and lowercase letters don't matter. It probably isn't needed in this case if the process will always be named "php.exe", but I mentioned it in case you might use find "Php.exe" when the process is named "php.exe". The /c tells find just to count the number of occurrences of the string you told it to look for.

If the result is "1", or greater if the process can run more than once, then it is running. If it is "0", it is not running. In your case the answer will likely always be 0 or 1 and you would change "explorer.exe" to "php.exe". If you wished you could test the result and issue the taskkill command if the value is not zero or you could just issue the taskkill command any time you want to stop the process if it is running. If it isn't running the taskkill command will just report "INFO: No tasks running with the specified criteria."

If you don't want the window to remain open, you can use the "start" command in your batch file. E.g.,

start "PHP" "php.exe -S 127.0.0.1:80 -t path/to/my/app/webroot my_app_bootstrap_script.php"

The "PHP" after the command is providing a title for the window. You can issue the command start /? at a command prompt to see the options for the start command.

1
  • Thank you so much for taking the time to answer all my questions. I'll keep this on my bookmarks because I am sure I'll need it again sooner or later ;-)
    – NotGaeL
    Jul 25, 2015 at 18:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .