I've created a custom Laravel PHP Artisan command which I was intending to use together with a cron job to carry out automated tasks on my server.
However, I'm having problems running the script and have tried a whole bunch of variations without much luck. Have looked high and low on the inter webs can't find anything to remedy my problems...
Here are a few of them:
* * * * * sudo su daemon -s /bin/sh -c "/opt/bitnami/php/bin/php /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1"
* * * * * cd /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1
* * * * * /opt/bitnami/php/bin/php /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1
The error I keep getting is: "/bin/sh: 1: php: not found"
I've also tried to execute the command as Bitnami, but no luck there either.
Thanks Jota, I ran the following and it seemed to have done the job:
* * * * * cd /opt/bitnami/apps/demo/htdocs/ && /opt/bitnami/php/bin/php artisan schedule:run >> /tmp/cron_output_8.txt 2>&1
Got this message instead now:
No scheduled commands are ready to run.
Which is good, I think that's just laravel, and I haven't set all that stuff up to run at this point.
Cheers,
Mikael
Related
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
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.
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
I'm trying to run a cron job on macOS and a site hosted locally using MAMP.
I have tried various options, none to any avail; please see below:
*/1 * * * * php http://mylocalsite.com/cron/my_function
*/1 * * * * /usr/bin/curl –silent –compressed http://mylocalsite.com/cron/my_function
*/1 * * * * /usr/bin/curl --silent --compressed http://mylocalsite.com/cron/my_function
*/1 * * * * /Applications/MAMP/bin/php/php7.1.12/bin/php http://mylocalsite.com/cron/my_function > /dev/null 2>&1
*/1 * * * * wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null
When I run the following in terminal, it does work:
wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null
I know that the URL executes the function that I want as I have tested this directly in a browser.
What am I doing wrong and what should go into the crontab in order to ping/run the specified URL?
The CodeIgniter manual suggests that running CodeIgniter through the commandline is possible.
They write the following script:
<?php
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
In turn they execute the script like this on the server:
$ cd /path/to/project;
$ php index.php tools message
Where tools denotes the controller, and message the action.
So in that case the crontab entry would become:
*/1 * * * * cd /path/to/project; php index.php tools message
And in the case sketched in the question:
*/1 * * * * cd /path/to/project; php index.php cron my_function
SOURCE: https://www.codeigniter.com/user_guide/general/cli.html
The thing I like to do is:
Make sure you have php installed using brew (brew install php#version (I use php#7.4))
Check if you have crontab, else install using brew install crontab
Enter crontab -l into the terminal, this will open a vim editor. If you can't seem to type anything press the i key. Then start entering the commands like :
*/1 * * * * cd /path/to/project; php index.php
press : and following it enter wq and enter to save and exit the vim, making it :wq
check cronjob using crontab -e in terminal
Additional details here(for crontab) and here(for vim)
I have Laravel 5 installed via composer on Ubuntu server.
My cron command looks like:
* * * * * /usr/bin/php /var/www/html/domain/public_html && php artisan schedule:run
And i get email from server with: "Could not open input file: artisan".
If i go to laravel directory, i can successfully run: php artisan command:command.
Cron is run as root.
If i remove "&&" from command, then nothing happens.
Any ideas, why it is not working?
please refer to this section in the docs
https://laravel.com/docs/5.6/scheduling#introduction
* * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run >> /dev/null 2>&1
You should try this:
* * * * * php /var/www/html/yourprojectname/artisan schedule:run >> /dev/null 2>&1
For more details please follow this link
https://laravel.com/docs/5.6/scheduling#introduction or previous laravel version should tell you ... and this is my setting in cron * * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run >> /dev/null 2>&1
I made a mistake in cron command.
This command works:
* * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run