How to add a cron job on Linux

Last updated on February 25, 2022 by Dan Nanni

Question: I would like to schedule a task on my Linux box, so that the task runs periodically at fixed times. How can I add a cron job for this task on my Linux system?

The cron utility is the default task scheduler used in Linux. Using cron, you can schedule a task (e.g., system backup, log rotation, vulnerability scanning, disk cleanup) to run it periodically or one-time at a specific time of hour, day, week, month, etc. The cron tool is useful when you schedule a variety of regular maintenance jobs, such as periodic backup, rotating logs, checking filesystem, monitoring disk space, and so on.

Add a cron Job from the Command Line

To add a cron job, you can use a command-line tool called crontab.

Type the following command to create a new cron job to run as the current user.

$ crontab -e

If you want a cron job to run as any other user, type the following command instead.

$ sudo crontab -u <username> -e

You will be presented with a text editor window, where you can add or edit cron jobs. By default, nano editor will be used.

Each cron job is formatted as follows.

<minute> <hour> <day-of-month> <month-of-year> <day-of-week> <command>

The first five elements specify the schedule for a task, and the last element is the (full-path) command or script to execute according to the schedule.

Here are a few useful cron job examples.

Once you are done with setting up cron job(s), press Ctrl+x to save and quit the editor. At this point, newly added cron jobs should be activated.

To browse existing cron jobs of yours, use the following command:

$ crontab -l

Add a cron Job from GUI

If you are in Linux desktop environment, you can use a GUI fronend for crontab to add or edit a cron job via a more user-friendly interface.

On GNOME desktop, there is GNOME Schedule (gnome-schedule package).

On KDE desktop, there is Task Scheduler (kcron package).

Besides cron, there are other ways to schedule periodic tasks on Linux, and one of them is systemd, which is the default init system for Linux. Check out this tutorial to find out how to schedule a periodic task with systemd.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean