How to Create a Configuration File for Crontab in Linux

Table of contents:

How to Create a Configuration File for Crontab in Linux
How to Create a Configuration File for Crontab in Linux
Anonim

Cron is a daemon that takes care of managing scheduled operations ('jobs') on a Linux system. It is very useful for scheduling jobs that must be repeated over time at regular intervals. If you need to schedule an operation to run automatically, even just once, use this tool. If previously authorized by the system administrator, the 'root' user, all users of a Linux system can schedule jobs to be delegated to 'cron'. The permissions to make changes to 'cron' are managed through two files '/etc/cron.allow' and '/etc/cron.deny'. Most Linux distributions have a system-wide 'cron' configuration, but this is not covered in this tutorial.

Steps

Set up a Crontab File on Linux Step 1
Set up a Crontab File on Linux Step 1

Step 1. Use your favorite editor to create a 'cron' file

Each 'job' must occupy a line of text and must have the following syntax: 'm h d m w [command]' (without quotes).

  • m = minutes
  • h = hours
  • d = day of the month
  • m = month (1-12)
  • w = day of the week (0-7 where 0 and 7 are equivalent to Sunday, 1 to Monday, 2 to Tuesday, etc.)
  • This is a simple syntax to remember, think about how you write a date 'Wednesday, July 29, 10:30 am', then reverse the order.
Set up a Crontab File on Linux Step 2
Set up a Crontab File on Linux Step 2

Step 2. Upload your file into 'cron' using the following command 'crontab [filename]' (without quotes)

Part 1 of 1: Example

Set up a Crontab File on Linux Step 3
Set up a Crontab File on Linux Step 3

Step 1. Create a file called 'testcron.txt' (without quotes) containing the following lines of text:

  • # do this every 10 minutes
  • * / 10 * * * * date >> ~ / testCron.log
Set up a Crontab File on Linux Step 4
Set up a Crontab File on Linux Step 4

Step 2. Upload the newly created file to 'cron' using the 'crontab testCron.txt' command (without quotes)

Set up a Crontab File on Linux Step 5
Set up a Crontab File on Linux Step 5

Step 3. Wait 30 minutes, then check the contents of the 'testCron.log' file

If everything worked correctly, within the file, you should find three 'timestamps', as shown in the image.

Set up a Crontab File on Linux Step 6
Set up a Crontab File on Linux Step 6

Step 4. Now remove your file from 'cron' to stop it from running using the following command:

'crontab -r' (without quotes).

Advice

  • You can directly edit your jobs using the 'crontab -e' command (without quotes). Note: The syntax of the command is the same as in the 'vi' editor, and it might be a little awkward for new users.
  • When using a * nix operating system, ALWAYS refer to the manuals, they are your best friend. In this case use the 'man crontab' command (without quotes).

Recommended: