Learn cron job in an easy way

Through this blog post you will learn what is cron job, expressions in cron job and how to schedule a cron job. The purpose of this post to help you understand cron job expressions so that setting up cron job schedule will be easy for you.

What is Cronjob?

A cron is a program or command or utility that executes your job on a scheduled time. It could be one time in a day or month on week or could be multiple times as per the instructions you have written through expression. Cronjobs are really very useful for repititive automated tasks.

Cronjob basically contains four components, out of which first three are must.

  • Schedule (represented by expression)
  • Command
  • Script
  • Output
 * * * * * /usr/bin/php /var/www/html/dev2devs.com/sendmail.php >> /logs/output-sendmail-cron.log

The expression is * * * * * which represnts a schedule, the current expression says every minute, the second part /usr/bin/php is the command that will run your script, third part sendmail.php is the script that will be executed, and the last part is is piping the output to a log file. The last part is not mandatory, but will be good for debugging purpose to check if any error in the script execution.

How to set cronjob on linux system

You can type below command on command prompt to see and edit running cron jobs or to add a new cron job.

$ crontab -e

Above command will open a vi editor which will show already added jobs. You can easily add you new cronjob in a newline if you are familiar with vi editor. Incase not familar then press escap button on keyboard and then press i button. This will enable insert mode to add, now go to last line of existing cron and press enter and type in your new cronjob. After typing to save and exit press escape button and then :wq to save and exit. Incase if you made a mistake and do not want to save and exit then press escape button and then :q!

If you do not want to add any cron and just want to see the list of cron, use below command:

$ crontab -l

To edit cronjob of other user:

$ crontab -u username -e

To list cronjob of other user:

$ crontab -u username -l

To edit or list other users cronjob you should have sudo privileges.

Use cases of cronjobs

There could be many usecases of cron depends on your usecase. Some common usecases are as below:

  • Regular database backup in midnight
  • Sending mass emails
  • Updating product catalogues with new prices and images
  • Log file cleanups

Expressions in cronjob

Cron expression is written in the order of minute, hour, day of month, month, year. Expression determine how often and when to run the job. Let's see through some examples to understand how expressions are written to setup a cron job or to understand schedule of an existing job.

Further you can use * for any value, - for ranges, , for list, and / for step.

Run cron every minute

* * * * * Run cron every minute

Run cron every 30 minute

*/30 * * * *

Run cron at 0 min past every 12 hours

0 */12 * * *

Run cron every midnight

0 0 * * *

Run every sunday at 00:00

0 0 * * SUN

Run at 22 minute past every second hour between 1 to 12 hours

22 0-12/2 * * *

Help us Grow

Leave a Reply

Your email address will not be published. Required fields are marked *