This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

Concrete5 is incredibly robust software out of the box, and with the ability to easily extend it's functionality it is also a very powerful framework to use to build advanced web based applications. When building these advanced applications sometimes we incorporate other external programs. For example, a program that will compress videos that have been uploaded by your end users, or a program that will analyze data and send it out to users in a specific User Group Set. Or, in the case I will lay out below, simply run an Automated Job process.

To access c5 from the PHP CLI, the first thing we need to do is create a PHP file that allows only CLI access.

Create a file named "index_CLI.php" in the c5 root directory (the same directory as "index.php"). Add the following code to it:

<?php
# ~/index_CLI.php
defined('STDIN') or die(_("Access Denied. CLI Only")); //Only allow CLI access.
chdir(__DIR__);
 
define('DIR_BASE','.');
define('C5_ENVIRONMENT_ONLY', true);
require_once('config/site.php');
if ( !defined('REDIRECT_TO_BASE_URL') ){
  define('REDIRECT_TO_BASE_URL', false);
}
if ( defined('DIRNAME_APP_UPDATED') ){
  $GLOBALS['APP_UPDATED_PASSTHRU'] = true;  
  require('updates/' . DIRNAME_APP_UPDATED . '/concrete/dispatcher.php');
}
else{
  require('index.php');  
}

What this file does is expose the appropriate c5 architecture securely to the CLI only. Now let's create a script that will run a specific Job.

Create a new directory at the c5 root named "cli". You don't have to do this, but it's a good idea to keep things organized. Next, create a new file named "run_a_specific_job.php" in the newly created "cli" directory. Add the following code:

<?php
# ~/cli/run_a_specific_job.php
chdir(__DIR__);
require('../index_CLI.php'); //Include the file that exposes the c5 architecture
$_REQUEST['auth'] = Job::generateAuth();
$_REQUEST['jID'] = '5'; //This is where you specify which Job ID you want to run
User::loginByUserID(1); //This is not necessary for core jobs, but a privileged session may be required for some custom Jobs
Loader::tool('jobs');

That's it! To run this file from a cron task, simply use the command "php /[INSERT PATH TO C5 ROOT DIRECTORY HERE]/cli/jobs.php"

If you need to record the output of the script, you can use this command: "php /[INSERT PATH TO C5 ROOT DIRECTORY HERE]/cli/jobs.php >> /[INSERT PATH TO C5 ROOT DIRECTORY HERE]/cli/output.txt"

Loading Conversation