docs/guides/linux/pages/cronjob.md

69 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

2023-06-27 08:28:15 +00:00
---
title: How to use cronjobs
description: Examples on how to use cronjob to automate tasks
published: true
date: 2023-06-27T08:28:13.676Z
2023-06-27 08:32:19 +00:00
tags:
2023-06-27 08:28:15 +00:00
editor: markdown
dateCreated: 2023-06-27T08:28:13.676Z
---
2023-06-27 08:32:19 +00:00
# How to use cronjobs
2023-06-27 08:33:54 +00:00
This guide provides examples on how to use cronjob to automate tasks. _This was tested on Ubuntu 22.04._
2023-06-27 08:32:19 +00:00
## Get Started
- Begin by opening a terminal and running the following command:
```bash
crontab -e
```
2023-06-28 20:49:05 +00:00
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.
2023-06-27 08:32:19 +00:00
- Next, you need to define the cronjob. Add the following lines to the file:
```bash
* * * * * <command>
```
- Finally, you can save the file by pressing `Ctrl + X` and then `Y`.
## Syntax
```bash
* <- minute (0-59)
* <- hour (0-23)
* <- day of month (1-31)
* <- month (1-12)
* <- day of week (0-6) (Sunday=0)
2023-06-27 08:36:10 +00:00
<command> <- the command to execute
2023-06-27 08:32:19 +00:00
```
## Examples
### Run a command every minute
```bash
* * * * * <command>
```
### Run a command every 5 minutes
```bash
*/5 * * * * <command>
```
### Run a command every hour
```bash
0 * * * * <command>
```
### Run a command every day at midnight
```bash
0 0 * * * <command>
```