Rev 918 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/php<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2015~2024 Min-Jhin,ChenThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QBPWCF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with QBPWCF. If not, see <http://www.gnu.org/licenses/>.*//*說明:管理排程的執行範例:開啟daemonauto.php --daemon on*/#使用命名空間qbpwcfnamespace qbpwcf;#取得 lib pathexec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);#如果執行失敗if($status!==0){#debugvar_dump(__LINE__,$output);#結束執行,回傳shell 1.exit(1);}#if end#儲存lib path$folderOfUsrLib=$output[0];#以該檔案的實際位置的 lib path 為 include path 首位$output=array();exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);#如果執行失敗if($status!==0){#debugvar_dump(__LINE__,$output);#結束執行,回傳shell 1.exit(1);}#if end#設置 include pathset_include_path($output[0].PATH_SEPARATOR.get_include_path());#匯入外部套件include("allInOne.php");#說明函式function help(){#印出指令說明echo "Usage of ".basename(__FILE__).":".PHP_EOL;echo "--daemon [on] 代表要啟動 daemon".PHP_EOL;echo "--socket [unix domain socket path and name] daemon 模式若需要建立 unix domain socket 的路徑與名稱,預設為\"/usr/lib/qbpwcf/qbpwcf-autoService.sock\"".PHP_EOL;echo "--config [schedule config path and name] daemon 模式所存取的排程設定檔案,預設位置為/usr/local/etc/qbpwcf-schedule.conf.php".PHP_EOL;echo "--log [write log file] 要將 log 寫入到哪邊?預設為\"/var/log/auto.log\"".PHP_EOL;echo "--action [command to run] 排程要執行的指令".PHP_EOL;echo "--startTime [HourHour:MinMin] 代表今日為HourHour:MinMin時要執行,若不設定代表立即執行.".PHP_EOL;echo "--startDate [YearYearYearYear-MonthMonth-DayDay] 代表特定日期才開始,預設為今天".PHP_EOL;echo "--repeat [times of repear] 預設為 -1 代表無執行周期上限".PHP_EOL;echo "--uuid [uuid of action] 排程的唯一識別碼,若不指定會隨即產生".PHP_EOL;echo "--cycleTime [time of cycle ] 周期時間,亦即過了多少時間要再執行一次,單位為秒".PHP_EOL;echo "--fixedMonth [Month][Month] 代表要固定月份執行".PHP_EOL;echo "--fixedDay [DayDay] 代表要固定日執行".PHP_EOL;echo "--fixedHour [HoutHour] 代表要固定小時執行".PHP_EOL;echo "--fixedMin [MinMin] 代表要固定分鐘執行".PHP_EOL;echo "--getList [yes] 代表要取得當前(更新後)的排程清單".PHP_EOL;#結束執行exit;}#function help end#函式說明:#解析參數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],解析好的參數陣列.#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.#$result["program"],字串,執行的程式名稱.#必填參數:#無#可省略參數:#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.$conf["helpFunc"]="help";#備註:#僅能在命令列底下執行.#建議:#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .$parseArgu=cmd::parseArgu($conf);unset($conf);#如果解析參數失敗if($parseArgu["status"]==="false"){#印出結果var_dump($parseArgu);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#函式說明:#使用 linux 的 uuid 指令來產生 uuid 字串#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函式名稱.#$result["content"],uuid.#必填參數:#無.#可省略參數:#無.#參考資料:#無.#備註:#無.$uuid=cmd::uuid();#如果建立uuid失敗if($uuid["status"]==="false"){#印出結果var_dump($uuid);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#取得uuid$uuid=$uuid["content"];#檢查參數#函式說明:#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["varInput"]=&$parseArgu["content"];#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");#$conf["mustBeFilledVariableName"]=array("backupAddr");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.#$conf["mustBeFilledVariableType"]=array("array");#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["skipableVariableName"]=array("daemon","socket","config","log","action","startTime","startDate","repeat","uuid","cycleTime","fixedMonth","fixedDay","fixedHour","fixedMin","getList");#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["skipableVariableType"]=array("array","array","array","array","array","array","array","array","array","array","array","array","array","array","array");#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["skipableVarDefaultValue"]=array(null,null,null,array("/var/log/auto.log"),null,null,null,array(array("times"=>"-1")),null,null,null,null,null,null,null);#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".#$conf["disallowAllSkipableVarIsEmptyArray"]="";#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php$checkArguments=variableCheck::checkArguments($conf);unset($conf);#若執行失敗if($checkArguments["status"]==="false"){#印出結果var_dump($checkArguments);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#若檢查不通過if($checkArguments["passed"]==="false"){#印出結果var_dump($checkArguments);#提示用法help();#結束執行exit;}#if end#如果有 config 參數if(isset($parseArgu["content"]["config"])){#取得 config 參數$config=$parseArgu["content"]["config"][0];}#if end#如果有 socket 參數if(isset($parseArgu["content"]["socket"])){#取得 socket 參數$socket=$parseArgu["content"]["socket"][0];}#if end#如果有 log 參數if(isset($parseArgu["content"]["log"])){#取得 log 參數$log=$parseArgu["content"]["log"][0];}#if end#如果有 deamon 參數if(isset($parseArgu["content"]["daemon"])){#取得 daemon 參數$daemon=$parseArgu["content"]["daemon"][0];}#if end#反之else{#設置不為daemon模式$daemon="off";}#else end#如果有 daemon 為 "on"if($daemon==="on"){#函式說明:#建立unix domain socket server,依照設定檔autoService::createList建立的設定檔案來執行排程事項.#回傳結果:#$result["ststus"],執行是否正常,"true"代表執行正常,"false"代表執行不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],所使用的參數.#必填參數:#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#可省略參數:#如果有config參數if(isset($config)){#$conf["config"],字串,排程的設定檔位置,預設為"/usr/local/etc/qbpwcf-schedule.conf.php".$conf["config"]=$config;}#if end#如果有socket參數if(isset($socket)){#$conf["unixDomainSocket"],字串,unix domain socket 檔案要放在哪裡,預設為"/usr/lib/qbpwcf/qbpwcf-autoService.sock".$conf["unixDomainSocket"]=$socket;}#if end#如果有log參數if(isset($log)){#$conf["log"],字串,要將log寫到哪個地方,若有指定就會嘗試寫log.$conf["log"]=$log;}#if end#可省略參數:#無.#參考資料:#無.#備註:#無.$daemon=autoService::daemon($conf);unset($conf);#若執行失敗if($daemon["status"]==="false"){#印出結果var_dump($uuid);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#執行到這邊代表意外結束exit(1);}#if end#如果有 startTime 參數if(isset($parseArgu["content"]["startTime"])){#取得 startTime 參數$startTime=$parseArgu["content"]["startTime"][0];}#if end#反之else{#為當下時分秒$startTime=gmdate("H:i:s");}#else end#如果有 startDate 參數if(isset($parseArgu["content"]["startDate"])){#取得 startDate 參數$startDate=$parseArgu["content"]["startDate"][0];}#if end#反之else{#為當下年月日$startDate=gmdate("Y-m-d");}#else end#如果有 repeat 參數if(isset($parseArgu["content"]["repeat"])){#取得 repeat 參數$repeat=$parseArgu["content"]["repeat"][0];}#if end#如果有 uuid 參數if(isset($parseArgu["content"]["uuid"])){#取得 repeat 參數$uuid=$parseArgu["content"]["uuid"][0];}#if end#反之else{#函式說明:#使用 linux 的 uuid 指令來產生 uuid 字串#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函式名稱.#$result["content"],uuid.#必填參數:#無.#可省略參數:#無.#參考資料:#無.#備註:#無.$uuid=cmd::uuid();#如果建立uuid失敗if($uuid["status"]==="false"){#印出結果var_dump($uuid);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#取得uuid$uuid=$uuid["content"];}#else end#如果有 fixedMonth 參數if(isset($parseArgu["content"]["fixedMonth"])){#取得 fixedMonth 參數$fixedMonth=$parseArgu["content"]["fixedMonth"][0];}#if end#反之else{#設置 fixedMonth 參數跟開始月份一樣$fixedMonth=$startDate[5].$startDate[6];}#else end#如果有 fixedDay 參數if(isset($parseArgu["content"]["fixedDay"])){#取得 fixedDay 參數$fixedDay=$parseArgu["content"]["fixedDay"][0];}#if end#反之else{#設置 fixedDay 參數跟開始日一樣$fixedDay=$startDate[8].$startDate[9];}#else end#如果有 fixedHour 參數if(isset($parseArgu["content"]["fixedHour"])){#取得 fixedHour 參數$fixedHour=$parseArgu["content"]["fixedHour"][0];#如果只有輸入一個數字if(strlen($fixedHour)===1){#前面補0$fixedHour="0".$fixedHour;}#if end}#if end#反之else{#設置 fixedHour 參數跟開始小時一樣$fixedHour=$startTime[0].$startTime[1];}#else end#如果有 fixedMin 參數if(isset($parseArgu["content"]["fixedMin"])){#取得 fixedMin 參數$fixedMin=$parseArgu["content"]["fixedMin"][0];}#if end#反之else{#設置 fixedMin 參數跟開始分鐘一樣$fixedMin=$startTime[3].$startTime[4];}#else end#如果有 cycleTime 參數if(isset($conf["cycleTime"])){#取得cycle參數$cycleTime=$parseArgu["content"]["cycleTime"][0];}#if end#如果有 action 參數if(isset($parseArgu["content"]["action"])){#取得 action 參數$action=$parseArgu["content"]["action"][0];#解析指令與參數#函式說明:#將指令字串解析成陣列,方便給予 external::callShell 使用#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],解析好的指令陣列.#$result["cmd"],解析好的指令名稱.#$result["argus"],解析好的參數陣列.#$result["argu"],所使用的參數.#必填參數#$conf["cmdStr"],字串,要解析的指令字串$conf["cmdStr"]=$action;#可省略參數:#無.#參考資料:#無.#備註:#無.$parseCmdString=cmd::parseCmdString($conf);unset($conf);#若執行失敗if($parseCmdString["status"]==="false"){#印出結果var_dump($parseCmdString);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#初始化給createList函式的repeat參數$repeatArray=array();#設置重複的規則$repeatArray["times"]=$repeat;#如果有 $cycleTimeif(isset($cycleTime)){#設置周期時間$repeatArray["after"]=$cycleTime;}#if end#反之如果有設定固定時間else{#組合成固定執行時間的年月日時分秒$dateAndTime=gmdate('Y')."-".$fixedMonth."-".$fixedDay." ".$fixedHour.":".$fixedMin.":00";#函式說明:#計算時間點距離1970/01/01多少秒,亦即unixtime.#回傳結果:#$result["stauts"],執行是否成功,"true"代表執行成功;"false"代表執行失敗.#$result["error"],錯誤訊息陣列.#$result["content"],時間點距離1970/01/01的多少秒.#$result["argu"],使用的參數.#$result["unixtime"],執行本函式時的unixtime.#$result["oriDateAndTime"],原始輸入的內容,若replace0參數為"true",才會有該回傳內容.#必填參數:#$conf["dateAndTime"],字串,格式爲 2011-05-27 23:59:59.$conf["time::caculateTimeAmount"]["dateAndTime"]=$dateAndTime;#可省略參數:#$conf["UTC"],字串,是否要用當前時區的設定去換算成unixtime,預設為"false",代表不要;反之為"true".$conf["time::caculateTimeAmount"]["UTC"]="true";#$conf["replace0"],字串,是否要將連續為0的內容替換成當下時間,預設為"true"代表要;反之為"false".$conf["time::caculateTimeAmount"]["replace0"]="false";#參考資料:#無.#備註:#無.$caculateTimeAmount=time::caculateTimeAmount($conf["time::caculateTimeAmount"]);unset($conf["time::caculateTimeAmount"]);#如果執行失敗if($caculateTimeAmount["status"]=="false"){#印出結果var_dump($caculateTimeAmount);#結束執行,回傳shell 1,代表異常.exit(1);}#if end#取得相對於update的固定時間$fixedTime=$caculateTimeAmount["unixtime"];#設置固定時間$repeatArray["fixed"]=$fixedTime;}#if end#函式說明:#新增或覆蓋系統上有哪些要自動執行的工作.#回傳結果:#$result["ststus"],執行是否正常,"true"代表執行正常,"false"代表執行不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],當前排程資訊內容.#$result["argu"],使用的參數.#必填參數:#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#$conf["actions"],字串陣列,每個元素代表要執行的事情,每個事情為要執行的指令與參數所組成的一維陣列.$conf["actions"]=$parseCmdString["content"];#$conf["times"],字串陣列,每個元素代表要執行事情的時間點,單為為秒.格式為UTC時區的「年年年年-月月-日日 時 時:分分:秒秒」格式,事情不存在於啟動的狀態且當前時間大於等於之就會執行.$conf["times"]=array($startDate." ".$startTime);#$conf["repeat"],字串陣列,每個元素為一維陣列,代表要執行事情的重複方式.key為"times"代表剩下幾次可以執行,其value為"N"代表剩下N次(若N等於-1,則代表無限重複執行;若N等於0,則代表不用執行);key為"after"代表下次執行的時間為times+加其value;key為"fixed"代表下次執行的時間為一個循環的固定時間點,value為每次循環的固定時間點,例如每天01:05則為1h*3600s+5m*60s=3900,小於一天(86400秒)的時間,因此周期為天.$conf["repeat"]=array($repeat);#可省略參數:#如果有config參數if(isset($config)){#$conf["config"],字串,排程的設定檔位置,預設為"/usr/local/etc/qbpwcf-schedule.conf.php".$conf["config"]=$config;}#if end#如果有uuid參數if(isset($uuid)){#$conf["uuids"],字串陣列,每個排程的uuid,若不指定則會自動產生,若跟既有action的uuid相同,則會覆蓋其相關資訊.$conf["uuids"]=array($uuid);}#if end#參考資料:#無.#備註:#無.$createList=autoService::createList($conf);unset($conf);#若執行失敗if($createList["status"]==="false"){#印出結果var_dump($createList);#結束執行,回傳shell 1,代表異常.exit(1);}#if end}#if end#如果有 getList 參數if(isset($parseArgu["content"]["getList"])){#取得 checkList 參數$getList=$parseArgu["content"]["getList"][0];#如果要印出排程清單資訊if($getList==="yes"){#函式說明:#查詢系統上有哪些要自動執行的工作檔案#回傳結果:#$result["ststus"],執行是否正常,"true"代表執行正常,"false"代表執行不正常.#$result["error"],錯誤訊息陣列.#$result["content"],回傳的結果,要執行的工作資訊陣列,每個元素的key有"uuid"跟"cmd&argu",前者為用於執行完畢時回報的排程項目識別;後者為該排程事項的指令與參數.#$result["function"],當前執行的函式名稱.#$result["found"],識別是否有要執行的動作.#必填參數:#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#可省略參數:#如果有config參數if(isset($config)){#$conf["config"],字串,排程的設定檔位置,預設為"/usr/local/etc/qbpwcf-schedule.conf.php".$conf["config"]=$config;}#if end#可省略參數:#無.#參考資料:#無.#備註:#無.$getList=autoService::getList($conf);unset($conf);#如果執行出錯if($getList["status"]==="false"){#印出結果var_dump($getList);#結束執行,回傳shell 1,代表異常.exit(1);}#if end}#if end#顯示排程清單var_dump($getList);}#if end