Using scheduled jobs to maintain your application

To help your application to run more efficiently, you should conduct regular website maintenance. You can automate maintenance tasks or jobs to run at scheduled intervals using the Scheduled Jobs page in the Acquia Cloud interface.

Scheduled jobs are specific to the environment that they are created in. While you cannot create a job that executes a command across multiple environments based on the same schedule, you can create a job for each environment, with each of these jobs using the same command line and schedule.

You should use the Scheduled Jobs page for scheduled jobs, rather than the default Drupal cron or any of the contributed cron modules, such as Elysia Cron or Ultimate Cron. Compared to other cron solutions, using the Scheduled Jobs page is more reliable and provides extensive and integrated logging for Acquia Cloud applications.

The default Drupal cron (poor man's cron) is enabled by default and you should disable it. To do this, go to http://[site_URL]/admin/config/system/cron, and in the Run cron every list, click Never. The default Drupal cron is triggered by application activity, so relying on Acquia Cloud instead ensures that cron jobs are run on the schedule you specify, and not just when the application is visited. This both improves reliability and avoids the performance reduction on page requests.

Creating scheduled jobs

To create a new cron job for an environment, complete the following steps:

  1. Sign in to the Acquia Cloud interface and open the Scheduled Jobs page for your application and environment.
  2. Click Add Job to add a scheduled job.

    Add a scheduled job

  3. In the Create a new scheduled job on [env] dialog, enter a descriptive name for the job in the Job name field.
  4. In the Command field, enter the command that you want to have Acquia Cloud regularly run for the environment. You can enter any commands that are allowed from an SSH connection to the environment. A command cannot be longer than 255 characters. If you need to execute a command longer than that, you should incorporate it into a shell script that you run as a scheduled job.
    Remember that % is a special character in cron commands and if your command uses it, you have to escape it with a backslash. For some common cron examples that you can use in this field, see the Using cron command examples section on this page.
    You should test commands using SSH in your Acquia Cloud environment to make sure they work before adding them as automated cron commands.

    Enter information about the scheduled job

  5. Select how often you want to run the command from the menus in the Command frequency section:
    • Every minute
    • Every hour, at a number of minutes past the hour
    • Every day, at a certain time
    • Every week, at a certain day of the week and time of day
    • Every month, at a certain day of the month and time of day
    • Every year, at a certain month, day of the month, and time of day

    All times are UTC.
    As an alternative, you can enter the cron frequency as a string. For more information, see Cron time string format.

  6. Click Add.

After you create a new job, Acquia Cloud displays the job in the list of scheduled jobs for the environment in which it was created.

Scheduled job output

When a scheduled job runs, and the output is not redirected to stdout, the cron user sends an email to the application user on the same server. This email is saved in /var/mail.

To avoid issues, scheduled jobs should always include a logging statement. For example:

drush @[site].[env] -dv -l http://[site_URL] cron &>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log

In this 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 server's logs directory, where it will be rotated along with the other logs in that directory. The log file will be rotated only if it is named drush-cron.log. For more information, see About Acquia Cloud logging.

Managing scheduled jobs

Each job that you create on the Scheduled Jobs page has links to help you manage the job, which allow you to do the following:

  • Disable - Stops the scheduled job from running until you click Enable.
  • Edit - Opens a window that allows you to change the attributes of the scheduled job. After you make your changes, click Edit.
  • Remove - Opens a window that deletes the scheduled job when you click Remove. If you remove a scheduled job, you cannot recover it, and you must manually recreate it to restore the job.

Cron and memory limits

Two approaches in using drush cron are to use the Acquia Cloud wrapper script for drush cron or to execute drush cron directly. These approaches have different memory limits. When you use the Acquia cron wrapper, the cron job uses the PHP memory limit for your environment (by default, 128 MB). When you use cron directly, it uses the command line process memory limit, which is 512 MB. If you find that your cron jobs fail to complete, try defining them to use drush cron directly to take advantage of this higher memory limit. For related information, see Debugging cron.

Cron command examples

Use the following examples as you create scheduled jobs for your application's environments.

Using drush cron

To use drush cron for enhanced cron performance, select from the following methods:

In each of these examples:

  • [site] is the name of your application on Acquia Cloud.
  • [env] is your environment (usually one of dev, test, or prod).
  • [site_URL] is your environment's URL (as listed on the Cloud > Domains page). If you are using Drupal multisite, your cron jobs are specific to each website in the multisite installation. Use the URL of the website in the installation that you wish to target.

Use the Acquia Cloud wrapper script for drush cron

Use a cron command like this:

/usr/local/bin/cron-wrapper.sh [site].[env] http://[site_URL]

Note that you do not need to append a cron_key to the website URL. The output of drush cron is logged to a file with a name like /var/log/sites/[site].[env]/logs/[server]/drush-cron.log. For example, if your site name was example, the log file for the prod environment might be named /var/log/sites/example.prod/logs/web-9876/drush-cron.log.

Execute drush cron directly

drush @[site].[env] -dv -l http://[site_URL] cron &>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log

In this 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 server's logs directory. As already noted, cron jobs should always include a logging statement, as in this example.

For instances where you want a single hook, you can also execute a specific cron hook directly. The command structure will vary based on your Drupal version and module. For example:

drush -v ev "mymodule_cron();"  &>> /var/log/sites/${AH_SITE_NAME}/logs/$(hostname -s)/drush-cron.log

The previous command will log output to the standard log files, ensuring that log rotation and maintenance continues.

Executing a shell script

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

For this example we will assume that you have added scripts/my-script.sh to your repository.

/var/www/html/[site].[env]/scripts/my-script.sh

where [site].[env] is your site name and environment.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
By submitting this form, you accept the Mollom privacy policy.

Contact supportStill need assistance? Contact Acquia Support