It is impossible if you are using Magento 2 and you are not familiar with Cron job because it is only hands-on functionality that allows you to automate your task and schedules. Basically, Cron job is a feature offered by Linux that contains a bunch of commands or a script that runs frequently. Due to its automation facility, it helps a lot to save time and efforts of store owner instead of manually doing a task which makes it the perfect choice for repeated tasks.
Generally, this Cron job can help to scheduled various tasks like reindexing, generating emails, generating newsletters, sitemaps and more. In Shared hosting, you can easily add Cron Jobs using Cron Job Manager from Cpanel but in Cloud Servers, you don’t have CPanel access. So, you have to use CLI (Command Line Interface) to write Cron Jobs using SSH.
Here are few steps to set up your own Cron Jobs on AWS EC2 Server within minutes.
Firstly open your SSH with your credentials and type below command to open an editor.
1 |
crontab -e |
Now, you will get some of the existed editors options to open, select the second one.
- /bin/ed
- /bin/nano
- /usr/bin/vim.basic
- /usr/bin/vim.tiny
After that type following command to your editor.
1 2 3 4 5 |
*/5 * * * * /usr/bin/php /home/magecomp/www/bin/magento cron:run > /home/magecomp/www/var/log/magento.cron.log& */5 * * * * /usr/bin/php -c /etc/php5/apache2/php.ini /home/magecomp/www/update/cron.php > /home/magecomp/www/var/log/update.cron.log& */5 * * * * /usr/bin/php /home/magecomp/www/bin/magento setup:cron:run > /home/magecomp/www/var/log/setup.cron.log& |
Note: You need to replace your php path with “/usr/bin/php” to above command.
To find your php installation path run command “which php”.
You need to put your Magento 2 root path instead of “/home/magecomp/www”
You can find it by using “pwd”.
Lastly, Saved the editor with “ctrl+x” and then press Y.
You have successfully created Cron.
To get a list of created Crons, type below command in your SSH Console.
1 |
crontab -l |
Besides that here are some of the examples that can help you to set your own Cron job according to your business needs.
Cron Expression Examples:
* * * * * –> Run every minute. ex: for 15 mins => */15 * * * *
0 * * * * –> Run on the hour, every hour ex: every 3hrs => * */3 * * *
0 21 * * * –> Run at 9 p.m. every day
0 0,12 * * 6 –> Run at midnight and midday every Saturday
45 3 */2 * * –> Run every second day at 3.45 a.m.
What starts stand for?
1st star: Minute (ranges from 0-59)
2nd star: Hour (ranges from 0-23)
3rd star: Day (ranges from 1-31)
4th star: Month (ranges from 1-12)
5th star: Day-of-week (0-6 –> 0- Sun. 1-Mon, 2-Tue…etc)
Tadaa! Now you did it.
Lastly, hit that stars if this blog worked for you & comment down below if you faced any issue.
Happy Croning!