Execute Server Commands on Laravel 7 Scheduler

Viewed 376

I have 2 commands in my Laravel 7 Schelduler

$schedule->command('inspire')->everyMinute()->emailOutputTo(env('MAIL_TO'));
$schedule->exec('whoami')->everyMinute();

✅ The first one works perfectly, I get the email

enter image description here


❌ This second one doesn't work at all

$schedule->exec('whoami')->everyMinute();

I followed: https://laravel.com/docs/7.x/scheduling

enter image description here

Any hints for me ?

3 Answers

My guess is that whoami runs fine but nothing is done with the output.

Can you try to add emailOutputTo(env('MAIL_TO')); to the second command to see if you get an email with the output ?

Please check the documentation about outputting the result from exec: https://laravel.com/docs/7.x/scheduling#task-output

as @Clément Bisaillon suggests, you have forgotten to add method for shell command output.

but your comment has been Raised a new Question.

Why it works with whoiam and date, but not working with history ?

This Works:

$schedule->exec('cat /home/abilogos/.bash_history ')->everyMinute()->appendOutputTo('/home/abilogos/Desktop/testHist.txt');

you can find history file in with echo $HISTFILE

BUT WHY?

it gets even more interesting when you just which history to find history path and it tells you there

which: no history in Your Path

like source command.

because they are not Programs stored in $PATH locations. they are bash`s command

http://manpages.ubuntu.com/manpages/bionic/man7/bash-builtins.7.html

and laravel uses php @proc_open (Symfony\Component\Process\Process) which just execute Programs not Bash commands :

https://www.php.net/manual/en/function.proc-open.php

Temporarily

I created this

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class exec extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'exec {cmd}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run any command(s)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $cmd = $this->argument('cmd');
        $this->info(shell_exec($cmd));
    }
}

and use it like this

$schedule->command('exec date')->everyMinute()->emailOutputTo(env('MAIL_TO'));
$schedule->command('exec history')->everyMinute()->emailOutputTo(env('MAIL_TO'));

It works !!

Related