0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cron(クロン)実行先でcron(クロン)一元管理👏

Posted at

cron(クロン)先でcron(クロン)一元管理

制作経緯

  • レンタルサーバー等のcronの制約を打破したいなと思った事があり制作に至る。
    尚、レンタルサーバーでは負荷がある処理はお断りしている場合があるので規約に沿って使用ください。

使用方法

  • cron.jsonに処理を行い日時、実行コマンドを明記する。
  • class_cron.phpに一分間隔で呼び出すようにcronに登録する。
  • class_cron.phpを呼び出す際は参照するjsonファイルのパラメーターを渡す事。
  • cron.jsonの週の項目は日曜日から始まり、フラグが1の時に起動する。
  • cron.jsonの週以外の項目の設定はcrontabの入力方法と同じ関係にあります。
class_cron.php
<?php
date_default_timezone_set('Asia/Tokyo');
class cron
{
    public function __construct(mixed $filepath = "")
    {
        $val = @file_get_contents($this->pval($filepath));
        $obj_ = @json_decode($this->pval($val));
        $obj = (object)[];
        foreach ($obj_ as $key => $value) {
            $obj->name = "month";
            $obj->val = $value->m;
            if ($flg = $this->trigger_check($obj,"m",1,12)) {
                $obj->name = "day";
                $obj->val = $value->d;
                if ($flg = $this->trigger_check($obj,"d",1,31)) {
                    $obj->name = "hour";
                    $obj->val = $value->H;
                    if ($flg = $this->trigger_check($obj,"H",0,23)) {
                        $obj->name = "minutes";
                        $obj->val = $value->i;
                        if ($flg = $this->trigger_check($obj,"i",0,59)) {
                            $obj->name = "week";
                            $obj->val = implode(",", $value->w);
                            if ($flg = $this->trigger_check($obj,"w",0,0)) {
                                $this->command($value->command);
                            }
                        }
                    }
                }
            }
        }
    }
    public function command(mixed $command_val = "")
    {
        $command_val = $this->pval($command_val);
        exec($command_val . " > /dev/null &");
        // print "よろしくお願いします~~~!!".PHP_EOL;
        return true;
    }

    public function pval(mixed $val = "")
    {
        if (is_array($val)) {
            foreach ($val as $key => $value) {
                $val[$key] = strip_tags($value);
            }
        } else {
            $val = strip_tags($val);
        }
        return $val;
    }

    public function trigger_check(mixed $variable = "",mixed $d="",int $min=0 ,int $max=0)
    {
        if (!$variable) return false;
        if ($variable->val === "*") return true;
        switch ($variable->name) {
            case 'week':
                $value = @explode(",", $variable->val);
                return (int)$value[(int)date($d)] === 1 ? true : false;
                break;
            default:
                if (preg_match("/^(\*\/[0-9]{1,})$/", $variable->val)) {
                    $value = @explode("*/", $variable->val)[1];
                    if (is_numeric($value) && $value >= $min && $value <= $max) {
                        return (int)date($d) % $value === 0 ? true : false;
                    }
                }
                if (preg_match("/^([0-9]{1,}\,{1,})/", $variable->val)) {
                    $value = @explode(",", $variable->val);
                    $value = array_map('intval', $value);
                    return in_array((int)date($d), $value, true) === true ? true : false;
                }

                $value = (int)$variable->val;
                if (is_numeric($value) && $value >= $min && $value <= $max) {
                    return $value === (int)date($d) ? true : false;
                }
                return false;
                break;
        }
        return false;
    }
}

if($argv[1]){
    //argv
    new cron($argv[1]);
}



設置例

crontab 一分間隔

*/1 * * * * taoka cd /var/www;/usr/bin/php class_cron.php cron.json

cron.json

cron.json
[
  {
          "m":"*",
          "d":"*",
          "H":"9,2,15",
          "i":"*/5",
          "w":[1,1,1,1,1,1,1],
          "command":"cd /var/www/html/example.com/lib;/usr/bin/php tw_news.php example"
  }
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?