0

I'm writing a simple function to test whether Cassandra is running on the local machine (by capturing the output of service cassandra status. The problem is that this command always exits with a bad return status of 3. I have successfully called service cassandra stop immediately prior to this command, and have even tried running status in a loop to see if there was some race condition (it seems to reliably fail with status 3 for a long time). However, running service cassandra status through the shell works. Any ideas what the issue may be, or even just how to debug this?

  private def isCassandraStopped(): Boolean = {
    val s = Seq("sudo", "sh", "-c", "service cassandra status").!!
    val r = " * Cassandra is not running" == s
    if (!r) logger.info(s"Cassandra is not stopped: #$s#")
    r
  }

This is the line that succeeds prior to executing the above method:

  Seq("sudo", "sh", "-c", "service cassandra stop").!
jonderry
  • 23,013
  • 32
  • 104
  • 171
  • Can you post the init.d script? is it hand made or came with a package on your system? Additionally run this from shell: `service cassandra status > /tmp/my.log` and `echo $?` right after and see if any output goes to screen at all and what was an exit status. – yǝsʞǝla Jul 24 '14 at 00:38
  • There's a lot in the file, but to the best of my knowledge, we didn't change anything in it from the standard cassandra package. I ran `echo $?` and it looks like it normally returns with status 3 when cassandra is not running, so I can just check the return status code and not do any string comparisons. – jonderry Jul 24 '14 at 00:50

1 Answers1

1

If you are reading process output with !! any non-zero exit status throws an exception by default as described in the docs. You can try to work around it and read the output as well as get the process exit status in ways described here.

By default exit status 3 should mean that the program is not running as described here. However, this might not be respected by your init script. So the best thing is to read the script and make sure that it returns the exit code 3 in that case.

So your options are:

  • read the exit code only if it's reliable
  • check how your run script is implemented and duplicate the process status check logic yourself - usually it checks that pid file exists and the process with that pid is running (checkpid).
  • read both output and exit status as described in other SO QAs
  • read all lines without throwing an exception with lines_!: Process("your command here").lines_!. You will not have an exit status in this case.

Usually exit status is more reliable in properly written shell scripts than output parsing.

Community
  • 1
  • 1
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65