2

Is it possible to redirect the output of an exec command to the monit log?

check host example.org with address example.org
   if failed port 80 protocol http for 2 cycles then
      exec "/root/cloudflare.py >> /var/log/monit.log"

This doesn't seem to work:

[EST Mar  6 16:02:19] error    : 'example.org' failed, cannot open a connection to INET[example.org:80] via TCP
[EST Mar  6 16:02:24] info     : 'example.org' exec: /root/cloudflare.py
[EST Mar  6 16:02:24] error    : 'nginx' process is not running
[EST Mar  6 16:02:24] info     : 'nginx' trying to restart
[EST Mar  6 16:02:24] info     : 'nginx' start: /etc/init.d/nginx
[EST Mar  6 16:02:24] error    : monit: Error reading pid from file '/var/run/nginx.pid'

As you can see it records that it invoked the script, and the script does actually do what it is expected to do, but the directed output is not captured in the log. It does, of course, if I run from the cmdline.

Is there a problem with two processes trying to append to the same log? or is this too much for a monit exec?

John Mee
  • 2,298
  • 1
  • 23
  • 27

1 Answers1

2

If monit uses standard syslog facilities then take a look at logger. You can pipe the output of your command through it and each line will appear as a new message in the relevant log file, for example

echo "Hello Sweetie" | logger

puts a log message into /var/log/messages on the CentOS system I have to hand. I don't use monit but it should be fairly straightforward to figure out how to direct your messages to the relevant log.

user9517
  • 114,104
  • 20
  • 206
  • 289