Improvements to this bash script to simulate “tail --follow”

回眸只為那壹抹淺笑 提交于 2019-12-05 19:34:09

For sleeping in microseconds, you can use

perl -MTime::HiRes -e "Time::HiRes::usleep(1)" ; 
perl -MTime::HiRes -e "Time::HiRes::sleep(0.001)" ;
peterh

Unfortunately, none of the tail versions on the remote OS (Solaris) support the --follow option

That's a little harsh.

Just use -f (rather than --follow) on both Solaris and Linux. On Linux you can use --follow as a synonym for -f. On Solaris you can't.

But anyway, to be more precise: you want a follow option that handles rollovers. GNU tail (i.e. Linux) has that natively by the way of the -F (capital F) option. Solaris doesn't. The GNU tail -F option can handle that the file is rolled over as long as it keeps the name name. In other words on Solaris you would have to use gtail command to force the use of GNU tail.

If you are a prudent Solaris site then such GNU tool would just be there, without you having to worry about it. You shouldn't accept a Solaris install from your SysAdmin where he/she has deliberately neglected to make sure the basic GNU tools are there. On Solaris 11 (as an example) he really has to go out of his way to make that happen.

You would make your script OS independent by the well known method:

TAILCMD="tail"

# We need GNU tail, not any other implementation of 'tail'
if [ "$(uname -s)" == "SunOS" ]; then
  TAILCMD="gtail"
fi

$TAILCMD -F myfile.log
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!