LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2013, 05:14 AM   #1
veda92
LQ Newbie
 
Registered: Jul 2013
Posts: 8

Rep: Reputation: Disabled
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

Last edited by veda92; 07-15-2013 at 05:46 AM.
 
Old 07-15-2013, 05:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 07-15-2013, 06:02 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.
 
Old 07-16-2013, 08:19 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 07-19-2013, 04:39 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-19-2013, 01:21 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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 )

Last edited by Firerat; 07-19-2013 at 01:36 PM.
 
Old 07-19-2013, 02:00 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] "Unexpected End of File" message on LXTerminal while trying to unzip a .tar.gz file pioneermtb Debian 2 12-22-2012 07:55 PM
how to avoid "file system full" error message while copying/ ftping a file? laginagesh Linux - Software 4 01-27-2012 03:03 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Downloaded rar file, tried to open with 7Zip, getting "file is broken" message? Ejfel Linux - Software 7 03-02-2009 12:51 AM
a bash file executing only "ls -l" doesn't produce colorful listing sirbijan Linux - General 2 08-06-2006 06:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration