1

In a bash script I have to find the running time of a proccess, I do it with this command:

PROCCESS_TIME=$(ps -eo pid,etimes | grep $(ps -ef | grep SCREEN | grep PROCCESS | head -1 | awk '{print $2}') | awk '{print $2}')

Which gives me running time in seconds,then I need to convert it to minutes

PROCCESS_TIME_MIN=$(( $PROCCESS_TIME / 60 ))

It works most of the time but sometimes give me this error and script exits

0 / 60 : syntax error in expression (error token is "0 / 60 ")

or:

1893 / 60 : syntax error in expression (error token is "1893 / 60 ")

Where is my problem, How to solve it?

0

2 Answers 2

2

You will get that error message in this situation:

PROCCESS_TIME="0
0"
PROCCESS_TIME_MIN=$(( $PROCCESS_TIME / 60 ))

Check

PROCCESS_TIME=$(ps -eo pid,etimes | grep something | awk '{print $2}')

This can return more than 1 result. You can add |head -1 in the command, or change the awk: awk '{print $2; exit}'.
When you change your variables to lowercase, correct the spelling of proccess_time and use braces, the command will look like

process_time=$(ps -eo pid,etimes | 
   grep $(ps -ef | grep SCREEN | grep PROCCESS | awk '{print $2;exit}') |
   awk '{print $2; exit}')

I would try to change this command so that you would need 1 ps and 1 awk, but I have a different ps command (without -o etimes but with -o etime, returning time as hh:mm:ss).
Also try to use grep -E "SCREEN.*PROCCESS" or grep -E "PROCCESS.*SCREEN"

Sign up to request clarification or add additional context in comments.

3 Comments

I thought PROCCESS_TIME could return multiple values, and I already have the " | head -1" in it if you check. will try with exit and see how it goes. I need etimes because in next commands I have: if [[ $PROCESS_TIME -ge $MINIMUM_TIME ]] ;then ....
You had 2 grep commands, and only one of those (that is used as a grep-argument) with a head -1.
Missed the 2nd grep, added next | head -1 and it looks like its solved. Thanks
0

Try using a variable for 60(I am simply giving it name as sixty, you could change it as per your need too) and try following then:

sixty=60
var=$((PROCCESS_TIME / sixty))

1 Comment

Then you have to either show us the complete code or use set -x in starting of your script to get the traces and let us know on same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.