PHP CLI in Windows: Handling Ctrl-C commands?

前端 未结 3 1445
北荒
北荒 2021-01-17 14:16

How can I handle CTRL+C in PHP on the command line? Pcntl_* functions do not work in Windows.

3条回答
  •  情话喂你
    2021-01-17 14:43

    If you want to run a task in PHP via command line that takes a very long time, I would try to organize it in badges and keep track of what is already done.

    Now you can completely process each badge (ex: process and then store it in an xml file) and not only after the whole list is processed. So a crash/stop in between will only cancel one badge and not all of them.

    If you store your current position after each badge somewhere, you can easily resume when your script crashes or is stopped.

    Now if you check the OS process-list to see if your script is running, you can write a cron job that starts your script every X minutes if it had crashed and was not already running.

    So, TL;DR

    • Process job in small badges
    • Store position of last successfully processed badge
    • Check for already running process at start
    • Continually start script until all are happy!

    That aside, I like PHP for small command line jobs but if you have such a large task, something else might be better suited. Check for something that can run stable for a long time and has a means of showing it's progress. Maybe a small C# app with a minimalistic gui.

提交回复
热议问题