Monit to turn off machine when program is done

0

I am trying to set up monit to turn off my virtual machine when my program is done. (The program runs as a daemon, takes long time which I do not know in advance, and my machine is expensive so I want to save money.)

I have created the following .monitrc in my home directory:

set daemon 60
set logfile /run/monit.log
set httpd unixsocket /run/monit.socket
    allow baruch:monit
check process python3 matching python3
      start program = "sudo systemctl poweroff"

I have created the log file and the socket file and made them writable.

Still monit monitor all the gives two errors:

/home/baruch/.monitrc:6: Program does not exist: 'sudo'

Unix socket /run/monit.socket connection error -- Connection refused

Any help will be appreciated.

Baruch Youssin

Posted 2018-09-30T03:34:46.560

Reputation: 1

Answers

0

As I have received no answers, I conclude that unfortunately, monit is not well supported by the community and possibly not used that much.

I came up with the following bash script that serves my purpose:

#!/bin/bash
while :
do
if ps -C python3 &> /dev/null
then :
else
sudo systemctl poweroff
fi
sleep 60
done

Here : is a no-op that returns true,

while :
do
...
done

is an infinite loop, python3 is the program I am monitoring, ps -C python3 is a command that returns true if python3 is running (possibly with any parameters) and false otherwise, &> /dev/null discards the output of this command (otherwise it is printed every minute in the terminal), sudo systemctl poweroff is the command that powers off my computer, sleep 60 puts the script to sleep for 1 minute.

I have put this code in a file, named it monitor, made it executable and run it as follows:

./monitor & disown

Here & runs the script in the background and disown allows the script to continue running even after the terminal from which I started it, is closed.

Baruch Youssin

Posted 2018-09-30T03:34:46.560

Reputation: 1