📜  crontab - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:07.424000             🧑  作者: Mango

Crontab - Shell-Bash

Crontab is a tool that runs on Unix/Linux systems and allows you to schedule tasks to run automatically at specific times and intervals. It is a time-based job scheduler in Unix-like operating systems that executes commands or scripts at specified dates and times.

How to Use Crontab
Viewing Crontab Entries

To view your crontab entries, use the following command:

crontab -l

This will display your current crontab entries.

Editing Crontab Entries

To edit your crontab entries, use the following command:

crontab -e

This will open your crontab file in the default text editor specified in the EDITOR environment variable.

Adding Crontab Entries

To add a new crontab entry, use the following command:

crontab -e

Enter the new crontab entry in the file. The format of a crontab entry is as follows:

*     *     *     *     *     command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of the week (0 - 6) (Sunday is 0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
Removing Crontab Entries

To remove a crontab entry, use the following command:

crontab -r

This will remove all of your crontab entries.

Examples

Here are some examples of how to use crontab:

Run a Script Every Minute
* * * * * /path/to/script.sh
Run a Script Every Hour
0 * * * * /path/to/script.sh
Run a Script Every Day at Midnight
0 0 * * * /path/to/script.sh
Run a Script Every Monday at Midnight
0 0 * * 1 /path/to/script.sh
Conclusion

Crontab is a powerful tool that allows you to schedule tasks to run automatically at specific times and intervals. With a little bit of practice, you can easily automate routine tasks such as backups, updates, and data processing.