2

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 ?

8
  • What is an output/error? Put it into try catch block and log error message. Commented Nov 29, 2020 at 1:08
  • Nothing there at all... just emptyness. Commented Nov 29, 2020 at 1:09
  • Is the user you're trying to execute that schedule with having the permissions for that directory? Commented Nov 29, 2020 at 1:13
  • @GreenPepper : How do I check that ? As you can see that when I create work around it works so I assume the same user as I would have run whoami Commented Nov 29, 2020 at 13:36
  • Is there a way you could access your server via SSH and execute the whoami command with the exact same user that your Laravel uses? Commented Nov 29, 2020 at 23:14

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Tried again, it seems working now, I forgot the emailOutputTo() on my second line, you're correct !
I've tries this $schedule->exec('history')->everyMinute()->emailOutputTo(env('MAIL_TO')); it never works : i.imgur.com/PMbvkkw.png
Can you try once you on your setup? I'm not sure why date & whoami commands works
The error means that it cannot find the command 'history' on your machine. Is history an artisan command your created? If yes, you have to use $schedule->command instead of $schedule->exec
1
+200

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

2 Comments

Thanks for answering, and finding the root cause - and you're correct.
@cyber8200 i think its get more useful to alter questions title, body and tag to reflect the history problem.
0

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 !!

2 Comments

If anyone have a better solution, please kindly post one, I would like to learn from you 👍🏼
Side note: you can also check Symfony's Process component to be used instead shell_exec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.