INTRODUCTION TO CRON JOB

Sudhanshu shrotriya
Blockchain Research Lab
4 min readDec 28, 2020

There are often ways to do things more efficiently. For example, handling repetitive tasks is using an automated process is preferred by busy webmasters. So if you use a Unix-like operating system, a CRON job could save your time by performing tasks automatically.

WHAT IS A CRON JOB?

CRON is a utility program for repeating tasks at a later time. Giving a command that schedules a task, at a specific time, repeatedly is a CRON job. If you want to scheduled a one time job, for a later time, you might want to use another command. But, for recurring jobs, CRON is the perfect solution. CRON is a daemon, meaning that it works in the background to execute non interactive jobs. A daemon is always in the idle state and waits to see if a command request it to perform a certain task — either within the computer or from any other computers on the network.

With CRON jobs, you can automate system maintenance, disk space monitoring, and schedule backups. Because of its nature, CRON jobs are great for a computer that works 24/7 — a server. It is to be noted that while CRON jobs are mostly used by system administrators, it can be incredibly useful for web developers too. For example use CRON to deactivate expired accounts, check broken links, or even send newsletters for targeted users.

HOW TO CREATE AND MANAGE CRON JOBS IN LINUX

  • CRON jobs run in the background and constantly check the /etc/crontab file, and the and /var/spool/cron/ directories. The CRON files are not supposed to be edited directly and each user has a unique crontab. How then are you supposed to create and edit CRON jobs? With Crontab commands. The crontab is the method you use to create, edit, install, uninstall, and list CRON jobs.

The command for creating and editing CRON jobs is the same and simple. And what’s even cooler is that you don’t need to restart CRON after creating new files or editing existing ones.

$ crontab -e

CRON SYNTAX

Just as it is with any language, working with CRON is a lot easier when you understand its syntax and there are 2 formats you should know:

A B C D E USERNAME /path/to/command arg1 arg2ORA B C D E USERNAME /root/backup.sh

Explanation of above CRON syntax:

  • A: Minutes range: 0–59
  • B: Hours range: 0–23
  • C: Days range: 0–31
  • D: Months range: 0–12
  • E: Days of the week range: 0–7. Starting from Monday, 0 or 7 represents Sunday
  • USERNAME: replace this with your username
  • /path/to/command — The name of the script or command you want to schedule That’s not all. CRON uses 3 operator symbols which allow you to specify multiple values in a field:
  • Asterisk (*): specifies all possible values for a field ▪ The comma (,): specifies a list of values ▪ Dash (-): specifies a range of values ▪ Separator (/): specifies a step value
  • The comma (,): specifies a list of values
  • Dash (-): specifies a range of values
  • Separator (/): specifies a step value

CRON JOB EXAMPLE

  • The first step to running CRON commands is installing your crontab with the command:
# crontab -e
  • Run /root/backup.sh at 3 am every day:
0 3 * * * /root/backup.sh
  • Run script.sh at 4:30 pm on the second of every month:
30 16 2 * * /path/to/script.sh
  • Run /scripts/phpscript.php at 10 pm during the week:
0 22 * * 1-5 /scripts/phpscript.php
  • Run perlscript.pl at 23 minutes after midnight, 2am and 4am, everyday:
23 0-23/2 * * * /path/to/perlscript.pl
  • Run Linux command at 04:05 every Sunday:
5 4 * * sun /path/to/linuxcommand

CRON OPTIONS

  • List CRON jobs.
# crontab -lOR# crontab -u username -l
  • Delete all crontab jobs.
# crontab -r
  • Delete CRON job for a specific user.
# crontab -r -u username

CONCLUSION

Setting up an automatically scheduled job will not only be practical but will also help prevent you from possibly missing the action on time. CRON job is a great way to manage such tasks either as a system administrator or other professions like a web developer. All you need to do is to use the right command and choose the right time.

Here are some of the basic commands:

  • $ crontab e — to create and edit a crontab file.
  • $ crontab -u username -e — to edit other user’s crontab file with superuser access.
  • $ crontab -l — to view the list of crontab file of the current users.
  • $ crontab -r — to remove the crontab files.
  • $ crontab -a filename — to install the filename as a crontab file (on some systems, –a is not needed).

--

--