我寫了一個nginx.lsp檔案,可以通過傳遞參數來進行Nginx進程管理。
使用方法
1. 從nginx網站下載windows版本的程式,加壓後,將newlisp.lsp檔案複製到該目錄。
2. 確保windows的system32目錄下有newlisp.exe程式。
3. 運行程式, 目前支援5個參數
newlisp nginx.lsp start|stop|monitor|reload|view
比如下面用view來觀察進程:
c:\newlisp nginx.lsp viewImage Name PID Session Name Session# Mem Usage========================= ======== ================ =========== ============nginx.exe 8048 Console 1 6,716 Knginx.exe 4512 Console 1 6,988 Knginx.exe 1684 Console 1 6,836 Knginx.exe 4652 Console 1 7,132 Knginx.exe 6512 Console 1 6,816 Knginx.exe 6868 Console 1 7,152 K
可以用stop參數關閉所有nginx進程。
具體實現代碼如下:
;; parse argument as one of the following;; start|stop|reload|monitor|view(define (start) (process "start.bat") )(define (kill-process e) (if (regex "^nginx.exe" e) (begin(set 'pid ((regex "^nginx.exe[ ]+([0123456789]+)" e) 3))(exec (append "Taskkill /PID " pid " /F")))))(define (handle-element e) (if (regex "^nginx.exe" e) (set 'h true)))(define (check-nginx-process) (set 'cmd "tasklist /fi \"imagename eq nginx.exe\"") (set 'result (exec cmd)) (set 'h nil) (dolist (str result) (handle-element str)) )(define (kill-nginx-processes) (set 'cmd "tasklist /fi \"imagename eq nginx.exe\"") (set 'result (exec cmd)) (set 'h nil) (dolist (str result) (kill-process str)) )(define (view) (set 'cmd "tasklist /fi \"imagename eq nginx.exe\"") (set 'result (exec cmd)) (dolist (str result) (println str)) )(define (stop) (set 'cmd "tasklist /fi \"imagename eq nginx.exe\"") (set 'result (exec cmd)) (check-nginx-process) (if h (begin(println "stoping nginx now ...")(exec "nginx -s stop")(check-nginx-process)(if h (begin (println "kill nginx processes ...") (kill-nginx-processes) ))) (println "do nothing because nginx is stopped")) )(define (reload) (exec "nginx -s reload") )(define (monitor) (while true (begin (sleep 10000) (println "check nginx process at 10 seconds") (check-nginx-process) (unless h (begin (println "nginx is stopped, start it now ...") (start)) (println "nginx process is alive") ) ) ) )(define (main-fun) (set 'action ((main-args) 2)) (unless action (begin (println "please pass one of the arguments: start|stop|reload|monitor|view") (exit))) (if (= "start" action) (start)) (if (= "stop" action) (stop)) (if (= "reload" action) (reload)) (if (= "monitor" action) (monitor)) (if (= "view" action) (view)) )(main-fun)(exit)