awk if condition help

Hi, I need little help with awk's if condition statement. I have following code:

$ ssh myRemotehost 'ps ww -fu tomcat ' | awk ' { if ($1 == "tomcat") print "tomcat (pid " $2 ") is running... "; else print "tomcat stopped or dead" }'

Prints: 

tomcat stopped or dead
tomcat (pid 12345) is running...

if I do the below command, it prints correctly :

$ ssh myRemotehost 'ps ww -fu tomcat ' | awk ' /tomcat/ { if ($1 == "tomcat") print "tomcat (pid " $2 ") is running... "; else print "tomcat stopped or dead" }'

Prints: 
tomcat (pid 12345) is running...

But the problem is when the tomcat is not detected by the ps command, i do not see any output where in I would like to see that "tomcat stopped or dead"

Can someone please help me refine this.

Thanks in advance.

Put something that runs in the END { } block to print when it's dead or alive. Remember whether anything was found with a variable.

You've got quoting problems happening -- not just single quotes being needed for pipes, but that $1 is probably being interpreted as a shell variable somewhere along the line; the single quotes vanish once it gets to the other side of ssh!

When quoting starts getting that complicated I'd use a here-document and avoid quoting entirely. You can feed entire unmodified shell statement blocks into ssh raw that way, with no special quoting or escaping needed.

ssh remotehost exec /bin/sh -s <<"EOF"
ps ww -fu tomcat |
        awk '
        # Run this code block whenever $1 == "tomcat"
        $1 == "tomcat" { TOMCAT=$2 }
        END {
                if(TOMCAT) printf("tomcat(%s) is alive\n", TOMCAT);
                else            printf("tomcat stopped or dead\n"); }'
# The EOF here MUST be at the beginning of the line
EOF

What is the output of

ssh myRemotehost 'ps ww -fu tomcat ' 
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
tomcat    3613     1 18 Mar28 ?        Sl   552:53 /usr/bin/java -Djava.util.logging.config.file=/app/apache-tomcat/conf/logging.properties -XX:MaxPermSize=256M -Xms1024m -Xmx1024m -server -javaagent:/app/apache-tomcat/lib/aspectjweaver.jar -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/app/apache-tomcat/endorsed -classpath :/app/apache-tomcat/bin/bootstrap.jar -Dcatalina.base=/app/apache-tomcat -Dcatalina.home=/app/apache-tomcat -Djava.io.tmpdir=/app/apache-tomcat/temp org.apache.catalina.startup.Bootstrap start

---------- Post updated at 04:58 PM ---------- Previous update was at 04:46 PM ----------

Hi Corona688,

what you proposed looks good but when i plug that in my script i get error: "syntax error: unexpected end of file". Not sure how it works

Is that EOF at the beginning of the line? It MUST BE at the beginning of the line. If it's not, it won't consider it the end of the here-document, and will eat the entire file then complain about EOF.

When I copy-paste that code into my own shell, it works fine except I don't have a topcat user so ps complains...

Alternatively try:

ssh myRemotehost 'ps ww -fu tomcat' | awk '/[o]rg.apache.catalina.startup.Bootstrap/{pid=$2} END{if (pid) print "tomcat (pid " pid ") is running..."; else { print "tomcat stopped or dead"; exit 1 }}'

The [o] is not really necessary, since the ps is run on the remote host and awk on the local host, but then you could reuse the code to check on the local host too... I added an exit statement so you can also check the return code..
The check needs to be in the END part like Corona688 suggested, otherwise you cannot conclude that it is not running.

Thanks both of you for the input / suggestions. It was really helpful. :slight_smile: