Running Laravel Artisan Schedule with prefix - laravel

I am running an artisan command via the scheduler on elastic beanstalk.
Elastic beanstalk cronjobs need to be passed the environment variables required for the process as follows.
* * * * * ec2-user source /opt/elasticbeanstalk/support/envvars; php /var/app/current/artisan schedule:run >> /dev/null 2>&1
Where /opt/elasticbeanstalk/support/envvars is a list of exports.
This works fine for the process running the scheduler itself, but the scheduler executes commands on separate processes and therefore the environment variables are missing.
For example
[ec2-user#ip-x-x-x-x current]$ php artisan schedule:run
Running scheduled command: '/usr/bin/php-7.0' 'artisan' rewards:notify > '/dev/null' 2>&1
Which then doesn't have access to the environment variables because the shell doesn't have a concept of a profile it seems and therefore doesn't autosource .bashrc etc.
How can I get the environment variables from the aforementioned file to be read in before my commands?

Related

Can't run a task schedule in Laravel using cron job

we prepared functionality to run automatically, while it is working in the local server using the terminal but not in the server.
what is wrong with this cron command?
/usr/local/bin/php /home/path_of_server/artisan schedule:run >> null 2>&1
dev is missing in your command.
It should be like this:
/usr/local/bin/php /home/path_of_server/artisan schedule:run >> /dev/null 2>&1

How can I run artisan Command to work In background laravel

I want to run the php artisan schedule:work command but the issue is that when i close putty it terminate the operation while i need it to remain processing on server
my server is running Ubuntu 20.4.1 LTS
Actually, the command schedule:work is for local development environment.
While you want to run your scheduler on server you should add a cron job like the following:
First, Go to your terminal, ssh into your server, cd into your project and run this command:
crontab -e
This will open the server Crontab file, paste the code below into the file, save and then exit.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Here we added one Cron job which executes every minute to start the Laravel scheduler.
Don't forget to replace /path-to-your-project with your project path.
Use "nohup php artisan schedule:work"
Go to your project path e.g. cd /var/www/mywebsite
Command this
crontab -e
choose editor nano number 1 if it shows a list of editors.
below of file add this.
* * * * * php /var/www/mywebsite/artisan schedule:run >> /dev/null 2>&1
save it CTR+S

Task scheduler in laravel homestead

I'm trying to get scheduled tasks to work in homestead.
First I created a command in called "randomUserCreatesubmission".
protected $signature = 'command:randomusercreatesubmission';
This command works just fine.
I then added it to a schedule in Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('disposable:update')->weekly();
$schedule->command('command:randomusercreatesubmission')->everyMinute();
}
Then, after SSHing into homestead, I run the command
php artisan schedule:run
This returns the following:
Running scheduled command: '/usr/bin/php7.4' 'artisan' command:randomusercreatesubmission > '/dev/null' 2>&1
and it will run the "randomUserCreatesubmission" command once, immediately. However, I want it to run every minute, and it does not do that.
Why is this happening?
You need to setup a cron job. You can run in your terminal
crontab -e
then add to the end of your crontab
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
and this command will run every minute.
Don't forget to start scheduler by adding below statement into crontab -e
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Familiarize yourself with the official Laravel Scheduler documentation.

Laravel 5.4 Cron Job for Mail Queue not working on AWS EC2 instance

I have created a cron job on my EC2 instance for sending email from queue. It shows running the corn command every minute but I see tasks remain in my jobs table. I've already searched and tried existing solutions but failed to figure out the problem. Although queue working on my local server when I run
php artisan schedule:run
My code in kernel:
$schedule->command('queue:work')
->everyMinute()
->withoutOverlapping();
Command I added in the file using crontab -e
* * * * * /usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1
This is the output I see in terminal when I run sudo service cron status
May 07 13:50:01 ip-172-31-39-27 CRON[20260]: pam_unix(cron:session): session opened for user ubuntu by (uid=0)
May 07 13:50:01 ip-172-31-39-27 CRON[20261]: (ubuntu) CMD (/usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1)
May 07 13:50:01 ip-172-31-39-27 CRON[20260]: pam_unix(cron:session): session closed for user ubuntu
Finally, I solved the problem. I got a community help that pointed out my cron command is wrong. I was running schedule command instead of queue command. So I changed it from
* * * * * /usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1
to
* * * * * /usr/bin/php /var/www/html/prod_back/artisan queue:work >> /dev/null 2>&1
One more thing, I also had to run below command to reload my env changes from sync to database for queue driver.
/usr/bin/php /var/www/html/prod_back/artisan config:cache
Please go to this link below and follow the checklist for diagnosing your specific problem in this regard.
https://stackoverflow.com/questions/58738157/laravel-queue-with-aws-elastic-beanstalk

Issue Running Artisan Command via Cronjob

I'm having a bit of a nightmare getting a crontab/cronjob to run an Artisan command.
I have another Artisan command running via cronjob no problems but this second command won't run.
Firstly, when I do 'crontab -e' and edit the file to contain:
0 0 * * * /usr/local/bin/php /home/purple/public_html/artisan feeds:send
The cronjob doesn't run at all.
If I go to cPanel and add the cronjob there, it runs but I receive the following error:
open(public/downloads/feeds/events.csv): failed to open stream: No such file or directory
The thing is the file exists and the directories have the correct permissions. If I run the command when logged in via SSH as root or the user purple (php artisan feeds:send) the command runs flawlessly and completes its tasks no problem.
If in cPanel, I edit the cronjob to use:
0 0 * * * php /home/purple/public_html/artisan feeds:send
I receive the following error:
There are no commands defined in the "feeds" namespace.
The funny thing is that my other command is registered in the crontab file and works and has no reference in cPanel at all.
Any help would be much appreciated. Just for brevity I have included the command and model that the command uses.
Feed.php Model:
http://laravel.io/bin/1e2n
DataFeedController.php Controller:
http://laravel.io/bin/6x0E
SendFeeds.php Command:
http://laravel.io/bin/BW3d
start/artisan.php:
http://laravel.io/bin/2xV3
FeedInterface.php Interface:
http://laravel.io/bin/LxnO
As you can see there is a GetRates command, which works.
Well it looks like I had to cd in to the script directory first before running the command, which now after working it out it makes sense. Easy when you know how eh!
* * * * * cd /home/purple/public_html/ && /usr/local/bin/php artisan feeds:send

Resources