newlisp 监控Linux进程 二
2013-11-05 12:41
141 查看
在前文newlisp 监控Linux进程 一 中,我已经实现了一个newlisp脚本,能够根据配置文件中的配置检查进程是否存在,并且将检查结果写到本地日志文件中。
本篇继续,当检查进程已经不存在后,要启动进程。但是每个进程启动的命令是不相同的,不可能将所有这些特殊命令都写在process.lsp文件中。最好是将代码以数据的方式卸载filter.lsp配置文件中。然后程序运行时,从配置文件中读取出来,并当作代码执行。由于目前代码比较简单,就是运行一句话,所以暂时还不需要宏这个大杀器出场,只需要用eval函数即可。
现在filter.lsp的配置改了一下:
(set 'filters (list '("dispatch"
"redmine"
(exec "service redmine start"))))
filters这里是一个list, 包含了唯一一个元素也是list,这个唯一的元素list拥有三个元素,第三个也是一个list。前面用了单引号防止过早求值。
process.lsp代码也修改了一点点:
#!/usr/bin/newlisp
(set 'cur-path "/opt/detector")
(load (append cur-path "/filter.lsp"))
(define (check-process filter-str)
(set 'r (exec (append "ps -def | grep " filter-str)))
(set 'l (length r))
(> l 3))
(define (add-log msg)
(println msg)
(append-file (append cur-path "/process.log") (append "\n" (string (now 480)) " "))
(append-file (append cur-path "/process.log") (append ": " msg))
)
(dolist (sub-list filters)
(if (check-process (first sub-list))
(begin
(add-log (append (sub-list 1) " is alive\n"))
)
(begin
(add-log (append (sub-list 1) " is dead\n"))
(eval (sub-list 2)))))
(exit)
本篇继续,当检查进程已经不存在后,要启动进程。但是每个进程启动的命令是不相同的,不可能将所有这些特殊命令都写在process.lsp文件中。最好是将代码以数据的方式卸载filter.lsp配置文件中。然后程序运行时,从配置文件中读取出来,并当作代码执行。由于目前代码比较简单,就是运行一句话,所以暂时还不需要宏这个大杀器出场,只需要用eval函数即可。
现在filter.lsp的配置改了一下:
(set 'filters (list '("dispatch"
"redmine"
(exec "service redmine start"))))
filters这里是一个list, 包含了唯一一个元素也是list,这个唯一的元素list拥有三个元素,第三个也是一个list。前面用了单引号防止过早求值。
process.lsp代码也修改了一点点:
#!/usr/bin/newlisp
(set 'cur-path "/opt/detector")
(load (append cur-path "/filter.lsp"))
(define (check-process filter-str)
(set 'r (exec (append "ps -def | grep " filter-str)))
(set 'l (length r))
(> l 3))
(define (add-log msg)
(println msg)
(append-file (append cur-path "/process.log") (append "\n" (string (now 480)) " "))
(append-file (append cur-path "/process.log") (append ": " msg))
)
(dolist (sub-list filters)
(if (check-process (first sub-list))
(begin
(add-log (append (sub-list 1) " is alive\n"))
)
(begin
(add-log (append (sub-list 1) " is dead\n"))
(eval (sub-list 2)))))
(exit)
相关文章推荐
- newlisp 监控Linux进程 四
- newlisp 监控Linux进程 一
- newlisp 监控Linux进程 四
- newlisp 监控Linux进程 三
- Linux 运行进程实时监控pidstat命令详解
- Linux 运行进程实时监控pidstat命…
- LFCS 系列第十四讲: Linux 进程资源用量监控和按用户设置进程限制
- Linux vmstat --监控虚拟内存,进程,CPU
- JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jsta (Linux 如何查看进程的各线程的CPU占用 )
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
- 使用Shell脚本对Linux系统和进程资源进行监控
- 使用Shell脚本对Linux系统和进程资源进行监控
- demopu教你学linux,shell实现简单的进程监控脚本
- linux下用脚本实现:监控一个进程,不存在则将其启动。
- Linux进程实时IO监控iotop命令详解 (系列一)
- Linux 运行进程实时监控pidstat命令详解
- 使用shell脚本对Linux系统和进程资源进行监控
- linux监控进程并且自动重启
- 使用JCONSOLE监控远程LINUX运行的JAVA进程,总是在报连接失败的错误。
- Linux进程监控日志