I am writing a Bash script that is supposed to run other daemonized Bash scripts, called servlets.

The function I am inquiring about is getStatus:

getStatus() {
     if [ "$(ps -aux | grep \"$(whoami)\" | grep \"${servletToCheck}\" | grep -v \"grep\" | wc -l)" -eq 0 ]; then
        echo -n "Offline"
        echo -n "Online"

What it should be doing is checking if the arguments are a running process owned by the user, and output Online if it is, Offline otherwise.
Now, this function seems to be working perfectly. The problem seems to be with how I'm parsing it:

if [ "$(getStatus ${tbnServlet})" == "Online" ]; then
    outSuccess "Started servlet: ${tbnServlet}"
    exit 0
    outWarning "Failed to start servlet: ${tbnServlet}"
    exit 3

Strangely, even when the servlet is up and running, it says it Failed to start/stop servlet, and running a function that lists the servlets lists all of them as offline, even when they aren't.

What is the issue, and how can I correct it?


Your code is too complex for this basic job...

pgrep -u $USER "$tbnServlet" >/dev/null && echo "Started servlet: $tbnServlet" || echo "Failed to start servlet: $tbnServlet"
  • Apparently using pgrep fixed it. +1 – InitializeSahib Dec 5 '16 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.