简体   繁体   中英

Laravel arbitary set exit status code to custom command

I am implementing the following custom command for running a background processes:

namespace App\Console\Commands;

use App\Model\MyModel;
use Exception;
use Illuminate\Console\Command;

class MyCommand extends Command
{
    /**
     * @var string
     */
    protected $description = "Doing StuffyStuff";

    /**
     * @var string
     */
    protected $signature = "mycommand:dostuff";

    public function __construct()
    {
        parent::__construct();
    }

    public function handle(MyModel $model): void
    {
        //@todo Implement Upload
        try {
            $model->findOrFail(12);
            //Set Exit Status Code 0
        } catch (Exception $e) {
            //Set status code 1
        }
    }


}

So as you can see I want to specify the statuc code depending if an exception has been thrown or not. If success I want to use exit status code 0 and if fail I want to use exit status code 1 as Unix Spec specifies.

So do you have any Idea how to do that?

You can return the code wherever you want

public function handle(MyModel $model): int
    {
        //@todo Implement Upload
        try {
            $model->findOrFail(12);
            return 0;
        } catch (Exception $e) {
            $this->error($e->getMessage());
            return 1;
        }
    }

You can see an example & explain here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM