How to check a process is running in monit - linux

check process myproc with pidfile /opt/management/myproc.pid
start program = "/etc/init.d/myproc start"
stop program = "/etc/init.d/myproc stop"
if 3 restarts within 3 cycles then timeout
mode active
group local
Above is monit file for myproc. When I clone my vm, sometimes the myproc.pid file is exist but actual process is not running in the cloned vm. In this case, monit still thinks myproc is running and not attempting to start my process in newly cloned vm.
Any suggestions to solve this issue?

Related

Run script in userdata Detached

I am trying to start and run a long running process on my ec2 instance via userdata.
This process needs to stay running for the lifespan of the instance.
This process also needs to run as another user i.e centos and not root.
I am able to run the script and it works fine but it will not allow me to ssh into the server until I stop/start the instance.
Here is how I am running the process now.
I get the error Connection refused
/bin/su -c "/home/centos/downloadAndStart.sh" - centos /dev/null &/dev/null &
without running the script I am able to ssh into the server as soon as it initializes.
I believe this is because of something like the process is still attached to the PID of the userdata script? Does that make sense?
I am not entirely sure why I cannot ssh into the instance until after I restart the instance.

Centos 7: Auto restart application if it stopped

I want to auto restart my application "Fiware IoT Agent" if it stopped, the problem is that it depends of Mongo Db Data Base and the Mosquitto broker. My OS is centOS 7
Here is the commands that I use to launch my three application in the following order:
*Mongo:
/usr/local/iot/mongodb-linux-x86_64-3.0.5/bin/mongod --dbpath /usr/local/iot/mongodb-linux-x86_64-3.0.5/data/db$
*Mosquitto broker
/usr/sbin/mosquitto -c /etc/iot/mosquitto.conf &
pid=$!
echo $pid > /var/run/iot/mosquitto.pid
Iot Agent:
than I start my application using this command
export LD_LIBRARY_PATH=/usr/local/iot/lib
/usr/local/iot/bin/iotagent -i 192.168.1.11 -p 80 -v DEBUG -d /usr/local/iot/lib -c /etc/iot/config.json
how can I start my application if it stopped known that it depends of the other two application? If for example Mongo DB stopped, I must be able to restart it and then to restart my application.
CentOS 7 uses systemd. You can create systemd service for each of your applications and specify dependencies between them. And specify "Restart=always" for service which need to be auto restarted.
You can create your own watch dog code. When you start your application get the pid of the process and the pid of mongo DB.
Every couple of second like 10 seconds check that the pid of both process still exist, or you can also make the programs touch a file every couple of seconds as well then check the file modification time to see if the programs are still alive.
If the program hasn't touched the file or if you go jus the pid route and the pid doesn't exist. Then the program has died.
Restart the program and get the new pid and go about again in a forever while loop.

Monit check processes for multiple pidfiles

I have a Nodejs Web app with multiple node process, which I start using pm2 start app.js multiple times.
To monitor these processes usinf monit, I created a init script using pm2's : pm2 startup ubuntu command. Then, in the monit config file for my App, I use this init script as start and stop program commands for monit. Then I use something like check process pm2_1 with pidfile /path/to/node-pidfile, for both the processes in the monit config file.
I would like monit to check the pidfiles of both these processes and when either or both processes are down, restart both the processes. So, here's what my my-webapp.monitrc looks like :
check process pm2_1
with pidfile /root/.pm2/pids/proc1-0.pid
start program = "/etc/init.d/pm2-init.sh start"
stop program = "/etc/init.d/pm2-init.sh stop"
check process pm2_2
with pidfile /root/.pm2/pids/proc2-1.pid
start program = "/etc/init.d/pm2-init.sh start"
stop program = "/etc/init.d/pm2-init.sh stop"
The problem is, if either of the processes is down,it works. But if both processes are down, monit executes the start command twice.
Is there a way to have a "OR" condition for check process to monitor multiple different pidfiles and execute the same start and stop commands only once ?

Node.js Ubuntu and Monit

I'm working on getting a Node server up with upstart and monit instead of using a cron job to run a script to check on things. I've built an admin dashboard for the server that uses the Node os module for things like os.loadavg() and os.totalmem(), etc...
The problem is, when monit is running, os.loadavg() always returns [0, 0, 0]. Has anyone else encountered this problem? Does monit create a lock or something that does not allow Node to read that property?
Thanks in advance for any help!
Monit Script
check process flinch
with pidfile "/var/run/flinch.pid"
start program = "/sbin/start flinch"
stop program = "/sbin/stop flinch"
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 0% then alert
To give this question some closure, I removed monit from the system check and wrote a custom bash script that checks the process and it runs on during a cron job every minute. Monit seems to put a lock on the system stats when in use.

Linux command to restart application

For example - i have process id which i want to restart. What command i should use to restart this process application ? I didn't find something about it(
Thanks!
You can find very similar question at Restart process script linux.
Linux doesn't have general command for restart, normally you should kill your process and start it over. However, if your process has been started as a service, i.e. it's contained in /etc/init.d/ directory, then you can do the following:
/etc/init.d/SERVICE_NAME restart
or
service SERVICE_NAME restart

Resources