Do you spend a lot of time manually performing repetitive tasks on your Linux system? If so, you’re not alone. Many Linux users waste hours every week on tasks that could be easily automated.
This can be a huge drain on your productivity and efficiency. Imagine how much time you could save if you didn’t have to manually back up your files, clear logs, generate reports, or deploy websites.
The good news is that there’s a solution. Cron and crontab are two powerful tools that can be used to automate a wide variety of tasks on your Linux system.
By learning how to use cron and crontab, you can automate many of the repetitive tasks that you currently perform manually. This will free up your time so that you can focus on more important things.
This blog post will teach you everything you need to know about how to use cron and crontab to automate your Linux tasks. We will cover the following topics:
Here we discuss about:
What is cron and crontab?
Cron is a job scheduler that allows you to schedule tasks to run at specific times or intervals. Crontab is a file that contains a list of cron jobs.
If Cron is not installed already You can install it using the following command:
sudo apt install cron
How to create a crontab configuration file
To create a crontab configuration file, run the following command:
crontab -e
If you are running it for the first time it will ask you to choose a text editor.
I will use nano but you can use any other editor from the given choices. If you want to change the default editor for crontab use the command below:
select-editor
You can use # to comment any line in crontab configuration file.
To close nano editor first save file using Ctrl+s and then use Ctrl+x.
Some Useful Crontab Commands and fields
To Edit crontab file of specific user:
crontab -e -u username
You need to use sudo to edit root crontab. For example: “sudo crontab -e -u root”
List Cron jobs:
crontab -l
use “crontab -l -u username” to list cron jobs of a specific user if you have privileges.
Delete User’s Crontab :
crontab -r
it will delete crontab of current user.You can use -u to specify user.
How to schedule cron jobs
To schedule a cron job, add a line to your crontab file in the following format:
minute hour day_of_month month day_of_week command
The first five fields specify the schedule for the cron job. The minute, hour, day_of_month, month, and day_of_week fields can be specified using wildcards to match any value. For example, the asterisk (*) wildcard matches any value.
The last field in the crontab line specifies the command to be executed. This can be any command that can be executed in a Linux terminal.
Below are the Range used in crontab:
- Minute: Range from 0 to 59
- Hour: Range from 0 to 23
- Day of the month: Range from 1 to 31
- Month: Range from 1 to 12
- Day of the week: Range from 0 to 6, where 0 and 7 = Sunday
There is also some other special syntax to run cron jobs.
- @reboot: On reboot
- @yearly: Run Every Year
- @monthly: Run Every Month
- @weekly: Run Every Week
- @hourly: Run Every Hour
Example of Reboot is shown Below.
The last field in the crontab line specifies the command to be executed. This can be any command that can be executed in a Linux terminal.
Schedule task by using crontab guru.
Examples of useful cron jobs
Here are some examples of useful cron jobs:
- Back up your files
- Run bash script after reboot
- Generate reports
- Deploy websites
- Run continuous integration pipelines
Back up my files every day at midnight
0 0 * * * /path/to/backup_script.sh
Run bash Script after reboot
@reboot /path/to/my_script.sh
Generate a monthly sales report on the first day of every month
0 0 1 * * /path/to/generate_sales_report.sh
Deploy my website to production every time the code is pushed to the master branch of the Git repository
0 * * * * /path/to/deploy_website.sh
Once you have added your cron jobs to your crontab file, save the file and exit the text editor. The cron daemon will then start monitoring your crontab file and execute the jobs as scheduled.
Common Errors Users Face When using Crontab:
bad hour : When the hour is out of range.
bad minute : When the minute is out of range.
bad day-of-week : When the day-of-week is out of range.
bad month : When the day-of-week is out of range.
bad day-of-month: When the day-of-month is out of range.
bad file mode: File Permission Issue
Tips for using cron and crontab effectively
- Use descriptive cron job names. This will make it easier to manage your cron jobs.
- Test your cron jobs before deploying them to production.
- Monitor your cron jobs to make sure that they are running as expected.
Conclusion
Cron and crontab are powerful tools that can be used to automate a wide variety of tasks on your Linux system. By automating your tasks, you can save time and effort, and you can also perform tasks that would be difficult or impossible to do manually.
In this blog post, we have covered the basics of using cron and crontab. We have also provided examples of useful cron jobs and tips for using cron and crontab effectively.
Fell Free to Contact us If you have any Questions or Want to add something in this post.