bknd logo

Cronjobs

Setup Cronjobs

Cronjobs are scheduled tasks that run automatically at specified intervals. They are useful for automating repetitive tasks, such as backups, system maintenance, and more.

Creating a Cronjob

To create a cronjob, you can use the crontab command. Here's how to do it:

  1. Open the terminal.
  2. Type crontab -e to edit the crontab file.
  3. Add a new line with the following format:
* * * * * /path/to/command

The five asterisks represent the time and date fields, which specify when the command should run. The fields are as follows:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-7) (Sunday is both 0 and 7)
  1. Save and exit the editor.
  2. The cronjob will now run at the specified intervals.

Example Cronjob

To run a script every day at midnight, you can add the following line to your crontab:

0 0 * * * /path/to/your/script.sh

This will execute script.sh at 00:00 every day.

Listing Cronjobs

To list all the cronjobs for the current user, you can use the following command:

crontab -l

This will display all the scheduled cronjobs in the terminal.

Removing a Cronjob

To remove a cronjob, you can edit the crontab file again using crontab -e and delete the line corresponding to the cronjob you want to remove. Save and exit the editor to apply the changes.

Common Cronjob Syntax

FieldAllowed ValuesSpecial Characters
Minute0-59, - * /
Hour0-23, - * /
Day of Month1-31, - * /
Month1-12, - * /
Day of Week0-7, - * /
0 and 7 both represent Sunday
* represents every value in the field
, separates multiple values
- specifies a range of values
/ specifies increments

Conclusion

Cronjobs are a powerful tool for automating tasks on your system. By using the crontab command, you can easily schedule and manage these tasks to run at specific times and intervals. Whether it's for backups, maintenance, or any other repetitive task, cronjobs can save you time and effort.

Additional Resources