Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data.
tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*'
I want to put some notification/alerts for this command like time taken ,space consumed by tar etc. in the same script. Please let me know if there is any way to do this.
This should get you started. I avoid bash, and have marked the one statement that I know you'll need to change if you wish to use bash over Ksh.
#!/usr/bin/env ksh
# function that does work to be monitored. when it is finished
# it deletes the marker file passed as $1
function runme
{
# tar command would go here
# some dummy work for easy testing instead of tar command
typeset i=0
while (( $i < 50 ))
do
ps -elf >> /tmp/out
i=$(( $i + 1 ))
sleep 1
done
# end of dummy work
rm $1 # remove marker to indicate done
}
# function to monitor and report every few seconds to the tty
function watch
{
printf "started watching: %s\n" "$(date)"
while [[ -f $1 ]] # keep watching until runme removes the marker ($1)
do
# replace /tmp/out with your filename, or pass filename in (better)
ls -hl /tmp/out | read junk junk junk junk size junk # WARNING: not bash friendly
printf "%s: cur size: %-10s\r" "$(date)" "$size"
sleep 2
done
printf "\n"
}
marker=/tmp/marker # watch function loops until this is gone
>$marker # create it
runme $marker & # start function to do work
watch $marker # update progress until done
Or only waiting subprocess ending, very cpu friendly.
#!/bin/ksh or bash or dash or ...
someecho()
{
while true
do
somestring=$(date '+%H%M%S')
printf "\r%s \r" $somestring
sleep 2
done
}
somesubprocess()
{
# tar or ... sleep is nice test command ...
sleep 20
}
#
somesubprocess & # start process
childPID=$! # just startrd process id
someecho & # some echo process
echoPID=$!
echo "child:$childPID"
wait $childPID # and then we wait process ending
kill $echoPID # and kill echo process
echo
echo done