How do i know if a php script is running?
I have a Show God is language agnostic but it would be nice to have a solution that works on windows as well.
asked Sep 22, 2008 at 20:03 I had the same issue - wanting to check if a script is running. So I came up with this and I run it as a cron job. It grabs the running processes as an array and cycles though each line and checks for the file name. Seems to work fine. Replace #user# with your script user.
answered Dec 11, 2011 at 17:07
JayJay 1211 silver badge2 bronze badges In linux run ps as follows:
You could then do in a php script:
The above code lists all php processes running in full, then checks to see if "my_script.php" is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing. answered Sep 9, 2015 at 11:25
1 Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:
Edit:Technically, this invokes the mailer right away, but only completes the command when the php script ends. Doing this captures the output of the php-script and includes in the mail body, which can be useful for debugging what caused the script to halt. answered Sep 23, 2008 at 15:25
troelskntroelskn 112k24 gold badges132 silver badges154 bronze badges 1 Simple bash script
answered Sep 22, 2008 at 20:11
MezMez 23.9k14 gold badges70 silver badges92 bronze badges 3 Not for windows, but... I've got a couple of long-running PHP scripts, that have a shell script wrapping it. You can optionally return a value from the script that will be checked in the shell-script to exit, restart immediately, or sleep for a few seconds -and then restart. Here's a simple one that just keeps running the PHP script till it's manually stopped. #!/bin/bash clear date php -f cli-SCRIPT.php echo "wait a little while ..."; sleep 10 exec $0 The "exec $0" restarts the script, without creating a sub-process that will have to unravel later (and take up resources in the meantime). This bash script wraps a mail-sender, so it's not a problem if it exits and pauses for a moment. answered Sep 23, 2008 at 12:13
Alister BulmanAlister Bulman 33.6k9 gold badges70 silver badges109 bronze badges 1 Here is what I did to combat a similar issue. This helps in the event anyone else has a parameterized php script that you want cron to execute frequently, but only want one execution to run at any time. Add this to the top of your php script, or create a common method.
answered Dec 30, 2019 at 4:29
Dom DaFonteDom DaFonte 1,56413 silver badges28 bronze badges You can write in your crontab something like this:
Your test.php file should look like this:
That way you will have only one active instace of the cron job with
answered Nov 17, 2015 at 15:51
Inspired from Justin Levene's answer and improved it as
Small but useful detail: Why Because while counting processes answered Apr 1, 2016 at 5:00
One possible solution is to have it listen on a port using the socket functions. You can check that the socket is still listening with a simple script. Even a monitoring service like pingdom could monitor its status. If it dies, the socket is no longer listening. Plenty of solutions.. Good luck. answered Sep 22, 2008 at 20:43
DreamWerxDreamWerx 2,8781 gold badge18 silver badges13 bronze badges If you have your hands on the script, you can just ask him to set a time value every X times in db, and then let a cron job check if that value is up to date. answered Sep 23, 2008 at 15:49
e-satise-satis 560k108 gold badges291 silver badges328 bronze badges troelskn wrote:
This will call Instead, use the double ampersand operator to separate the commands, i.e.
answered Sep 23, 2008 at 17:46
1 If you're having trouble checking for the PHP script directly, you can make a trivial wrapper and check for that. I'm not sufficiently familiar with Windows scripting to put how it's done here, but in Bash, it'd look like... wrapper_for_test_php.sh
Then you'd just check for the wrapper like you'd check for any other bash script: pidof -x wrapper_for_test_php.sh answered Sep 22, 2016 at 15:00
Jack SimthJack Simth 1131 silver badge7 bronze badges I have used cmder for windows and based on this script I came up with this one that I managed to deploy on linux later.
answered Nov 7, 2016 at 11:01
KamaroKamaro 8871 gold badge10 silver badges9 bronze badges How do I know if my PHP code is working?How to Test PHP Code on Localhost. Make certain XAMPP is installed. ... . If XAMPP is running, close it. ... . Put your PHP files into your htdocs folder. ... . Double click on your icon for XAMPP or open your XAMPP. ... . Just to the right of your Apache heading, you will see the Apache web server.. How do I know if PHP script is running on server?In PHP this can be retrieved using getmypid() which will return an integer number. This pid number can be saved to a file and each time the script is run a check made to see if the file exists. If it is the posix_kill() function can be used to see if a process is running with that pid number.
How can I see what processes are running PHP?Depending on your setup this could be managed via fast-cgi or mod_php or even php-fpm. If you use mod_php , then there will be no "php processes" visible for ps . You can still see if your PHP engine is in use by using lsof : $ lsof -ln [...]
How do I run a PHP script?php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.
|