---
title: "Advanced cron and scheduled job tasks on Cloud Platform"
date: "2024-02-14T06:18:38+00:00"
summary: "Optimize cron and scheduled jobs on Cloud Platform with custom tasks, Drush commands, and shell scripts for advanced automation."
image:
type: "page"
url: "/acquia-cloud-platform/advanced-cron-and-scheduled-job-tasks-cloud-platform"
id: "25ab9979-3da6-4ad6-bb03-364280ed839e"
---

Table of contents will be added

On Cloud Platform environments, advanced custom tasks can be scheduled using the scheduled jobs feature or triggered through Drush cron.

Limitations
-----------

When creating custom scheduled jobs, the following limitations apply:

*   Commands in scheduled jobs can’t be longer than 255 characters. If you must run a command longer than 255 characters, you must incorporate it by [running a shell script](#cloud-execute-shell-script) you run as a scheduled job.
*   The `%` character is a special character in cron commands. If your command uses this special character, be sure to precede it with a backslash (`\`). For example: `your_log_file_$(date +\%F).log`.
*   Commands entered should always provide absolute paths to ensure correct execution as shown in the following examples.
*   Cron jobs configured to use Acquia’s cron-wrapper.sh script through the Cloud Platform user interface or Cloud API v2 will use the [PHP memory limit configured](/acquia-cloud-platform/manage-apps/php#php-mem) for your environment (by default, 128 MB).
*   All other scheduled jobs, including cron jobs triggered manually through Drush, have a maximum PHP memory limit of 512 MB.
*   The maximum duration of code deploy tasks is 1 hour. Exceeding this time will stop the task, and the original code will remain.
    

If you find your scheduled jobs failing to complete, try defining them to use Drush cron instead of the Acquia cron-wrapper.sh script to take advantage of the greater memory limit available to CLI commands. For related information, see [Debugging cron](/acquia-cloud-platform/help/93006-debugging-cron "Debugging cron").

Custom Drush commands
---------------------

You can define scheduled jobs that explicitly trigger specific actions using Drush.

When attempting this, keep in mind the following:

*   Include a `--root=/var/www/html/${AH_SITE_NAME}/docroot` argument, or use a Drush site alias, or prefix your command with a `cd` that switches to the correct folder.
*   Use the Drush version appropriate for your Drupal and module version(s).
*   Test your Drush commands while SSH’d into your environment before adding them to your scheduled jobs to ensure everything works. To do this:
    *   Type `cd $HOME` to switch to your home folder before running it to ensure it will run within the scheduled job’s available environment.
    *   If using a site alias, ensure you pick from the available list shown by Drush. See [Using Drush aliases](/acquia-cloud-platform/manage-apps/command-line/drush/aliases).

Running a shell script
----------------------

You can create scheduled jobs to run shell scripts you have written.

Note

Acquia recommends using a shell script for running complex or long cron commands. This method ensures complex commands can be stored in version control when necessary, enables more controlled error logging, and prevents potential problems submitting the commands to the Cloud Platform interface.

The following example assumes you have added `scripts/my-script.sh` to your repository:

    /var/www/html/${AH_SITE_NAME}/scripts/my-script.sh

Note

Acquia highly recommends adding your shell scripts to your source code repository. Acquia also recommends adding a `library` or `scripts` folder to your repository in the same directory as `acquia-utils` and [docroot](/definitions/docroot).

Scheduled job output
--------------------

When a scheduled job runs and the output does not redirect to `stdout`, the cron user sends an email to the application user on the same infrastructure.

### Cloud Next

Cloud Platform stores the logs in `/shared/logs`. If the messages are never picked up or cleared, the messages fill the disk. This might cause issues, including bringing down your application.

To avoid issues, you must include a logging statement in scheduled jobs. For example, you can use the following statement by replacing `[site-uri]` with the base URL to the website where you want to run cron:

    drush --uri=http://[site-uri] -d -v cron &>> /shared/logs/drush-cron.log

In the preceding example, `&>> /shared/logs/drush-cron.log` logs the cron output to a `drush-cron.log` file in the infrastructure’s logs directory.

To improve usability, you can add a timestamp to the log messages. For example, you can replace `[site-uri]` with the base URL to the website where you want to run cron.

    drush --uri=http://[site-uri] -d -v cron 2>&1 | awk '{print "["strftime("\%Y-\%m-\%d \%H:\%M:\%S \%Z")"] "$0}' &>> /shared/logs/drush-cron.log

The size of your log files must not exceed 1 GB. Therefore, you must periodically prune outdated logs. Unlike Cloud Classic, log files are not auto-rotated in Cloud Next.

### Cloud Classic

Cloud Platform saves the email in `/var/mail`. If the messages are never picked up or cleared, the messages fill the disk. This might cause issues, including bringing down your application.

To avoid issues, you must include a logging statement in scheduled jobs. For example, you can use the following statement by replacing `[site-uri]` with the base URL to the website where you want to run cron:

    drush --uri=http://[site-uri] -d -v cron &>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log

In the preceding example, `&>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log` logs the cron output to a `drush-cron.log` file in the infrastructure’s logs directory. The output log file is rotated along with the other logs in the logs directory. This log file is rotated only if the file is named `drush-cron.log`. For more information, see [About Cloud Platform logging](https://docs.acquia.com/acquia-cloud-platform/monitor-apps/logs).

To improve usability, you can add a timestamp to the log messages. For example, you can replace `[site-uri]` with the base URL to the website where you want to run cron.

    drush --uri=http://[site-uri] -d -v cron 2>&1 | awk '{print "["strftime("\%Y-\%m-\%d \%H:\%M:\%S \%Z")"] "$0}' &>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log

You must run this command as a standalone script as it exceeds the [255 character limit](https://docs.acquia.com/acquia-cloud-platform/manage-apps/cron#cloud-cronjob-limits) for scheduled jobs.