S
S
Sergey Pugovkin2018-02-17 22:59:10
PHP
Sergey Pugovkin, 2018-02-17 22:59:10

Why does ssh console hang when php daemon exits?

There is a php daemon. In an action loop and checking that a signal has been received. If received, exit from the loop. And completion.

#!/usr/bin/env php
<?php
declare(strict_types=1);
declare(ticks=1);
$pid = pcntl_fork();
if ($pid) {
    echo 'Bot started.' . PHP_EOL;
    exit(0);
}
posix_setsid();
pcntl_signal(SIGTERM, function () {
    global $STOP;
    $STOP = true;
});
while (!$STOP) {
    //...
    sleep(1);
}
echo 'Bot stopped.' . PHP_EOL;
exit(0);

The problem is that when I terminate the daemon with the kill pid command, the daemon ends and "Bot stopped." and a line feed and that's it, the console seems to hang, as if waiting for more answers from the demon. Until I hit enter.
How to fix? What's next for the console?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
roswell, 2018-02-17
@Driver86

The console simply does not understand what they want from it - daemon processes and should not output anything to stdout and even to stderr, this usually ends badly for them. Build a service description for systemctl and steer the process from there.

R
rPman, 2018-02-18
@rPman

are you sure the console is freezing?
you forked the process, finished the first one, bash displayed a command prompt, after that a second process ended after a second, and displayed a message - you now have an invitation on the previous line and the cursor is on a new clean one, and you are waiting for something, although in fact bash has you are already waiting for the input of the command, you can enter it.
when you hit enter, bash just re-prompts you to enter commands

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question