Login Try it Free

Linux PHP Cron Job

Last Updated: Mar 12, 2013 07:46AM MDT

"Cron is a time-based job scheduler that enables you to schedule jobs (commands) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration." (src: Wikipedia Cron) After reading this guide, you should be familiar with:

  •  How to schedule Cron Jobs

Cron Jobs can be scheduled from either the Dashboard or the Boxfile. It's important to note that only certain executables are allowed when using Cron Jobs. See the full list of available executables on Github.

Dashboard

From the Cron tab in your Pagoda Dashboard and click "Add A Job".

Command

Enter your command. Here a few examples to get you started. Files can either be referenced using relative(e.g. app/cache) or absolute(e.g. /var/www/app/cache) paths.

Purpose Command
Clear Cache rm /var/www/cache/*
Database Migration php app/db_migration.php
Visit URL wget -q http://example.com/cron/
Visit URL with output wget -O writable_dir/cron.log http://example.com/cron_script.php

 Read Only File System 

Remember that cron commands run in a Read Only File System. Any cron jobs that create files or any output, must create those files in Shared Writable Directories.

If you don't need the output from a cron command it may be simpler to use curl intstead of wget since curl doesn't attempt to create a file by default.

Schedule

Create the schedule you'd like the command to run on. Here are a few examples.

Interval Description Schedule
Yearly Run once a year, midnight, Jan. 1st 0 0 1 1 *
Monthly Run once a month, midnight, first of month 0 0 1 * *
Weekly Run once a week, midnight on Sunday 0 0 * * 0
Daily Run once a day, midnight 0 0 * * *
Hourly Run once an hour, beginning of hour 0 * * * *

Be aware that there are no Predefined Sheduling Definitions(e.g. @daily, @hourly, etc.) available on Pagoda Box. All schedules must be numeric.

 15 Minute Limit 

The shortest available cron schedule is every 15 Minutes.

 Crons Will Not Run While an App is Sleeping 

Idle, unscaled applications are periodically put to sleep on Pagoda Box. If an app is sleeping, cron jobs will not run—automatically or manually. To keep you app from going to sleep, you can either scale any of your components or add caffeine.

Boxfile

If you'd like to configure a cron job using the Boxfile, nest a cron node under the appropriate web component.

 YAMLCron Job 

/Boxfile

 Things to Note About Cron Jobs in Your Boxfile 

It's important to note that when you remove a Cron Job from your Boxfile, it will not actually remove the Cron Job from your application. This can only be done in your App Dashboard.

Also, if you make an edit to a Cron Job in your Boxfile, it will not edit the existing job, but create a new Cron Job instead. You will need to delete the previous version of your job in your App Dashboard.

Emailing the Output of Your Cron Job

Pagoda Box allows you to specify email addresses to notify when your Cron Job runs. Part of the email will be the actual output of the script or command. You can specify these email addresses in your App Dashboard when creating a new Cron Job or editing an existing job. Adding notification email addresses can not be done in the Boxfile.

  Adding Cron Email Recipients  

If you have any suggestions or corrections for this guide, please submit a ticket.

Contact Us