startup script和cronolog日誌切分
相關參考資料: http://man.cx/start-stop-daemon(8) http://cronolog.org/usage.html http://book.opensourceproject.org.cn/lamp/ruby/railscook/opensource/0596527314/i_0596527314_chp_13_sect_6.html 安裝cronolog cronolog是個簡單的日誌切分外掛,常見的經典應用就是切分apache的單個龐大日誌,按日期儲存 安裝: -----------------------------------------------------------------------------
$ wget
http://cronolog.org/download/cronolog-1.6.2.tar.gz
$ tar xvzf cronolog-1.6.2.tar.gz
$ cd cronolog-1.6.2
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
----------------------------------------------------------------------------- 檢視裝在哪兒了(which也要sudo許可權那): which cronolog 簡單測試 $ echo "This is a test." | /usr/bin/cronolog -o /var/log/www/%Y/access.%m-%d-%y.log "-o" 命令才能建立檔案 不加的話報錯 ok! 為自己的web server編寫自啟動程式 沒有副檔名的指令碼檔案cam-hello : -----------------------------------------------------------------------------
#! /bin/sh # example python daemon starter script # based on skeleton from Debian GNU/Linux # [email protected] # place the daemon scripts in a folder accessible by root. /usr/local/sbin is a good idea PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON="/usr/local/hello/python_server/HelloServer" NAME=cam-hello DESC="cam hello" CRONOLOG="/usr/local/sbin/cronolog /var/log/www/cam/%Y/access.%m-%d-%y.log" test -f $DAEMON || exit 0 set -e export PIDFILE=/var/run/${NAME}.pid case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --make-pidfile --pidfile ${PIDFILE} --exec "$DAEMON" | $CRONOLOG & ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --pidfile ${PIDFILE} | $CRONOLOG # --exec $DAEMON echo "$NAME." if [ -f ${PIDFILE} ]; then rm ${PIDFILE} fi ### rest of shutdown ### exit 0 ;; #reload) # # If the daemon can reload its config files on the fly # for example by sending it SIGHUP, do it here. # # If the daemon responds to changes in its config file # directly anyway, make this a do-nothing entry. # # echo "Reloading $DESC configuration files." # start-stop-daemon --stop --signal 1 --quiet --pidfile # /var/run/$NAME.pid --exec $DAEMON #;; restart|force-reload) # # If the "reload" option is implemented, move the "force-reload" # option to the "reload" entry above. If not, "force-reload" is # just the same as "restart". # echo -n "Restarting $DESC: " start-stop-daemon --stop --pidfile ${PIDFILE} # --exec $DAEMON sleep 1 start-stop-daemon --start --make-pidfile --pidfile ${PIDFILE} --exec $DAEMON | $CRONOLOG & echo "$NAME." ;; *) N=/etc/init.d/$NAME # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 echo "Usage: $N {start|stop|restart|force-reload}" >&2 exit 1 ;; esac exit 0
----------------------------------------------------------------------------- --make-pidfile 才能建立自啟動檔案,被網上不完善程式誤導,導致只能啟動不能停止服務 ==! python指令碼需更改為可執行檔案 不然無法執行 日誌檔案有個緩衝,達到一定長度才能寫入,很無語(實驗時候還以為python不能這麼弄。。。。) 部署自啟動程式 拷貝到 /etc/rc.d/init.d/ 修改指令碼檔案chmod 777 cam-hello 註冊: /etc/init.d# sudo update-rc.d cam defaults 如果要刪除start/stop links 則: sudo update-rc.d -f cam remove (注: chkconfig --add autoruntest 新增服務 這個命令有錯,不用它! There is a loop between service apache2 and rsyslog if stopped) 部署成功後
/etc/init.d/cam-hello start
/etc/init.d/cam-hello restart
/etc/init.d/cam-hello stop