linux脚本取前一天时间戳,linux – 为脚本日志条目添加时间戳

目前,我使用exec 22>& 2 21>& 1 1> $log 2>& 1将我的bash脚本写入stderr和stdout到日志文件.我想为每个条目添加一个时间戳,但我还没有找到一个简单的方法来做到这一点.理想情况下,对当前命令进行简单的更改,然后在其余部分之前将时间写在同一行上.

这是我正在使用命令的脚本:

#!/bin/bash

#This script takes the server to rysnc as an argument. You can also tell

#the script to check the server_status.txt file.

#

#Example: /path/to/script/sync.sh grail true

#

#The arguments are order senstive. The server name must come before the status

#check value.

#Logfile

LOG=/var/log/sync.log

DIRECTORYS="auth/ keys/ log/mailwhen/ intranet/ www/calmaa/data/ www/admatch/data/ www/sfhsa/data/ www/hfa3_org www/padmatch/ www/serverdown/"

if [ "x$2" == "xfalse" ]; then

return 0

elif [ "x$2" == "xtrue" ]; then

if [ `cat /srv/www/wan*/server_status.txt` == "primary" ]; then

exit 0

fi

else

echo "Please use \"true\" or \"false\" for the second value."

exit 1

fi

# Copy stdout and stderr,and then open the logfile

exec 22>&2 21>&1 1>$log 2>&1

# Here is how to restore stdout and stderr:

# exec 2>&22 1>&21

for DIRECTORY in $DIRECTORYS; do

rsync -azu --delete --bwlimit=500 $1:/srv/$DIRECTORY /srv/$DIRECTORY

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值