Question

I have a few questions regarding an init script I'm writing. I haven't actually done it from scratch before so there are probably several tips and tricks I don't know about. Also, shell script is not something I have used a lot so the syntax took a bit of getting used to.

First of all I have a function to see if the application is running. The reason for this, instead of lockfiles or similar, is that I only want ONE instance and the application is a bit buggy and plugins can sometime linger even if the main application is killed.

So I have something like.

isrunning() {
  pids=`ps aux | grep -e 'FooBar' | grep -v grep | awk '{print $2}'`
  if [ ! -n "$pids" ]; then
    return 1
  else
    return 0
  fi
}

Is there a better way?

I start the main application with

daemon --user apache "./FooBar"

but that hung the script execution on that line until FooBar was terminated.

daemon --user apache "FooBar&"

works though. But it looks like the [OK] / [FAILED] that shows up when running the init script is actually output by the daemon function? So even if the execution fails, daemon still reports [OK] since the process of FooBar is detached.

Is there a way to get daemon NOT to get stuck without detaching the process? I have tried the -d option etc but no luck.

Otherwise I was thinking of just doing a sleep 3 and then check isrunning which works fine. But then I don't know how to print the [OK] / [FAILED] messages in the nice pretty way that init scripts normally do. Is there perhaps a function one can use to print this in the standard form?

UPDATE

I found the solution to printing the messages. It seems it actually are functions for this. success and failure will print the messages.

The daemon issue remain though. Any way to get rid of &?

UPDATE 2

I found a workaround and used

su -m -c "./FooBar&" apache

and checking if the application was running after that. Depending on the result I print the correct message.

Was it helpful?

Solution 2

I found the solution to printing the messages. It seems it actually are functions for this. success and failure will print the messages.

OTHER TIPS

The script you are using is the wrong answer to a FAQ. Try this simpler version instead:

isrunning () {
  pids=`ps aux | awk '/FooBar/{print $2}'`
  if [ ! -n "$pids" ]; then
    return 1
  else
    return 0
  fi
}

I'm very very tempted to set the exit code from within awk as well:

isrunning () { ps aux | awk '/FooBar/{print $2;exit 0}END{exit 1}'; }

If you are on Linux, perhaps you should be using pidof instead, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top