2

I'm new to UNIX-Solaris, and I got to make a script and I'm having an issue with a value I require.

Basically I need to detect if two process are running. I find out I can get that info using the following command

ps -fu "user" | grep -i "name_oftheprocess/file"

When I executed it, I get this info.

user   16544 16530   0 08:00:00 ?           0:08 /bin/bash /path/file

user   19098 16544   0 15:44:38 ?           0:00 /bin/bash /path/file

So everything is fine here. The thing is I got to compare the 2nd value (16544) from line 1, with the 3rd value (16544) of the 2nd line. If they match then a message should be sent stating everything is ok; if they don't, something is wrong.

I'm trying to find how with awk I can get those two values separated. If I use

ps -fu "user" | grep -i "name_oftheprocess/file" | awk '{print $2}' 

it gives me the result

16544   19098  

(this last one I don't need it)

ps -fu "user" | grep -i "name_oftheprocess/file" | awk '{print $2}' 

it gives me the result

16530   16544  (this fist one I don't need it)

So I want those two values to be stored each one in a separated variable and later compared it with an if sentence.

4
  • 2
    You may want to check out pgrep and its various flags. – Wildcard Feb 21 '17 at 21:25
  • Also, does Solaris have /proc? (It may be Linux-specific.) – Wildcard Feb 21 '17 at 21:25
  • 2
    @Wildcard /proc in itself is not Linux specific (a similar hierarchy is available in at least Solaris 11.3), but what Linux puts in it is very Linux specific. – Kusalananda Feb 21 '17 at 21:40
  • @Wildcard Linux took /proc from Solaris. – Gilles 'SO- stop being evil' Feb 21 '17 at 23:51
0

Pipe the info you get into this AWK:

awk 'NR==1{a=$2}NR==3{print a==$3?"everything is ok":"something is wrong"}'

The complete command in your case would be:

ps -fu "user" | grep -i "name_oftheprocess/file" | awk 'NR==1{a=$2}NR==3{print a==$3?"everything is ok":"something is wrong"}'

Note:

This might not be robust! This answer takes your posted text as input, if you provide more details about what you want to do and include the output of the first command, it might be possible to make further considerations and find a more appropriate solution.

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.