LoginSignup
2
0

More than 5 years have passed since last update.

web から、cron の設定をする

Posted at

Web 経由で、cron の設定をする方法です。
Ubuntu 17.04 の Nginx で確認しました。
php7.0-ssh2 をインストールする必要があります。

cron_jul3101.png

<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<script src="/js/jquery-3.2.1.min.js"></script>
<script src="cron_edit.js"></script>
<link href="cron_edit.css" rel="stylesheet">
<title>Cron Edit</title>
</head>
<body>
<h1>Cron Edit</h1>
<textarea name="text" id="text"></textarea>
<br />
<button id="submit">Submit</button>
<br />
<hr />
<div id="outarea_aa">outarea_aa</div>
<div id="outarea_bb">outarea_bb</div>
<div id="outarea_cc">outarea_cc</div>
<div id="outarea_dd">outarea_dd</div>
<div id="outarea_ee">outarea_ee</div>
<div id="outarea_ff">outarea_ff</div>
<div id="outarea_gg">outarea_gg</div>
<div id="outarea_hh">outarea_hh</div>
<hr />
Version: Jul/31/2017<p />
</body>
</html>
cron_edit.css
/* -------------------------------------------------------------- */
/*
        cron_edit.css

                    Jul/31/2017
*/
/* -------------------------------------------------------------- */
#text {
    width:640px;
    height:400px;
}

button#submit {
    width:200px;
    height:30px;
}

/* -------------------------------------------------------------- */
cron_edit.js
// -----------------------------------------------------------------------
//  cron_edit.js
//
//                  Jul/29/2017
//
// -----------------------------------------------------------------------
jQuery (function ()
{
    jQuery("#outarea_aa").text ("*** cron_edit.js *** 開始 ***")

    const url_php = "cron_fetch.php"

    jQuery.get (url_php,function (data_rec)
        {
        jQuery("#text").text (data_rec)

        submit_click_monitor ()
        })

    jQuery("#outarea_hh").text ("*** cron_edit.js *** 終了 ***")
})

// -----------------------------------------------------------------------
function submit_click_monitor ()
{
    jQuery("button#submit").click (function ()
        {
        jQuery("#outarea_bb").text ("*** clicked ***")

        const text_in = jQuery("#text").val ()

        const url_php = "cron_update.php"

        const args={"text": text_in}

        jQuery.post (url_php,args,function (data_rec)
            {
            jQuery("#outarea_ff").html (data_rec)
            })
        })
}

// -----------------------------------------------------------------------
cron_config.php
<?php
// ----------------------------------------------------------------
//
//  cron_config.php
//
//                      Jul/31/2017
//
// ----------------------------------------------------------------
date_default_timezone_set('Asia/Tokyo');
//
$host = "127.0.0.1";
$user = "scott";
$password = "tiger";

// ----------------------------------------------------------------
?>
cron_fetch.php
<?php
// ----------------------------------------------------------------
//
//  cron_fetch.php
//
//                      Jul/31/2017
//
// ----------------------------------------------------------------
include "cron_config.php";

$connection = ssh2_connect($host, 22);

if($connection !== false){
    $auth = ssh2_auth_password($connection, $user, $password);
    ssh2_exec($connection, "crontab -l > /tmp/cron.txt");
    $stream = ssh2_exec($connection, "cat /tmp/cron.txt");
    stream_set_blocking($stream, true);
    echo stream_get_contents($stream);
//  ssh2_exec($connection, "exit");
}


// echo "*** cron_fetch.php *** end ***\n";
// ----------------------------------------------------------------
?>
cron_update.php
<?php
// ----------------------------------------------------------------
//
//  cron_update.php
//
//                      Jul/31/2017
//
// ----------------------------------------------------------------
include "cron_config.php";
// ----------------------------------------------------------------
function file_write_proc ($string_out,$file_out)
{
    $fp_out=fopen ($file_out,"w");
    flock ($fp_out,LOCK_EX);
    fputs ($fp_out,$string_out);
    flock ($fp_out,LOCK_UN);
    fclose ($fp_out);

    chmod ($file_out,0666);
}

// ----------------------------------------------------------------

$file_out = "/tmp/cron_sent.txt";

if (isset ($_REQUEST['text']))
    {
    $string_out = $_REQUEST['text'];
//  echo $string_out;

    file_write_proc ($string_out,$file_out);
    }

$connection = ssh2_connect($host, 22);

if($connection !== false){
    $auth = ssh2_auth_password($connection, $user, $password);

    $file_received = "/tmp/cron_received.txt";
    $result = ssh2_scp_send ($connection,$file_out,$file_received);

    echo "result = " . $result . "<br />";

    $command =  "crontab < " . $file_received;
    echo $command . "<br />";
    $result = ssh2_exec($connection, $command);
    echo "result = " . $result . "<br />";

    }

echo "*** cron_edit.php *** end ***\n";
// ----------------------------------------------------------------
?>
2
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
2
0