0

I have a PHP script that runs automatically from the CLI every day (using Task Scheduler on Windows) but may also be run manually via the browser. The script may conclude very quickly - within a matter of seconds, or may run for many hours (at which point it sends an email).

What I'd like to be able to do is have the script check to see if an instance of itself is already running - because if two (or more) instances are kicked off, then I'll have two (or more) emails when it completes.

Is it possible for a PHP script to save some kind of process id (for itself) to a file and continue executing, and then have another script read that file and use the process id to terminate the original instance?

I already have a log file generated every time the script runs. So I envision somehow being able to save a reference to the currently executing script to the top of that log. Then if the script is executed again, it can check the log to see if an instance of itself is already running and if so kill it?

I apologize if this isn't very clear - I'm having a tough time concisely articulating what I'm trying to do

MJC
  • 57
  • 9
  • 1
    On Windows you could write to the event log ( or a custom log ) and check that log before the script gets down to business – Professor Abronsius Aug 24 '23 at 15:48
  • You could also just write the task to the database and use it as a queue so it still runs but only after the current one is finished or not at all depending on your logic. – Jaquarh Aug 24 '23 at 16:03
  • ProfessorAbronsius, I do already have a log generated by the script - so yes, using that existing log to check if it's already running is something I can already do @Jaquarh, I could use a DB - or the log I already mentioned to prevent re-running the script if it's already running. But is it possible to actually terminate the existing process from a new instance of the script? Like can I save a task instance from one script and then use that in another script to terminate the first one? That's the crux of my question – MJC Aug 24 '23 at 16:12
  • 1) Check for lock file. If exists, exit else create the lock file. 2) do the script work 3) remove the lock file when done. – Markus Zeller Aug 24 '23 at 16:57
  • @MarkusZeller, thanks for that suggestion - that's fairly clean. Are you suggesting though that it's not possible (or realistic) to terminate an already executing process? Better to merely prevent a second (or more) instance in the first place? – MJC Aug 24 '23 at 16:59
  • you should be able to terminate a running process from cmdline ( which you may be using anyway in this ) but you need to find the PID so [perhaps this](https://stackoverflow.com/questions/117226/how-to-check-if-a-php-script-is-still-running) might be useful? – Professor Abronsius Aug 24 '23 at 17:06
  • Of course you can "kill" the running process. But I don't think this is a good practice. Would be easy on a linux server. Can't tell how to do on Windows. – Markus Zeller Aug 24 '23 at 17:17
  • Does this answer your question? [How to kill a running php script in windows](https://stackoverflow.com/questions/14686358/how-to-kill-a-running-php-script-in-windows) – Markus Zeller Aug 24 '23 at 17:19
  • @ProfessorAbronsius I'll take a look. Markus Zeller: I'm not sure if php processes that are spawned from apache show up there. I'll have to investigate – MJC Aug 25 '23 at 13:10

0 Answers0