Developers Documentation

×

Warning

0 error for file:https://clearos.com/dokuwiki2/lib/exe/css.php?t=dokuwiki&tseed=82873f9c9a1f5784b951644363f20ef8

User Tools

Site Tools


Tutorials Creating Scripts

In many cases, a developer will want to create scripts using the same API that is used in Webconfig. Scripts can be used to:

  • Create Webconfig AJAX pages for long process times
  • Provide a set of command line tools
  • Create and run cron (scheduled) jobs

Script Library

The base app includes a library (\app\base\libraries\Script.php) that provides common functions to users creating scripts. By including this library, you can make your script easier to write and maintain while providing needed features like locking.

Functions

lock()

Creates a lock file. If a lock is not able to be created, an exception is thrown. The lock function has two passed in variables:

  1. interval (the interval time in seconds, to wait on a retry attempt)
  2. retries (the number of times to retry and create a lock file)

unlock()

Function to remove your lock file. This function should be called at the end of every script. If exceptions are caught, remember to call this function in your catch statement.

is_running()

Returns boolean value to indicate whether your script is running or not.

Examples

  #!/usr/clearos/sandbox/usr/bin/php
  <?php
  
  /**
   * ClearOS Example Script
   *
   * @category   apps
   * @package    developer
   * @subpackage scripts
   * @author     ClearFoundation 
   * @copyright  2008-2011 ClearFoundation
   * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License version 3 or later
   * @link       http://www.clearfoundation.com/docs/developer/apps/developer/
   */
  ///////////////////////////////////////////////////////////////////////
  //
  // This program is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // This program is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with this program.  If not, see .
  //
  ///////////////////////////////////////////////////////////////////////
  
  ///////////////////////////////////////////////////////////////////////
  // B O O T S T R A P
  ///////////////////////////////////////////////////////////////////////
  
  $bootstrap = getenv('CLEAROS_BOOTSTRAP') ? getenv('CLEAROS_BOOTSTRAP') : '/usr/clearos/framework/shared';
  require_once $bootstrap . '/bootstrap.php';
  
  ///////////////////////////////////////////////////////////////////////
  // T R A N S L A T I O N S
  ///////////////////////////////////////////////////////////////////////
  
  clearos_load_language('base');
  
  ///////////////////////////////////////////////////////////////////////
  // D E P E N D E N C I E S
  ///////////////////////////////////////////////////////////////////////
  
  // Classes
  //--------
  
  use \clearos\apps\base\File as File;
  use \clearos\apps\base\Script as Script;
  
  clearos_load_library('base/File');
  clearos_load_library('base/Script');
  
  // Exceptions
  //-----------
  
  use \Exception as Exception;
  
  ///////////////////////////////////////////////////////////////////////
  // M A I N
  ///////////////////////////////////////////////////////////////////////
  
  //--------------------------------------------------------------------
  // Command line options
  //--------------------------------------------------------------------
  
  $short_options  = '';
  
  // Handle command line options
  //----------------------------
  
  $options = getopt($short_options);
  
  $script = new Script();
  
  // Initialize status
  try {
      if ($script->lock() !== TRUE) {
          echo "Script already running.\n";
          exit(1);
      }
      do_something();
      $script->unlock();
  } catch (Exception $e) {
      echo "Something unexpected occurred: " . clearos_exception_message($e) . "\n";
      $script->unlock();
  }
  
  /////////////////////////////////////////////////////////////////
  // F U N C T I O N S
  /////////////////////////////////////////////////////////////////
  
  /**
   * Do Something.
   *
   * @return void
   */
  
  function do_something()
  {
      sleep(5);
      $file = new File("/tmp/rootkit.sh");
      if ($file->exists())
          echo "Never not be afraid.\n";
  }
  
  // vim: syntax=php
content/en_us/dev_framework_tutorials_creating_scripts.txt · Last modified: 2014/12/23 17:25 by dloper

https://clearos.com/dokuwiki2/lib/exe/indexer.php?id=content%3Aen_us%3Adev_framework_tutorials_creating_scripts&1568839147