Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 任何终端上的清理控制台命令_Php_Laravel_Laravel Artisan - Fatal编程技术网

Php 任何终端上的清理控制台命令

Php 任何终端上的清理控制台命令,php,laravel,laravel-artisan,Php,Laravel,Laravel Artisan,对于Laravel 5.3命令,我有以下简单的锁定代码: private $hash = null; public final function handle() { try { $this->hash = md5(serialize([ static::class, $this->arguments(), $this->options() ])); $this->info("Generated signature ".$this

对于Laravel 5.3命令,我有以下简单的锁定代码:

private $hash = null;
public final function handle() {
    try { 
        $this->hash = md5(serialize([ static::class, $this->arguments(), $this->options() ]));
        $this->info("Generated signature ".$this->hash,"v");

        if (Redis::exists($this->hash)) {
            $this->hash = null;
            throw new \Exception("Method ".$this->signature." is already running");
        }            
        Redis::set($this->hash, true);                                    
        $this->info("Running method","vv");
        $this->runMutuallyExclusiveCommand(); //Actual command is not important here
        $this->cleanup();
    } catch (\Exception $e) {
        $this->error($e->getMessage());
    } 
}

public function cleanup() {
    if (is_string($this->hash)) {
        Redis::del($this->hash);
    }
}
如果允许该命令正常执行,包括在出现PHP异常时进行处理,则该命令可以正常工作。但是,当通过其他方式(如CTRL-C)中断命令或关闭终端窗口时,会出现问题。在这种情况下,清理代码不会运行,并且该命令被视为仍在执行,因此我需要手动从缓存中删除该项以重新启动它。我曾尝试在_destruct函数中运行清理代码,但似乎也没有调用


我的问题是,有没有一种方法可以设置命令终止时要运行的代码,而不管它是如何终止的?

简短的回答是否定的。当您通过Ctrl-C或仅仅关闭终端终止正在运行的进程时,您就终止了它。您需要在shell中有一个链接到清理代码的中断,但这超出了范围


不过,还有其他选择。Cron作业可以间歇运行,以执行清理任务和其他有用的事情。您还可以创建一个在当前代码之前运行的启动例程。当您执行启动例程时,它可以为您进行清理,然后调用您当前的例程。我认为最好的选择是使用cron作业,该作业只需按给定的时间间隔运行,然后在缓存中查找不再合适的条目,然后清除它们。这里有一个不错的站点,可以让您开始使用cron作业

我认为cron不合适,因为我不知道缓存条目是否存在,是因为任务被终止还是因为任务仍在运行。但我愿意承认,答案确实是否定的。