List and edit cronjobs guide for Linux OS

Category : Linux/ Unix

Cron is Linux inbuilt utility which is a time-based job scheduler. All the Linux Distribution comes with this utility and it is very helpful when anyone wants to automate any Linux Command or script such as updating system/packages, creating a backup of data, sending emails, etc. You can also schedule the downloading of any file at a particular time period. Cronjobs are helpful in repetitive tasks like checking a total number of files in a particular directory every 30 minutes or fetching the server stats every 5 minutes to make sure all the services are running correctly. Today, we will help you in learning about crons following by list and edit cronjobs in Linux.

Cronjob runs with the help of crontab which is a cron table. It is a configuration file for all the cron job in a particular system. Every Linux user has its own configuration file which means every user can set any cronjob as per their requirements. A crontab file contains instructions for the cron utility in the following simplified manner: “run this command at this time on this date”.  We will show how these cron table configuration file looks like when any cron is added along with time and command.

Each line of a crontab file represents a job, and looks like this:

# ┌───────────── minute (0 – 59)
# │ ┌───────────── hour (0 – 23)
# │ │ ┌───────────── day of the month (1 – 31)
# │ │ │ ┌───────────── month (1 – 12)
# │ │ │ │ ┌───────────── day of the week (0 – 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute

To display the contents of the crontab file of the currently logged in user:

crontab -l

list and edit cronjobs

To edit the current user’s cron jobs, do:

crontab -e

If you are the first time using crontab edit then you will get the following message.

no crontab for user – using an empty one

Select an editor. To change later, run ‘select-editor’.
1. /bin/nano <—- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed

Choose 1-4 [1]:

Select any editor of your choice and it will show the cron list menu. In this file, you need to add your cron jobs one by one.

To edit the crontab of a different user, for example, basezap, run:

crontab -u basezap -e

Below are a few examples to set the cronjobs.

  • Run a cron job at every minute
    * * * * * <command-to-execute>
    For example, if the time is 7:00, the next job will run at 7:01, 7:02, 7:03 and so on.
  • Run cron job at every 5th minute
    */5 * * * * <command-to-execute>
    For example, if the time is 7:00, the next job will run at 7:05, 7:10, 7:15 and so on.
  • Run a cron job every hour at minute 30
    30 * * * * <command-to-execute>
    For example, if the time is 7:00, the next job will run at 7:30, 8:30, 9:30 and so on.
  • Run a job every day at 3 am
    0 3 * * * <command-to-execute>
  • Run a job every Monday
    0 0 * * MON <command-to-execute>
    It will run at exactly at 00:00 on Monday.
  • Run a job every 6 months
    0 0 1 */6 * <command-to-execute>
    This cron job will start at 00:00 on day-of-month 1 in every 6th month.

We hope you now have learnt about cronjobs and list and edit cronjobs in Linux operating system.

Want to check if your cron did run or not? Head over to Read Cronjob logs in Linux Guide.