PS_LOG=/tmp/ps.log
do_start(){
echo "=== `date` : start logging" >> $PS_LOG
do_log=YES
}
do_stop(){
echo "=== `date` : stop logging" >> $PS_LOG
do_log=NO
}
do_log=NO
trap do_start USR1
trap do_stop USR2
while true; do
sleep 1
if [ "$do_log" = "YES" ]; then
echo >> $PS_LOG
echo "*** `date`" >> $PS_LOG
ps x >> $PS_LOG
fi
done
$ ./ps_log.sh &
[1] 3384 #サーバのプロセスID
$ cat /tmp/ps.log
cat: /tmp/ps.log: No such file or directory
$ kill -USR1 3384 #記録開始
$ cat /tmp/ps.log
=== Mon Mar 8 21:07:41 2010 : start logging
*** Mon Mar 8 21:07:41 2010
$ kill -USR2 3384 #記録停止
$ tail /tmp/ps.log
*** Mon Mar 8 21:08:11 2010
=== Mon Mar 8 21:08:17 2010 : stop logging
$ kill 3384 #サーバを終了