2012-11-09 51 views
3

我正在寫關於通過Monit啓動我的java程序的一些幫助。我寫了一個啓動腳本program.sh。 monit代碼和scipt代碼在本文中給出。使用監控監控一個java程序

問題是我無法使用通過monit執行的腳本文件啓動和停止程序。如果我使用終端啓動它,我可以監視該過程,但無法用monit啓動/停止它。來自monit的日誌顯示「無法啓動」

但是,我可以從monit啓動和停止像ssh easliy這樣的程序。 monit在sudo下運行,我從具有管理權限的帳戶運行腳本。 它將如果有人幫助非常有幫助我弄清楚了這一點 感謝

monitrc文件

#++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
#Monit settings 
set daemon 10 with start delay 2   # check services at 2-minute intervals 
set logfile syslog facility log_daemon      
set logfile /var/log/monit.log 
set idfile /var/lib/monit/id 
set statefile /var/lib/monit/state 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
# Mail Server 
set mailserver smtp.gmail.com port 587 
    username "[email protected]" password "password" 
    using tlsv1 with timeout 30 seconds 

set eventqueue 
     basedir /var/lib/monit/events # set the base directory where events will be stored 
     slots 100      # optionally limit the queue size 


set alert [email protected]      # receive all alerts 
set alert [email protected] only on { timeout } # receive just service- 
#            # timeout alert 
#set alert [email protected] { nonexist, timeout, resource, icmp, connection } 
#set alert [email protected] on { checksum, permission, uid, gid } 
# setup the email for the SMS thing over here....................... 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
set httpd port 2813 and 
#  use address localhost # only accept connection from localhost 
    allow localhost  # allow localhost to connect to the server and 
    allow 0.0.0.0/0.0.0.0 
    allow admin:monit  # require user 'admin' with password 'monit' 
# allow @monit   # allow users of group 'monit' to connect (rw) 
# allow @users readonly # allow users of group 'users' to connect readonly 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
set mail-format { 
    from: [email protected]$HOST 
    subject: Monit Alert -- $EVENT $SERVICE 
    message: $EVENT Service $SERVICE 
       Date:  $DATE 
       Action:  $ACTION 
       Host:  $HOST 
       Description: $DESCRIPTION 

      Your faithful employee, 
      Monit 
} 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

#*********************************************************************************************** 
#Computer Resources check 
check system myhost.mydomain.tld 
    if loadavg (1min) > 4 for 5 cycles then alert 
    if loadavg (5min) > 2 for 5 cycles then alert 
    if memory usage > 75% for 3 cycles then alert 
    if swap usage > 25% for 5 cycles then alert 
    if cpu usage (user) > 70% for 5 cycles then alert 
    if cpu usage (system) > 70% for 5 cycles then alert 
    if cpu usage (wait) > 20% for 5 cycles then alert 
#*********************************************************************************************** 


################################################################################################ 
#Monitoring SSH Service 

check process ssh123 with pidfile /var/run/sshd.pid 
start program = "/etc/init.d/ssh start" 
stop program = "/etc/init.d/ssh stop" 
if cpu > 50% for 5 cycles then alert 
if totalmem > 200 MB for 5 cycles then alert 
if children > 2 then alert 
#if loadavg(5min) greater than 10 for 8 cycles then stop 
#if 5 restarts within 5 cycles then timeout 

################################################################################################ 
#Monitoring Prorgam in Java 

check process javaprg with pidfile /home/user/Desktop/Binaries/javaprg.pid 
start program = "/home/user/Desktop/Binaries/javaprg.sh start" 
stop program = "/home/user/Desktop/Binaries/javaprg.sh stop" 
if cpu > 50% for 5 cycles then alert 
if totalmem > 1500 MB for 5 cycles then alert 
if children > 2 then alert 
#if loadavg(5min) greater than 10 for 8 cycles then stop 
#if 5 restarts within 5 cycles then timeout 

啓動/停止腳本

#!/bin/bash 
case $1 in 
    start) 
     echo $$ > javaprg.pid; 
     exec /usr/bin/java -jar javaprg.jar 
     ;; 
    stop) 
     kill $(cat javaprg.pid); 
     rm javaprg.pid 
     ;; 
    *) 
     echo "usage: javaprg {start|stop}" ;; 
esac 
exit 0 

回答

1

你應該設定絕對路徑在你開始停止腳本。

您可以嘗試使用有根的外殼sudo -s啓動它。

而且你應該使用/etc/monit/conf.d文件夾將你的conf文件合併。

+0

管理使其使用絕對路徑。對不起,超級遲到的答案接受! – Sparrow

0

我在嘗試在Monit下配置shell腳本時遇到了同樣的問題。 解決問題的方法是在程序本身之前使用/ bin/sh。

嘗試使用:

start program = "/home/user/Desktop/Binaries/javaprg.sh start"

stop program = "/home/user/Desktop/Binaries/javaprg.sh stop"

0

我不得不使用你的腳本同樣的問題,這是因爲啓動腳本沒有指定保存的PID。它將javaprg.pid保存到/而不是主文件夾。將開始腳本更改爲'echo $$ > /home/usr/binaries/javaprg.pid',它將起作用。