1

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() { servletToCheck="$@" if [ "$(ps -aux | grep \"$(whoami)\" | grep \"${servletToCheck}\" | grep -v \"grep\" | wc -l)" -eq 0 ]; then echo -n "Offline" else echo -n "Online" fi } 

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 else outWarning "Failed to start servlet: ${tbnServlet}" exit 3 fi 

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?

1 Answer 1

2

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" 
1
  • Apparently using pgrep fixed it. +1 Commented Dec 5, 2016 at 3:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.