Sunday, January 22, 2023

Set cron for laravel in ngnix server

SSH into your server and navigate to the root directory of your Laravel application.

Run the command crontab -e to open the cron tab editor.

If you want to run cron in every minute then : 

Add the following line to the editor: * * * * * php /path/to/your/laravel/installation/artisan schedule:run >> /dev/null 2>&1

Save and exit the editor.

This will run the Laravel task scheduler every minute.


Note: You should replace the /path/to/your/laravel/installation/ with the actual path of your laravel installation in the server.


Alternatively, you can also add the cron job using the cron command. You can do this by running the following command:


* * * * * cd /path/to/your/laravel/installation && php artisan schedule:run >> /dev/null 2>&1

This will change the current working directory to your laravel installation and runs the schedule:run command every minute.

If you want to run cron in every day: 

0 0 * * * cd /path/to/your/laravel/installation && php artisan schedule:run >> /dev/null 2>&1
This will change the current working directory to your laravel installation and runs the schedule:run command every day at 00:00 (midnight)

You can adjust the time in the cron schedule by modifying the first two fields. The first field is for minutes (0-59), the second field is for hours (0-23), the third field is for day of the month (1-31), the fourth field is for months (1-12) and the fifth field is for day of the week (0-7) (both 0 and 7 mean Sunday).

Make sure that your Nginx server have the correct permissions to execute the cron job, and also make sure that your laravel application have the correct permissions to write to log files, cache and other necessary files.

You can also verify that the cron job is running correctly by checking the logs of the cron jobs, usually located in the /var/log/cron or /var/log/syslog directory. 

It is not necessary to set up a cron job in the cron tab when using Supervisor to manage your Laravel task scheduler.

1 comment: