On Acquia Cloud Platform environments, advanced custom tasks can be scheduled using the scheduled jobs feature or triggered through Drush cron.
When creating custom scheduled jobs, the following limitations apply:
%
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
.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.
You can define scheduled jobs that explicitly trigger specific actions using Drush.
When attempting this, keep in mind the following:
--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.cd $HOME
to switch to your home folder before running it to ensure
it will run within the scheduled job’s available environment.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.
When a scheduled job runs, and the output isn’t redirected to stdout
, the
cron user sends an email to the application user on the same infrastructure. For
environments running on Cloud Classic infrastructure, Cloud Platform saves the
email in /var/mail
. This output is not saved on Acquia Cloud Next
infrastructure.
Important
On Cloud Classic infrastructure, if the messages are never picked up or cleared, the messages fill the disk, which can cause serious problems, including possibly bringing down your application.
To avoid issues, scheduled jobs must always include a logging statement. For
example, you could use the following statement, replacing [site-uri]
with
the base URL to the website where you want to run cron:
/usr/local/bin/drush10 --uri=http://[site-uri] --root=/var/www/html/${AH_SITE_NAME}/docroot -dv 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, where the output will be rotated along with
the other logs in the logs directory. The log file will be rotated only if the
file is named drush-cron.log
. For more information, see About Cloud Platform logging.
To improve usability, you can also add a timestamp to the log messages, as
displayed in the following example, replacing [SITEURL]
with the base URL
to the website where you want to run cron. You must run the following example
as a standalone script, because it exceeds the 255 character limit for scheduled jobs. The following example serves as a
general guide for creating similar scripts:
/usr/local/bin/drush10 --uri=http://[SITEURL] --root=/var/www/html/${AH_SITE_NAME}/docroot -dv 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
For environments running on Acquia Cloud Next infrastructure, logs will be
stored in /shared/logs
instead of the aforementioned /var/log/sites/
directory.