LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   display message "file is running" while file is executing (https://www.linuxquestions.org/questions/programming-9/display-message-file-is-running-while-file-is-executing-4175469668/)

veda92 07-15-2013 05:14 AM

display message "file is running" while file is executing
 
suppose there is a file which fgrep from long log file.
now while the file is running i wish to display message that the file is still running
script i used is:
ps ax|grep ./file >output
if [$? -eq 0]
then
echo "process is running"
else
echo "result is avaable"
fi
well the above script does not display msg on output file but on terminal also "process is running" is displayed even after execution

grail 07-15-2013 05:34 AM

Firstly, please use [code][/code] tags when displaying code.

As for the code you have provided, $? is the return code of the previous command, so until it 'returns' the 'if' will not be executed.

Try instead to place the command in question in the background and capture its PID. Then you can test if the PID is still active and choose which message you wish to display.

Firerat 07-15-2013 06:02 AM

as grail suggests..

if you are using bash version 4 or greater you can make use of coproc, which has a handy environment var. for the pid

I think by default it is $COPROC_PID , but you can label it yourself ( search web for "bash coproc" to get more detail )

something like this

Code:

#!/bin/bash
coproc { ps ax|grep ./file >output }
printf "process $COPROC_PID is running"
while [ $COPROC_PID ];do
  printf "."
  sleep 1
done

a "." will be added every second, which is a little cleaner than having the same line printed over and over.

ta0kira 07-16-2013 08:19 AM

pgrep would be a better way to search. If you're searching for a script, use the -f option because the script name won't be the process name. You should also account for the more "ugly" states you could end up in, like waiting for a process that's defunct.
Code:

while :; do
  pid=$(pgrep -f "./file" | head -1)
  [ -n "$pid" ] || break
  ps -o state= "$pid" | egrep -vq '[ZX]' || break
  sleep 1
done

Kevin Barry

David the H. 07-19-2013 04:39 AM

I don't quite understand what "the file is still running" means? Files don't run, processes do.

Do you mean that you want to detect whether a process that's writing to, or reading from, the log file is still running? Or whether some process listed in the log file, as found by grep, is still running? Or what?

Please try to explain what you are doing in more detail. Tell us your ultimate purpose, not just the immediate goal.

Firerat 07-19-2013 01:21 PM

Yeah, a little miss-match with Subject and the content
the message echo'd is "correct" in the OP
Code:

echo "process is running"

Actually. yes I see the confusion
I think ( might be wrong ) they are looking for any process whose full path begins "./file"

So the below is nonsense , since ps ax|grep is going to be over quite quickly

So first they need to find the PID , and then check it is still running

Just for fun ( with printf, as per above it is a nonsense ),

Code:

#!/bin/bash
coproc { ps ax|grep ./file >output }
printf "process $COPROC_PID is running\r"
C=1
while [ $COPROC_PID ];do
  case $C in
      1) printf "process $COPROC_PID is running |\r";;
      2) printf "process $COPROC_PID is running /\r";;
      3) printf "process $COPROC_PID is running –\r";;
      4) printf "process $COPROC_PID is running \\\\\r"
          C=0;;
  esac
  C=$(( $C + 1 ))
  sleep 1
done

apologies for case 4, bit of a mess with the escaping

Open for better ways to do the above ( I often get stuck in the "but it works" rut )

Firerat 07-19-2013 02:00 PM

re-written, using top as example

Code:

Process=top
PID=$(pidof $Process)

C=1
while [ "$(pidof $Process >/dev/null;echo $?)" == "0" ];do
  case $C in
      1) printf "process $PID ($Process) is running |\r";;
      2) printf "process $PID ($Process) is running /\r";;
      3) printf "process $PID ($Process) is running –\r";;
      4) printf "process $PID ($Process) is running \\\\\r"
          C=0;;
  esac
  C=$(( $C + 1 ))
  sleep 1
done

But it gets messy if more than one top running

If the script is starting the process, and you want to give some feedback, then coproc is the way forward.
instead of the nonsense I had early ( the ps | grep is short lived ),
have your actual process ( ./file<whatever> )

depending on the nature, you can print progress

for example , some time ago I wrote a script to send files to my Android Phone over adb

so.. my coproc was

Code:

coproc { adb push /path/to/somefile /sdcard/ }

Size=$(stat -c %s /path/to/somefile)
while [ $COPROC_PID ];do
  SentSize=$(adb shell stat -c %s /sdcard/somefile)
  .. some awk stuff to work out % complete and print progress bar ..
done

not got the script to hand, but you get the idea.


All times are GMT -5. The time now is 07:01 AM.