Sleep Command

Hello,

Need a little help with the script below. I can't get it to sleep :frowning: I'm trying to get this to check if the process is running and if it is, wait 10 secs and check again. Keep doing this until it's not running and then stop checking and send the email.

#!/bin/ksh
mailto=`cat maillist.txt`
isrunning=`ps -ef | grep -v grep | grep execute | wc -l`
 if [ $isrunning -gt 0 ]
   then
     echo "is still running"
        sleep 10
   else
     echo "is not running. Sending last 20 lines of log file."
     tail -20 /log.log > log.txt
    cat log.txt | mailx -s "Log" $mailto
 fi
exit

That's a useless use of cat.

You don't need to count how many lines grep outputs to tell whether it matches anything or not: grep tells you directly, returning zero if it found anything, nonzero if it didn't, letting you plug it directly into an if or other logical statement.

Try

while true
do
        if ! ps -ef | grep '[e]xecute' > /dev/null
        then
                tail -20 /log.log | mailx -s "Log" $mailto
                break
        fi

        sleep 10
done

The '[e]xecute' avoids needing grep -v grep, since the regular expression '[e]xecute' will still match the string "execute" but won't match the string "[e]xecute".

Thanks for this. I did this when I was trying to find a quick solution to a problem we had. Didn't really know UNIX at the time (although I'm still learning). Didn't know about the '[e]' trick either.

Will give it a try.