0

I have setup my Mac's launchd to run this monit script:

set daemon 60

set logfile /var/log/monit.log

check host ac_server with address 127.0.0.1
    if failed port 3000
        then exec "/bin/bash -c '/Users/liren/ac-project/monit_task.sh'"

Basically at 60s interval, it will ping my Ruby on Rails server and execute the monit_task.sh script if server is down:

#!/bin/bash

cd "/Users/liren/ac-project/rails_app"
bundle exec "sidekiq -C config/sidekiq.yml" &
rails s -e production

monit script is located at /usr/local/etc/monit/monitrc, executed by my LaunchDaemon plist located at /Library/LaunchDaemons/.

However, the shell script just doesn't get executed without any errors showing in log. Any idea why?

Liren Yeo
  • 3,215
  • 2
  • 20
  • 41
  • 1
    Run `type bundle` and `type rails` in Terminal, then put the correct full paths into your script. – Mark Setchell Jul 02 '18 at 07:56
  • omg wow @MarkSetchell that solves it! Thank you so much!! Please post that as an answer so that I can mark this question as answered :) – Liren Yeo Jul 02 '18 at 08:31
  • @MarkSetchell I am guessing the reason is because I installed my ruby through rbenv which doesn't have root permission. And when mac was booted, before logging in as user, my bash_profile was not sourced and therefore commands like `rails` was still unknown. Is that the right way to put this? – Liren Yeo Jul 02 '18 at 08:52
  • I'm not certain - all I would say is that processes started from `launchd` may run as a different user or with a different environment (including stuff from your profile) and thus may work differently from an interactive session. – Mark Setchell Jul 02 '18 at 10:14

2 Answers2

0

Try running:

type bundle
type rails

to find what is actually being run when you use those commands. Then put the full paths you discover as a result into your script.

#!/bin/bash

cd "/Users/liren/ac-project/rails_app"
/full/path/to/bundle exec "sidekiq -C config/sidekiq.yml" &
/full/path/to/rails s -e production
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

You can simply keep your current script without modifying and adding the full path and just edit something small in monitrc (it doesn't matter if it's linux or mac): here's my answer

edit: don't forget to chmod +x monit_task.sh

Viktova
  • 552
  • 7
  • 19