开发者

logging unix "cp" (copy) command response

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

eg:

>cp -R bin/*.ksh ../backup/开发者_StackOverflow中文版

>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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜