title: Monit script not working to restart service
body: <p>This is my first post so please be patient with me!</p>
  
  <p>I have tried to create script that checks if service is unreachable (http error code), then Monit should restart program (Preview Service). Monit is run as user "spark".</p>
  
  <p>This is phantomjs-check.sh code:</p>
  
  <pre><code>#!/bin/bash
  # source: /opt/monit/bin/phantomjs-check.sh
  
  url="localhost:9001/preview/phantomjs"
  
  response=$(curl -sL -w "%{http_code}\\n" $url | grep 200)
  
  if [ "$response" = "}200" ]
  then
          echo "-= Toimii!!!! =-"
          exit 1
  else
          echo "-= RiKKi!!!! =-"
          exit 0
  fi
  [root@s-preview-1 bin]#
  </code></pre>
  
  <p>If I manually kill previewservice and run that script I get exit code of 0 which is how that should work.</p>
  
  <p>In Monit I have following conf:</p>
  
  <pre><code>check program phantomjs with path "/opt/monit/bin/phantomjs-check.sh"
  if status = 0 then exec "/opt/monit/bin/testi.sh"
  </code></pre>
  
  <p>Currently I added some logging to it and this is test.sh code:</p>
  
  <pre><code>#!/bin/sh
  # source: /opt/monit/bin/testi.sh
  
  ############# Added this for loggin purposes ############
  #########################################################
  
  dt=$(date '+%d/%m/%Y %H:%M:%S');
  echo Testi.sh run at $dt &gt;&gt; /tmp/testi.txt
  
  # Original part of the script
  sudo bash /opt/previewservice/preview-service.sh start
  </code></pre>
  
  <p>In /etc/sudoers file I have line:</p>
  
  <pre><code>spark ALL=(ALL) NOPASSWD: /opt/previewservice/preview-service.sh
  </code></pre>
  
  <p>This command works from cli and it starts/restarts previewservice. I can run "testi.sh" script manually as spark <code>[spark@s-preview-1 bin]$ ./testi.sh</code> and it works as intended, but even Monit gets info that service is down it doesn't start. </p>
  
  <pre><code>$ cat /tmp/testi.txt
      Testi.sh run at 05/01/2018 10:30:04
      Testi.sh run at 05/01/2018 10:31:04
      Testi.sh run at 05/01/2018 10:31:26
  </code></pre>
  
  <p>$ <code>cat /tmp/previews.txt</code> (This line was created by preview-service.sh start script so it has been run.</p>
  
  <pre><code>File created 05/01/2018 09:26:44
  ********************************
  Preview-service.sh run at 05/01/2018 10:31:26
  </code></pre>
  
  <p><code>tail -f -n 1000 /opt/monit/logfile</code> shows following</p>
  
  <pre><code>[EET Jan  5 10:29:04] error    : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
  [EET Jan  5 10:29:04] info     : 'phantomjs' exec: /opt/monit/bin/testi.sh
  [EET Jan  5 10:30:04] error    : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
  [EET Jan  5 10:30:04] info     : 'phantomjs' exec: /opt/monit/bin/testi.sh
  [EET Jan  5 10:31:04] error    : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
  [EET Jan  5 10:31:04] info     : 'phantomjs' exec: /opt/monit/bin/testi.sh
  [EET Jan  5 10:32:04] error    : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
  [EET Jan  5 10:32:04] info     : 'phantomjs' exec: /opt/monit/bin/testi.sh
  [EET Jan  5 10:33:04] info     : 'phantomjs' status succeeded
  </code></pre>
  
  <p>And the last status succeeded comes when I run that testi.sh script as spark without sudoing.</p>
  
  <p>Any tips what I should try next? I appreciate all the help I can get!</p>
  
  <ul>
  <li><a href="https://i.stack.imgur.com/DLQQL.png" rel="nofollow noreferrer">Monit Previewservice status - Failed</a></li>
  <li><a href="https://i.stack.imgur.com/wXBzu.png" rel="nofollow noreferrer">Manually as spark it works.</a></li>
  </ul>