用newlisp管理windows下的nginx
我写了一个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 view Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ nginx.exe 8048 Console 1 6,716 K nginx.exe 4512 Console 1 6,988 K nginx.exe 1684 Console 1 6,836 K nginx.exe 4652 Console 1 7,132 K nginx.exe 6512 Console 1 6,816 K nginx.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)