macintosh264
macintosh264

Reputation: 981

Handling PHP ^C CLI Script

I have a php script that runs in the background 24/7. I have to occasionally terminate it, and the point of the script is to cache transaction data to memcahced from bitcoin RPC (if you don't know what that is, it is irrelevant). I want the script to execute a function when the program receives the signal sent on ^C (control C).

Upvotes: 5

Views: 2308

Answers (2)

oranj
oranj

Reputation: 41

In case anybody else is looking, I've found an answer that doesn't require pcntl_signal.

You can use system("stty intr ^-"); to stop ^C from exiting the script automatically. You can then capture it as ord(fread(STDIN, 1)) == 3 within PHP, and handle exiting manually.

I'm working on a library that does this.

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

You probably want pcntl_signal. The signal you need to catch is SIGINT.

Upvotes: 22

Related Questions