开发者

Output bash script into file (without >> )

Perhaps this is very simple thing, but yet I dont know it. I have a job specified in cron, which calls a script once in a while.

something like that: 1 1 * * * bash script.sh

Now, when script launched it reads configuration and realises that all output needs to be appended to log file.

The simple solution would be to update cron task like 1 1 * * * bash script.sh >> log But let assume that name and location of the file is not known at this stage.

So I came up with the solution which works, but I dont like it a lot. Code at the top of the script:

source config.sh

N=$#
LAST_ARG=${!N}
if [ "$LAST_ARG" != "log" ]
then
  bash \`basename $0\` \`echo $@ log\` >>  "$LogFile"
  exit
fi

I recursively launching the same sc开发者_如何学Cript, with additional argument, and direction to log file. Then Parent instance just getting closed.

Sure there has to be a better way to do it ?

Thanks


exec 1>/the/file/name.log

This will redirect stdout to /the/file/name.log

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜