How to monitor a command inside shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to monitor a command inside shell script
# 1  
Old 01-25-2011
How to monitor a command inside shell script

Hi All,

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.

Code:
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.


Thanks in advance.
# 2  
Old 01-25-2011
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.

Code:
#!/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

Have fun!
This User Gave Thanks to agama For This Post:
# 3  
Old 01-26-2011
Or only waiting subprocess ending, very cpu friendly.
Code:
#!/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

This User Gave Thanks to kshji For This Post:
# 4  
Old 01-27-2011
Thanks a lot guyz.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sql command inside shell script runs without giving anything back as outout

#!/bin/sh # This script returns the number of rows updated from a function echo "The execution is starting ....." sqlplus -silent $UP <<EOF set serveroutput on set echo off set pagesize 0 VAR no_rows_updated NUMBER; EXEC :no_rows_updated :=0; DECLARE CURSOR c_update is SELECT * FROM... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. Shell Programming and Scripting

Can i use if else inside expect command in shell script?

hii,, I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this: "Configure which ports JIRA will use. JIRA requires two TCP ports that are not being used by any other applications on this machine. The HTTP port is where you... (1 Reply)
Discussion started by: nithinfluent
1 Replies

3. Shell Programming and Scripting

Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr

Hello, Recently, I've started with shell scripting, and decided to write a script for my system backup using tar. When I was dealing with tar execution inside shell script I found this, inside shell we have the following code: tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_STARTwith... (6 Replies)
Discussion started by: ilnar
6 Replies

4. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

5. Shell Programming and Scripting

Error while using sqlplus command inside 'if' condition in an unix shell script

Hi all, I am using the below given sqlplus command in my unix script to invoke a stored procedure which returns a value .It works fine. RET_CODE=$(/opt/oracle/product/10.2.0.4.CL/bin/sqlplus -S $USER/$PASSWD@$DB_NAME <<EOF EXEC MY_PKG.MY_SP (:COUNT); PRINT COUNT; commit; ... (6 Replies)
Discussion started by: Shri123
6 Replies

6. Shell Programming and Scripting

C Shell - Command Inside a Loop

I have a command nested in some while loops to parse some data that looks something like this. while ($condition) while ($condition) ... gzcat /dir/$fileName.gz | grep $searchString > out_file end end On the first loop, the command is executed properly (and takes maybe 10... (3 Replies)
Discussion started by: hobbers
3 Replies

7. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

8. UNIX for Dummies Questions & Answers

how to get the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (1 Reply)
Discussion started by: kripssmart
1 Replies

9. Shell Programming and Scripting

Using cp command inside shell scrip

Hi, First i would like to say that im a unix begginer. I have a file named /tmp/sample.lst that contain about 20 rows like the following two : '/tmp/aa.txt' '/temp/aa.txt' '/tmp/xx.txt' '/temp/xx.txt' Inside a ksh script i would like to do the following task: add the cp command at... (2 Replies)
Discussion started by: yoavbe
2 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question