I get a log file as soon as I run one command(process is still running in background). Now I want to fetch the status (clean or un-clean) from that log file. If status is clean then I will leave the process as is else, if it is un-clean then I have to kill the process which was started by my first command and re-run the same command again.

I have tried with cat logfilename | grep "un-clean". But I don't know how to verify this output in shell script.

I want something like (roughly) var= clean then the output of above command == var if yes then echo "ok" else re-run command

I have tried some of the commands but not working for me.


Basically, you want this structure

if grep -q "un-clean"  /path/to/log_file.log ;
    # put some command in case we find result is unclean
    # if the output is ok, do something else

All it does is to silently (without printing to screen) check if there is a match of string "unclean" in the file . If there is we do the if part, otherwise - we do else part.

Here's an example:

$> if grep -q 'root' /etc/passwd ; then  echo "This user exists" ; else echo "This user doesn't exist"; fi    
This user exists
$> if grep -q 'NOEXIST' /etc/passwd ; then  echo "This user exists" ; else echo "This user doesn't exist"; fi 
This user doesn't exist

What also can be done is to start command you want from script, but in background. That way we can have it's PID. Here's what I mean

$> echo "HelloWorld"   &                                                                                      
[1] 6876

Adding & causes echo "HelloWorld" run in background, and we have it's PID stored in $! variable. Thus, we can do something like,

some-command  &
if grep -q "un-clean"  /path/to/log_file.log ;
         kill -TERM $CMD_PID
        # if the output is ok, do something else
  • thanks a lot :) its working....finally....was waiting since long – Siddharth Tiwari Jul 19 '16 at 13:51
  • @SiddharthTiwari glad I could help. If this post has solved your question, please mark it as accepted ( there's a gray check-mark next to up-down arrows ) – Sergiy Kolodyazhnyy Jul 19 '16 at 13:52
  • i dint get that kill process part.. i would be appreciated if you help me in detail... i am sorry i am very new to this.. just working from past two days – Siddharth Tiwari Jul 19 '16 at 14:05
  • @SiddharthTiwari basically , if you run command with & , you can save it's process ID . And if you have process ID, you can kill that process. -TERM just sends SIGTERM signal to that command. That signal just allows for nicer closing of command instead of just abruptly stopping it – Sergiy Kolodyazhnyy Jul 19 '16 at 14:09
  • For example, if I have script and run firefox & then FOXPID=$! I can start firefox, remember its PID, and then continue running other commands. Then later I can use that PID to kill firefox – Sergiy Kolodyazhnyy Jul 19 '16 at 14:11

Did you try something like that:

some-command  &
wait $PID
while grep -q un-clean filename.log ; do
  kill -TERM $PID
  some-command  &
  wait $PID

the process will be killed (see killall: man killall) and re-run command will be run while the file filename.log contains the string un-clean.

Does it help?

EDIT: post updated using the good @Serg solution and added the wait command to wait for the end of the command before checking the log file.

  • yes it is but its getting into infinite loop. any suggestions for that? i have 4 more process running in background so will kill process kill those as well? how to use kill process command?? directly shall i write kill process – Siddharth Tiwari Jul 19 '16 at 14:01
  • Maybe you will have to insert a sleep 5 after the re-run command to wait 5 seconds after launching the command to wait the execution before searching for the result? But if you only want to retry one time, the @Serg solution is better. – yaap Jul 19 '16 at 14:04
  • no i want to have a loop cz it may show un-clean for 4 - 5 times in a row so loop is better but its getting into infinite loop – Siddharth Tiwari Jul 19 '16 at 14:07
  • updated my post to avoid infinite loop (if the command only show un-clean for 4 - 5 times) – yaap Jul 19 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.