0

Trying to get monit to monitor a custom daemon we wrote, and it's just not working with the bash stop/start script. If I run the stop/start script by hand from the command line it's working 100% perfectly, every single time. If it get's executed through monit, the variables is empty. Extract from the script where I am having problems:

GETPID=$(ps aux | grep unicorn | grep master | cut -d" " -f7)
echo "getPID : $GETPID"                                             
echo $GETPID > $PIDFILE

The $GETPID variable is blank when this gets executed with monit. By hand it works perfectly.

Anyone have any ideas?

chepner
  • 497,756
  • 71
  • 530
  • 681
daemonza
  • 527
  • 5
  • 16
  • Does it help if you add a shebang line `#!/bin/sh` as the first line of the file? – tripleee Aug 23 '12 at 09:38
  • got a #!/bin/bash as the first line, the above code is really just a extract from the script. – daemonza Aug 23 '12 at 09:41
  • Does it help if you use `awk '{print $2}'` instead of `cut` since `cut` is fragile to changes in whitespace? – Michał Górny Aug 23 '12 at 09:51
  • 2
    Just out of curiosity: why not use `pgrep` or `pidof` to get your PIDs? – Rody Oldenhuis Aug 23 '12 at 10:04
  • don't have pgrep or pidof installed, it's a custom, extremely barebones linux install on a "appliance". awk gives same result, works from cmd line but not monit. – daemonza Aug 23 '12 at 10:57
  • It's possible that `monit` launches the script in such a way that the she-bang is ignored, but I don't see anything that would cause `sh` to make `GETPID` empty. – chepner Aug 23 '12 at 12:11
  • 1
    At the top of the script, redirect stderr to a file (`exec 2>/tmp/log`), and invoke `set -x`; then examine the log file after your script has been run. Edit anything particularly interesting into the question -- this will show things like the empty-PATH case the proposed question is asking about (by having commands like ps emit not-found errors to stderr). – Charles Duffy Aug 23 '12 at 12:32
  • 2
    Try echoing `$PATH` to a file from within your script. Run it by hand, then within monit. If `$PATH` is empty when run within monit, you will have to supply the full path to `ps`within your script. See http://stackoverflow.com/questions/819944/proper-way-to-run-a-script-using-cron for more information on launching non-interactive scripts. – Barton Chittenden Aug 23 '12 at 12:30
  • Better advice would be to set the PATH at the top of the script, or source any OS-provided scripts for setting up the default environment (`/etc/profile`?), rather than using full paths. – Charles Duffy Aug 23 '12 at 12:34

2 Answers2

0

In general it is not a very good idea to parse the output of ps or ls.

You can write a simple pgrep using find on the proc filesystem:

# find /proc/ -maxdepth 2 -type l -name exe -lname '/bin/bash' -printf '%h\n' 2>/dev/null | sed 's/.*\///'
3580
3595
9504
9869
10054
10156
10193
# pgrep bash
3580
3595
9504
9869
10054
10156
10193
ceving
  • 21,900
  • 13
  • 104
  • 178
0

Thanks for the help. Problem was path to unicorn in rvm install.

daemonza
  • 527
  • 5
  • 16