docs/guides/linux/pages/cronjob.md
2023-06-28 21:49:05 +01:00

1.4 KiB

title description published date tags editor dateCreated
How to use cronjobs Examples on how to use cronjob to automate tasks true 2023-06-27T08:28:13.676Z markdown 2023-06-27T08:28:13.676Z

How to use cronjobs

This guide provides examples on how to use cronjob to automate tasks. This was tested on Ubuntu 22.04.

Get Started

  • Begin by opening a terminal and running the following command:
crontab -e

If it's the first time you're using cronjob, you will be asked to select an editor. You can choose nano or vim. If you're not familiar with either, choose nano as the guide is based on this and it's the easiest. This command will open the crontab file in the nano text editor.

  • Next, you need to define the cronjob. Add the following lines to the file:
* * * * * <command>
  • Finally, you can save the file by pressing Ctrl + X and then Y.

Syntax

* <- minute (0-59)
  * <- hour (0-23)
    * <- day of month (1-31)
      * <- month (1-12)
        * <- day of week (0-6) (Sunday=0)
          <command> <- the command to execute

Examples

Run a command every minute

* * * * * <command>

Run a command every 5 minutes

*/5 * * * * <command>

Run a command every hour

0 * * * * <command>

Run a command every day at midnight

0 0 * * * <command>