display message "file is running" while file is executing
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.