Bram Vanroy
Bram Vanroy

Reputation: 28505

Does a PHP process exit when you ctrl + c it from CLI?

I'm witnessing strange disk activity on one of our servers. The only thing I can think of is that during the day a lot of PHP processes were started from CLI in screen mode, like so:

php -f myprocess.php

As soon as I noticed the disk issue, I terminated these processes by attaching the screen, ctrl + c the process, and exiting the screen. I would assume that this exits the PHP process. However, after an hour the disk activity is still very high and obstructively so. (I am no sysadmin so I cannot back-trace the activity.) I know that when you send an ajax request to a server and then abort that request (jqxhr.abort()), the server will still execute the request. I thought that maybe something similar could be going on when doing a "request" from CLI. The requests are done from home over SSH.

So, did I act correctly and should the processes have stoppped, or is it possible they will run until completion?

Upvotes: 1

Views: 248

Answers (1)

scrowler
scrowler

Reputation: 24405

Let's construct a little test:

# File: infinite.php
<?php
while (true) { sleep(1); }

Run it, then monitor the process:

$ php infinite.php                        # screen one
$ watch -n 1 "ps aux | grep infinite.php" # watch the process (screen two)

Ctrl + C the process in screen one while you watch it from screen two.

Result:

The process dies. Ctrl + C kills an active screen's PHP process.


It's possible that you may find the process continues to run if your client terminal simply closes due to a session timeout.

Upvotes: 1

Related Questions