P_PROC=`ps aux | grep $PPID | grep sshd | awk '{ print $11 }'` if tty -s;then if [ "$P_PROC" = sshd: ]; then script -q /var/log/script/`whoami`_`date '+%Y%m%d%H%M%S'`.log exit fi fi ifの分岐は無限ループ起こさないようにするためのポイント! ログ保存用ディレクトリ作成 /var/log/script/は各ユーザが書き込めるようにパーミッション設定しておきます。