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. |
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 |
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 |
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. |
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 Open for better ways to do the above ( I often get stuck in the "but it works" rut ) |
re-written, using top as example
Code:
Process=top 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/ } |
All times are GMT -5. The time now is 07:01 AM. |