如何确保从队列作业启动的进程在超时后终止?

I have a problem with terminating processes started from a queue job.

I use the yii2-queue extension to run some long running system commands that have a total execution time limit controlled by the getTtr method of the RetryableInterface. The command may take anywhere from minutes to hours to fully complete, but I need to kill it after it hits the 60-minute mark.

    <?php

    use Symfony\Component\Process\Process;
    use yii\base\BaseObject;
    use yii\queue\RetryableJobInterface;

    class TailJob extends BaseObject implements RetryableJobInterface
    {
        public function getTtr()
        {
            return 10;
        }

        public function execute($queue)
        {
            $process = new Process('tail -f /var/log/dpkg.log');
            $process->setTimeout(60);
            $process->run();
        }

        public function canRetry($attempt, $error)
        {
            return false;
        }
    }

Now, the problem that I face is that even when queue/listen kills the job, the tail command (it's just an example; in production I need to run a different command) keeps running in the background. Is there any way I can force the system to kill the tail command when the job is killed?

Your script needs to keep checking if the timeout was reached; e.g.

while($process->isRunning()) {
    $process->checkTimeout();
    usleep(200000);
}

Read more about "Process Timeout" here: https://symfony.com/doc/current/components/process.html

Run the command with a timeout

$process = new Process('timeout 3600 tail -f /var/log/dpkg.log');

Will limit the process to a maximum of 60 minutes.

If your script kills it first that's fine and if it doesn't then the process will die at the timeout time.

https://linux.die.net/man/1/timeout