Mohamed Saligh
Mohamed Saligh

Reputation: 12349

logging unix "cp" (copy) command response

I am coping some file,So, the result can be either way.

eg:

>cp -R bin/*.ksh ../backup/

>cp bin/file.sh ../backup/bin/

When I execute above commands, its getting copied. No response from the system, if it copied successful. If not, prints the error or response in terminal itself cp: file.sh: No such file or directory.

Now, I want to log the error message, or if it successful I want to log my custom message to a file. How can I do?

Any help indeed.

Thanks

Upvotes: 0

Views: 6178

Answers (1)

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

try writing this in a shell script:

#these three lines are to check if script is already running.
#got this from some site don't remember :(

ME=`basename "$0"`;
LCK="./${ME}.LCK";
exec 8>$LCK;

LOGFILE=~/mycp.log

if flock -n -x 8; then

    # 2>&1 will redirect any error or other output to $LOGFILE

    cp -R bin/*.ksh ../backup/ >> $LOGFILE 2>&1

    # $? is shell variable that contains outcome of last ran command
    # cp will return 0 if there was no error
    if [$? -eq 0]; then
       echo 'copied succesfully' >> $LOGFILE
    fi
fi

Upvotes: 2

Related Questions