问题
I am writing to get some help regarding starting my java programs via Monit. I have written a start script program.sh. The monit code and the scipt code is given with this posting.
The issue is that I am not able the start and stop the program using the the script file executed via monit. I can monitor the process if I start it using the terminal but I can't start/stop it with monit. The log from monit says "Failed to start"
However, I can start and stop programs like ssh easliy from monit. The monit runs under sudo and I am running the scripts from an account with administrative privileges. It will very helpful if someone helps me figure this out Thanks
monitrc file
#++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#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 "monit.abc123@gmail.com" 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 abc123@gmail.com # receive all alerts
set alert abc123@gmail.com only on { timeout } # receive just service-
# # timeout alert
#set alert foo@bar { nonexist, timeout, resource, icmp, connection }
#set alert security@bar 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: monit@$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
Start/Stop script
#!/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:
You should set absolute path in your start stop script.
You can try to launch it using a rooted shell sudo -s
.
And you should concider using the /etc/monit/conf.d
folder to put your conf files.
回答2:
I had the same problem when I was trying to configure a shell script under Monit.
what solved the problem was using the /bin/sh prior to the program itself.
try using:
start program = "/home/user/Desktop/Binaries/javaprg.sh start"
stop program = "/home/user/Desktop/Binaries/javaprg.sh stop"
回答3:
I had the same problem using your script, and it was because the start script did not specify where to save the PID. It was saving javaprg.pid to / rather then the home folder. Change start script to 'echo $$ > /home/usr/binaries/javaprg.pid'
and it will work.
来源:https://stackoverflow.com/questions/13316553/use-monit-to-monitor-a-java-program