How to set up crontab and check if it works

What is crontab?

crontab is a command that allows you to schedule periodic tasks on Linux.
For example, you can use crontab to automate tasks such as backing up every night, deleting logs every Monday, or checking system status every hour.

crontab can be divided into two types:
  • System-wide crontab: Tasks defined in the /etc/crontab file that are shared by all users.
  • Individual user’s crontab: Tasks that each user creates and manages with the crontab command that only the user can access.
In this post, we will learn about the individual user’s crontab and how to set it up and check if it works.

How to set up crontab

The crontab command has the following options:
  • -e: Edit the crontab file. By default, the vi editor opens, but you can use a different editor by setting the EDITOR environment variable.
  • -l: Print the contents of the current crontab file.
  • -r: Delete the current crontab file.
To edit the crontab file, enter the following command:

1
$ crontab -e
cs

Then you can define tasks in the following format:

1
minute hour day month weekday command
cs

Each item has the following meaning:
  • minute: A number between 0 and 59 that specifies the minute when the command will run.
  • hour: A number between 0 and 23 that specifies the hour when the command will run.
  • day: A number between 1 and 31 that specifies the date when the command will run.
  • month: A number between 1 and 12 that specifies the month when the command will run.
  • weekday: A number between 0 and 6 that specifies the day of the week when the command will run. 0 is Sunday, 1 is Monday … 6 is Saturday.
  • command: The path and options of the shell script or program to run.
For example, if you want to run /home/user/backup.sh script every day at 10 a.m., you can write as follows:

1
0 10 * * * /home/user/backup.sh
cs

Or, if you want to run /home/user/clean.sh script on the first Monday of every month, you can write as follows:

1
0 0 * * 1 [ `date +\%d` -le 7 ] && /home/user/clean.sh
cs

Here [ date +\%d -le 7 ] is a conditional statement that checks if the date is less than or equal to 7.
In crontab, % symbol means newline, so you need to escape it with %.

When you write and save the crontab file, you will see a message saying crontab: installing new crontab, and the set tasks will run automatically.

How to check if crontab works

There are several ways to check if the tasks set by crontab have been successfully executed.
  • Check /var/log/cron file. This file contains logs of all tasks executed by crontab. You can check it with the following command:
1
$ sudo tail -/var/log/cron
cs

Add an option to log the command.
For example, if you want to record the result of running /home/user/backup.sh script in /home/user/backup.log file, you can write as follows:

1
0 10 * * * /home/user/backup.sh >> /home/user/backup.log 2>&1
cs

Here >> is a redirection operator that appends output to a file, and 2>&1 means sending standard error to standard output.
  • Receive notifications by email. you can set MAILTO variable to receive notifications at your desired email address. For example, if you want to receive notifications at user@example.com, you can write as follows:
1
2
MAILTO=user@example.com
0 10 * * * /home/user/backup.sh
cs

Conclusion

In this post, we learned what crontab is and how to use it to schedule periodic tasks and check if they work. crontab is a powerful tool for automating tasks on Linux systems.
Using crontab well can make system administration or development more convenient.
I personally use crontab to automate tasks such as backup, log cleaning, system monitoring, etc.
I think crontab is a simple but useful feature. I hope you try it too.
Thank you.

댓글

이 블로그의 인기 게시물

crontab 설정방법과 로그 확인하는 법

Microsoft Defender 방화벽 설정 또는 해제하는 방법

한국 군비지출 세계 9위