Question

I am writing an init.d script and am looking to test if a returned value is greater than 1. What would be the correct syntax for 'greater than'?

mc_status() {
        if ps ax | grep -ci 'CanaryMod.jar' > 0
        then
                echo "$SERVICE is running."
        else
                echo "$SERVICE is not running."
        fi
}
Was it helpful?

Solution

From my recollection init scripts are written in the sh shell. Many shell scripts use a pid file (usually found in /var/run) to check if a service is running. In your case a process as found in ps is used for validation.

The test you are performing is somewhat incorrect, as ps/grep are returning not a number but a set of characters or nothing. Try the following:

mc_status() {
        if [ ! -z "`ps ax | grep -ci 'CanaryMod.jar'`" ]; then
                echo "$SERVICE is running."
        else
                echo "$SERVICE is not running."
        fi
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top