2013年11月11日月曜日

crontabについて

忘れるのでメモ

編集コマンド

crontab [-u ユーザ名] -e


基本書式

* * * * * [実行コマンド]

左から、分 時 日 月 曜日

以下の数値を設定可能
分 0-59
時 0-23
日 1-31
月 1-12
曜日 0-7 (0または7は日曜日)


日時の指定方法各種

各種時間指定方法
・カンマ区切りで列挙
(例)
0,15,30,45 * * * * /hoge/hoge
毎時0分15分30分45分に/hoge/hogeを実行

・ハイフンつなぎで期間を指定
(例)
「0-6」
0 0-6 * * * /hoge/hoge
0時から6時の間0分に/hoge/hogeを実行

・カンマ区切りとハイフンつなぎの両方
(例)
「0,4-6」
0 0,4-6 * * * /hoge/hoge
0時と4時5時6時の0分に/hoge/hogeを実行

・スラッシュで間隔を指定
(例)
*/5 * * * * /hoge/hoge
5分間隔で/hoge/hogeを実行


メール送信先の設定

デフォルトではcrontabのユーザにメールが送信される。

・メールの送信先を代える場合
crontabの先頭に
MAILTO="xxx@xxx.xx.xx"

・メールを受信したくない場合
crontabの先頭に
MAILTO=""
または、
0 * * * * /hoge/hoge >/dev/null 2>&1

・エラーメールを受信したい
0 * * * * /hoge/hoge 1> /dev/null

・エラー以外のメールを受信したい
0 * * * * /hoge/hoge 2> /dev/null


2013年11月1日金曜日

CodeIgniterでGET, POSTの動作を分ける

FuelPHPで最初に便利だったのがhttpdのメソッドでコントローラーのアクションを分岐できるところ。
FuelPHPが動かないサーバの案件でCodeIgniterを使ったときに真っ先に追加したのがこの機能。

function action_xxx (){
function get_xxx (){
function post_xxx (){

と書くだけで、GET, POSTによるアクションの分岐が可能になります。


以下の内容のMy_Controller.phpを作成。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
var $uploaded;
var $view_headers;
var $view_footers;
public function __construct()
{
parent::__construct();
}
// action_, post_, get_への振り分け
public function _remap($method, $params = array())
{
if ($method == '') $method = 'index';
if (strtolower($this->input->server('REQUEST_METHOD')) == 'post')
{
// POST
if (method_exists($this, 'post_'.$method))
{
return call_user_func_array(array($this, 'post_'.$method), $params);
}
elseif (method_exists($this, 'action_'.$method))
{
return call_user_func_array(array($this, 'action_'.$method), $params);
}
else
{
show_404();
}
}
else
{
// GET
if (method_exists($this, 'get_'.$method))
{
return call_user_func_array(array($this, 'get_'.$method), $params);
}
elseif (method_exists($this, 'action_'.$method))
{
return call_user_func_array(array($this, 'action_'.$method), $params);
}
else
{
show_404();
}
}
}
public function view($viewfile, $data)
{
foreach ($this->view_headers AS $header_file)
{
$this->load->view($header_file, $data);
}
$this->load->view($viewfile, $data);
foreach ($this->view_footers AS $footer_file)
{
$this->load->view($footer_file, $data);
}
}
}

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'AUTH_Controller.php';
?>