In many cases, a developer will want to create scripts using the same API that is used in Webconfig. Scripts can be used to:
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.
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:
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.
Returns boolean value to indicate whether your script is running or not.
#!/usr/clearos/sandbox/usr/bin/php <?php /** * ClearOS Example Script * * @category apps * @package developer * @subpackage scripts * @author ClearFoundation <developer@clearfoundation.com> * @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 <http://www.gnu.org/licenses/>. // /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// // 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