読者です 読者をやめる 読者になる 読者になる

michihide's blog

技術メモおよび雑感

CLI起動とcron起動の違い

$ cat _server.php
#!/usr/bin/php
<?php
print_r($_SERVER);

このようなPHPスクリプトで挙動を観察してみた。
まずはコマンドラインから起動。

$ ./_server.php
Array
(
    [HOSTNAME] => atlas.example.com
    [TERM] => xterm
    [SHELL] => /bin/bash
    [HISTSIZE] => 1000
    [NLS_LANG] => JAPANESE_JAPAN.AL32UTF8
    [SSH_CLIENT] => 10.28.1.30 62948 22
    [SSH_TTY] => /dev/pts/2
    [USER] => m-hotta
    [LS_COLORS] => no=00:fi=00:di=01;33:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;35:*.png=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:
    [MAIL] => /var/spool/mail/m-hotta
    [PATH] => /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/sbin
    [INPUTRC] => /etc/inputrc
    [PWD] => /home/m-hotta/lc_view/progs
    [LANG] => en_US.UTF-8
    [SSH_ASKPASS] => /usr/libexec/openssh/gnome-ssh-askpass
    [SHLVL] => 1
    [HOME] => /home/m-hotta
    [LOGNAME] => m-hotta
    [CVS_RSH] => ssh
    [SSH_CONNECTION] => 10.28.1.30 62948 10.27.4.26 22
    [LESSOPEN] => |/usr/bin/lesspipe.sh %s
    [G_BROKEN_FILENAMES] => 1
    [_] => ./_server.php
    [OLDPWD] => /home/m-hotta
    [ORACLE_HOME] => /usr/lib/oracle/10.2.0.3/client/lib
    [PHP_SELF] => ./_server.php
    [SCRIPT_NAME] => ./_server.php
    [SCRIPT_FILENAME] => ./_server.php
    [PATH_TRANSLATED] => ./_server.php
    [DOCUMENT_ROOT] =>
    [REQUEST_TIME] => 1291857301
    [argv] => Array
        (
            [0] => ./_server.php
        )

    [argc] => 1
)

次はこのようなcronを仕掛けて、

m-hotta@gtr:~$ crontab -l
MAILTO=hotta@example.com
HOME=/home/m-hotta
LANG=ja_JP.UTF-8
SUDO=/usr/bin/sudo
RM=/bin/rm
MV=/bin/mv
 */1 * * * * $HOME/lc_view/progs/_server.php

これで飛んできたメールの中身がこれ。

Array
(
   [SHELL] => /bin/sh
   [MAILTO] => hotta@example.com
   [USER] => m-hotta
   [RM] => /bin/rm
   [PATH] => /usr/bin:/bin
   [SUDO] => /usr/bin/sudo
   [PWD] => /home/m-hotta
   [MV] => /bin/mv
   [LANG] => ja_JP.UTF-8
   [SHLVL] => 1
   [HOME] => /home/m-hotta
   [LOGNAME] => m-hotta
   [_] => /home/m-hotta/lc_view/progs/_server.php
   [ORACLE_HOME] => /usr/lib/oracle/10.2.0.3/client/lib
   [PHP_SELF] => /home/m-hotta/lc_view/progs/_server.php
   [SCRIPT_NAME] => /home/m-hotta/lc_view/progs/_server.php
   [SCRIPT_FILENAME] => /home/m-hotta/lc_view/progs/_server.php
   [PATH_TRANSLATED] => /home/m-hotta/lc_view/progs/_server.php
   [DOCUMENT_ROOT] =>
   [REQUEST_TIME] => 1291857241
   [argv] => Array
       (
           [0] => /home/m-hotta/lc_view/progs/_server.php
       )

   [argc] => 1
)

コマンドラインでの実行に比べて、かなり情報が欠落しているのがわかる。
HOSTNAMEやSERVER_NAMEは、必要であればcrontabの中で設定してやる必要がありそう。