How to write search a word in file and to make it monitor like running it continuously

how to write search a word in file and to make it moniter like runnining it continously

#!/bin/bash


result=$(ps -eaf | grep -i "international")


if [ -n "$result" ]; then

    echo "$result" | mail -s "Found 'international' in processes"  xxxmail@mail.com
fi

Hello,

Welcome to the forum ! We hope you find this to be a friendly and helpful place, and that you enjoy your time here.

There are a few ways you could go about it, but the most straightforward would be to include your code as part of a while loop in Bash, so that the test will be repeated indefinitely.

So something like this, for example, might do what you're after:

#!/bin/bash

while result=$(/bin/ps wwauxf | /usr/bin/grep -i international)
do
        if [ -n "$result" ]
        then
                echo "$result" | /usr/bin/mail -s "Found international in processes" address@domain.tld
        fi

        /usr/bin/sleep 1
done

This would repeat your command over and over in an infinite loop, with a one-second delay between iterations of the loop (the effect of the sleep 1 command - you could adjust that as per your requirements).

Hope this helps ! If this doesn't quite work for whatever reason, or if you have any follow-up questions, please let us know and we can take things from there.

This checks the exit status of the grep command; if not successful then the loop ends.

The following runs endlessly regardless of the exit status:

#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin
while :
do
  result=$(ps -eaf | grep -i "[i]nternational")
  if [ -n "$result" ]; then
    echo "$result" | mail -s "Found 'international' in processes"  xxxmail@mail.com
  fi
  sleep 1
done

EDIT: set PATH, to not rely on environment. And the [i] trick prevents from finding grep arguments in the ps arguments.

Sending an email every second :slight_smile: (as long as any process is there, and it most likely is*)
Btw, why wouldn't you use pgrep instead of ps ... | grep ...?
*Also, when using it like this, isn't grep always guaranteed to find at least one process in ps output, i.e. itself? This does not necessarily guarantee, that result contains what you're looking for.

$ ps -eaf | grep -i 'international'
theia       234     80  0 01:43 pts/0    00:00:00 grep -i international
$ result=$(ps -eaf | grep -i 'international')
$ echo "$result"
theia       237    235  0 01:43 pts/0    00:00:00 grep -i international
$

Woudln't result=$(ps -eaf | grep -i 'international' | grep -v 'grep') be better? (if you must use grep instead of pgrep)

Im getting error like root: command not found when i run ur code. Please check

@salman4370 Since we don't know your system, and know nothing about the environment you're running the script in ...
Have you copied the code (into your script) exactly, as it was posted here?
Did you make sure that /bin/ps , /usr/bin/grep, /usr/bin/mail, /usr/bin/sleep binaries exist in your system (and are visible in PATH of script's execution environment)?
Check it before running the actual script. And if any of the commands is not there, please make sure you understand how to correct the code to fit your environment. When in doubt, present the complete code of your script, complete method of how you executed it, and complete output/error message you received.

I made two improvements to my previous post (see the EDIT comments).
Of course
if pgrep -if 'international' >/dev/null
is more efficient (if your OS supports it).
And yes, receiving Email every second is certainly not a convenient solution.