DEV Community

Lucas Pereira de Souza
Lucas Pereira de Souza

Posted on

Scheduling tasks with crontab

logotech

## Configuring Cron Jobs on Linux Servers: Scheduling and Practical Examples

Cron is an essential tool on any Linux server, allowing the automation of tasks and scripts at specific times. Whether it's for performing backups, updating databases, sending reports, or executing any other routine activity, cron offers the necessary flexibility and control. In this article, we'll explore how to configure cron jobs, understand their scheduling syntax, and provide practical examples for you to start using this powerful tool.

What is Cron?

Cron (from the Greek \"time\") is a daemon (a program running in the background) that executes scheduled tasks on a Linux server. These tasks are defined in files called \"crontabs\" (cron tables), which specify the exact time each command or script should be executed.

Accessing and Editing the Crontab

Each user on a Linux server has their own crontab. To edit your user's crontab, use the following command in the terminal:

crontab -e
Enter fullscreen mode Exit fullscreen mode

This command will open the crontab in your default text editor (usually vi or nano). If this is the first time you're editing the crontab, it may prompt you to choose an editor.

Cron Scheduling Syntax

The cron scheduling syntax may seem a bit intimidating at first, but with a little practice, you'll become familiar with it quickly. Each line in the crontab represents a scheduled task and has six fields separated by spaces:

minute hour day_of_month month day_of_week command_to_be_executed
Enter fullscreen mode Exit fullscreen mode

Let's break down each field:

  • minute: Represents the minute the task should be executed (0-59).
  • hour: Represents the hour the task should be executed (0-23, where 0 is midnight).
  • day_of_month: Represents the day of the month the task should be executed (1-31).
  • month: Represents the month the task should be executed (1-12). You can also use abbreviations like JAN, FEB, MAR, etc.
  • day_of_week: Represents the day of the week the task should be executed (0-7, where 0 and 7 are Sunday). You can also use abbreviations like SUN, MON, TUE, etc.
  • command_to_be_executed: The command or script you want to execute.

Wildcard Character (*)

The asterisk (*) is a wildcard character that means \"all possible values". For example:

  • \* \* \* \* \*: Executes the task every minute.
  • 0 \* \* \* \*: Executes the task every hour, at minute 0.
  • 0 0 \* \* \*: Executes the task every day at midnight.

Practical Examples

Let's look at some examples of how to use the crontab:

  1. Run a backup script daily at 2:30 AM:

    30 2 * * * /usr/local/bin/backup.sh
    

    In this example, the backup.sh script located in /usr/local/bin/ will be executed at 2:30 AM every day.

  2. Clear temporary files every hour:

    0 * * * * find /tmp -type f -delete
    

    This command will use the find command to locate and delete all files in the /tmp directory every hour, at minute 0.

  3. Send a report by email on the first day of each month:

    0 0 1 * * /usr/local/bin/enviar_relatorio.sh
    

    This command will execute the enviar_relatorio.sh script located in /usr/local/bin/ on the first day of each month, at midnight. Make sure you have email delivery configured correctly on your server.

  4. Execute a specific command every Monday at 10:00 AM:

    0 10 * * 1 /usr/bin/comando_especifico
    

    In this example, the command /usr/bin/comando_especifico will be executed every Monday at 10:00 AM.

Important Tips

  • Absolute Paths: Always use absolute paths for commands and scripts in your crontab. This ensures that cron finds the files correctly, regardless of the working directory.
  • Command Output: By default, the output of cron commands is sent by email to the user who owns the crontab. You can redirect the output to a log file or disable email sending by adding > /dev/null 2>&1 to the end of the command.
  • Permissions: Make sure the scripts executed by cron have the correct permissions to be executed.
  • Testing: Test your scripts and commands before adding them to the crontab to avoid problems.
  • Log: Monitor the system logs (usually in /var/log/syslog or /var/log/cron) to identify errors or problems in cron jobs.

Conclusion

Configuring cron jobs is a valuable skill for any Linux system administrator. With the correct syntax and the examples provided, you'll be on your way to automating your tasks and optimizing your server management. Explore the possibilities, experiment with different configurations, and make the most of the power of cron!

Top comments (0)