I am actually writing a little PHP script, in order to retrieve files in folder on a FTP server and download them locally.
Another script put those file in the server.
So here's my question : how can I write a batch file that will execute the first php script, then run a program (a game in this case).
When the game is closed, execute the second script.
So it will be this process :
When run the batch file, execute the first script (get file)
When all files are downloaded, run the programm (game.exe)
After game closing, execute the second script (put file)
I think at a .bat file, but maybe there is an another solution.
Thanks is advance !
Telest.
EDIT :
Thanks for your answer.
Google gives me this post : How to wait for a process to terminate to execute another process in batch file.
I found that I can use /W in order to wait the program ends.
But /W seems not working. Tried /WAIT but no success.
Here is my batch file :
PHP C:\Users\PHProjects\test1.php
START /WAIT /B chrome.exe
PHP C:\Users\PHProjects\test2.php
PAUSE
(test1.php & test2.php are just dummy echo).
And the console result :
Chrome executes well, but test2 too
As you can see, Chrome executes well, but so do test2.php.
Is something wrong here ?
Thanks.
call getfile.bat
start "" /w game.exe
call putfile.bat
see start /? and call /?. With start take note of the second half which details how not using start works (confusing I know}. Call details, as start details for programs, how it works calling a batch directly versus call.
I updated my first post with new elements.
Related
I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual
2 part question,
I am running a script that executes a second script.
I have it setup this way because I read if I put a script in the /etc/init.d directory it will run it at start up.(true or false?)
I have tried adding >> LoopTriggerLogging.log at the end of each line but nothing comes out in the log file
So I have a first script as follows
#!/bin/bash
/var/www/Dev/LoopTrigger.sh
exit
This triggers the following script to run
#!/bin/bash
while true; do
# do some work
php /var/www/Dev/FindNearestDriverAndSendPush.php
php /var/www/Dev/ProcessCustomerPayment.php
php /var/www/Dev/ProcessDriversPayment.php
# write to LoopTriggerLogging.log
sleep 2 # sleep and repeat
done
What I would like is to have the commands logged along with any errors. I have tried to read a little on this but get lost in the answers and what they are trying to tell the user. I am still new at this and learning, kindly give definition to any commands or options. I am open to a best practice scenario.
Also, with putting in the etc/init.d directory will this tell the script to run at start up?
Is there a way to run this script without it taking up the command line because its an endless script?
My ultimate goal is to get the 3 php files to execute every 2 seconds with some sort of logging.
I did some reading on Cron but seems it is not meant for this type of use case.
Ive also seen this:
exec > logfile 2>&1 (dont know what this does)
set -x makes bash print every command before executing it
FOO=BAR (dont know what this means)
echo $FOO (dont know what this means)
if I put a script in the /etc/init.d directory it will run it at start
up.(true or false)
True. If you put a script in init.d then that script will run for every startup.
My ultimate goal is to get the 3 php files to execute every 2 seconds
You are using the correct way of running it approx every 2sec depending upon the time your php script takes to run. Crontab runs after a minimum of one minute so that would not an option.
I have tried adding >> LoopTriggerLogging.log at the end of each line
but nothing comes out in the log file
You can use /var/www/Dev/LoopTrigger.sh >> LoopTriggerLogging.log in your first script so that whenever it runs it will
Create a file for the first time and append the content from the next time.
Push all the logs of the second script into the file.
Note: As logs will keep on appending to the single file, this file will become very huge at some point of time so make sure your handle it well.
I have 2 websites, hosted on 2 different servers. They are kind of interlinked. Sometimes I just do stuff on Website-1 and run a script on Website-2. Like I edited something on Website-1 and now I want to run a script on Website-2 to update accordingly on it's server.
Till now I am using following code on website 1.
$file = file_get_contents('Website-2/update.php');
But the problem with this is that my Website-1 server script stops running and wait for the file to return some data. And I don't wanna do anything with that data. I just wanted to run the script.
Is there a way where I can do this in a better way or tell PHP to move to next line of code.
If you want to call the second site without making your user wait for a response,
I would recommend using a message queue.
Site 1 request would put a message to the queue.
Cron job to check queue and run update on site 2 when message exists.
Common queues apps to look at:
[https://aws.amazon.com/sqs/?nc2=h_m1][1]
[https://beanstalkd.github.io/][2]
[https://www.iron.io/mq][3]
[1]: https://aws.amazon.com/sqs/?nc2=h_m1
[2]: https://beanstalkd.github.io/
[3]: https://www.iron.io/mq
What you're trying to achieve is called a web hook and should be implemented with proper authentication, so that not anybody can execute your scripts at any time and overload your server.
On server 2 you need to execute your script asynchronously via workers, threads, message queues or similar.
You can also run the asynchronous command on your server 1. There are many ways to achieve this. Here are some links with more on this.
(Async curl request in PHP)
(https://segment.com/blog/how-to-make-async-requests-in-php/)
Call your remote server as normal. But, In the PHP script you normally call, Take all the functionality and put it in a third script. Then from the old script call the new one with (on Linux)
exec('php -f "{path to new script}.php" $args > /dev/null &');
The & at the end makes this a background or non-blocking call. Because you call it from the remote sever you don't have to change anything on the calling server. The php -f runs a php file. The > /dev/null sends the output from that file to the garbage.
On windows you can use COM and WScript.Shell to do the same thing
$WshShell = new \COM('WScript.Shell');
$oExec = $WshShell->Run('cmd /C php {path to new script}.php', 0, false);
You may want to use escapeshellarg on the filename and any arguments supplied.
So it will look like this
Server1 calls Server2
Script that was called (on Server2) runs exec and kicks off a background job (Server2) then exits
Server1 continues as normal
Server2 continues the background process
So using your example instead of calling:
file_get_contents('Website-2/update.php');
You will call
file_get_contents('Website-2/update_kickstart.php');
In update_kickstart.php put this code
<?php
exec('php -f "{path}update.php" > /dev/null &');
Which will run update.php as a separate background (non-blocking) call. Because it's non-blocking update_kickstart.php will finish and return to searver1 which can go about it's business and update.php will run on server2 independantly
Simple...
The last note is that file_get_contents is a poor choice. I would use SSH and probably PHPSecLib2.0 to connect to server2 and run the exec command directly with a user that has access only to that file(Chroot it or something similar). As it is anyone can call that file and run it. With it behind a SSH login it's protected, with it Chrooted that "special" user can only run that one file.
Thats what I've been trying to do for days now, and I can't find a solution.
I have managed to launch the cmd-line OR any given program via php, but I need a program to start within the cmd-line, give it a static command and write the return values into a .txt.
What I want to do
1.The user clicks on a link
2.That executes a php script which automatically:
-opens the command line
-starts a program within the command line
-enters somme commands (e.g process the file, write result to a .txt-file)
It just doesnt work, no matter how I've tried it.
I just don't understand how I should approach the whole thing.
Edit:
Everything is supposed to run on the server! I want to start a biometrics program that compares a picture with another one and return a percentage value.
And I am running windows with xampp.
How do I execute cmd commands through a batch file?
This helped me out a lot. I now have a batch file that does the following:
#echo
c:\
cd C:\Users\XXX\Desktop\OpenBR-0.5.0-win64\bin
start br -algorithm FaceRecognition -compare C:\Users\XXX\Pictures\1.jpg C:\Users\XXX\Pictures\2.jpg C:\Users\fxw2_000\Desktop\result.csv
pause
Aaaaaaand it works. Awesome!
If i run shell_exec(php file) , will it activate the shell_execution and continue with the php file, or will it try to complete everything in the shell_executed php file first, then run the rest of the php file that executed it.
It will complete the shell execution first and then it will run rest of the code in the php file.
shell_exec(), as stated in the Documentation, will return the complete output as a string. So it has to be a "blocking" function. That means it will block the execution of the rest of your code until it is complete.
Depending on your command that you want to execute, you may want to force the process to run in the background with the & character at the end of the command. This is assuming ofcourse that you are running on a unix based server.