> For the complete documentation index, see [llms.txt](https://docs.glesys.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.glesys.com/products/compute/guides-for-server-management/automate-tasks-in-linux-using-cron.md).

# Automate tasks in Linux using cron

***

Most operating systems support some form of scheduled activities. Cron jobs are a simple yet powerful way to schedule automatic execution of commands or scripts on Linux or Unix (BSD, macOS, etc.).

Here is an example of a cron job that appends a line with the current date and time to a file every hour:

```
* 1 * * * /bin/date >> /tmp/date_and_time.txt
```

The schedule for the various jobs is stored in a configuration file called "**crontab"**. The order of the fields in a crontab entry is:

* Minute
* Hour
* Day of month
* Month
* Day of week
* Command

Allowed values for the different fields are:

```
Minute        0 to 59
Hour          0 to 23
Day of month  1-31
Month         1-12
Day of week   0-6
Command       The command to execute
```

## Example of a cron job for Magento

Below is a slightly more advanced example of a cron job for a Magento installation that should run every day at 23:30. The purpose of the cron job is to empty the directory that holds session files, which are generated continuously. Those files consume unnecessary disk space and can eventually fill the disk. We also describe what we intend to do with the result.

Here’s what our cron job looks like:

{% code title="crontab" %}

```
30 23 * * * /usr/bin/find /var/www/magento/var/session/ -type f -name 'sess*' -mtime +14 -delete
```

{% endcode %}

### What does the cron job do?

We want to find all files matching `sess*` in the directory `/var/www/magento/var/session/` that are older than 14 days. We want the cron job to delete all those files.

### Examples of how to handle the resulting output

If you want to receive email confirmation that the cron job has run, add the following line to your crontab:

{% code title="crontab" %}

```
MAILTO="user@example.com"
*/30 * * * * /usr/bin/php /home/httpd/user/exempel.se/cron.php
```

{% endcode %}

If you want to write the cron job’s log to a file, use the following syntax:

{% code title="crontab" %}

```
*/30 * * * * /usr/bin/php /home/httpd/exempel.se/cron.php > /var/log/crontab_log.log
```

{% endcode %}

If you want the cron job to run every 30 minutes, suppress all output (both stdout and stderr), and prevent any email notifications for completed or missed runs:

{% code title="crontab" %}

```
*/30 * * * * /usr/bin/curl "http://www.exempel.se/cron.php" &> /dev/null
```

{% endcode %}

For an older version of Bash, you write it like this instead:

{% code title="crontab" %}

```
*/30 * * * * /usr/bin/curl "http://www.exempel.se/cron.php" > /dev/null 2>&1
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.glesys.com/products/compute/guides-for-server-management/automate-tasks-in-linux-using-cron.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
