Recently one of my follower on @techalyst interested to know how to Setup Cron Job on Digital Ocean Server where he has deployed a Laravel Web Application. He was aware on how to setup Laravel Task Scheduler using artisan command, but he was not aware how to execute Laravel Artisan Scheduler using Linux Cron Job.
There are times when your application needs to run administrative tasks periodically on the server. Whether you want to send out emails to your users, backup important data or clean up the database tables ! at the end of the day, you will need a task scheduling mechanism to take care of the tasks.
Cron is a task scheduler in Linux, Unix systems, which runs shell commands at certain intervals. This article is not meant for introducing Cron or Laravel Task Scheduler, but since it is a key concept in our tutorial, you may follow the links bellow to learn further about it.
- Click here to learn about Laravel Task Scheduler from one of my previous post.
- Click here to learn directly from Laravel Documentation.
How to add Cron Job on Digital Ocean Droplet for Laravel Task Scheduler
Login to your Droplet using
SSH terminal and
Root user credential, you must login as
root user to setup cron entry, so that we can guarantee the execution of cron job without permission issues in the future.
then write the following command in your terminal
sudo crontab -u root -e
this will open a new file in your terminal, copy past the following command in the end of the file, make sure to replace
yourlaravelproject folder name with correct one matching to your project.
* * * * * php /var/www/yourlaravelproject/artisan schedule:run 1>> /dev/null 2>&1
Then to save it remember to press
Ctrl + X and then type
Y and then press enter.
then restart the linux Cron Service with executing the following command in your terminal:
sudo service cron restart
Just to verify everything went good and cron is running, write the following command in your terminal:
sudo grep -i cron /var/log/syslog|tail -3
that is all, we have completed with setting up a Cron entry on Digital Ocean Droplet for executing Laravel Artisan Task Scheduler.
If you have any other questions, experience or insights on "How to Setup Cron Job on Digital Ocean Server" please feel free to leave your thoughts in the comments bellow, Don't forget to share the posts.