Bash管道输出和程序输入 [英] Bash piping output and input of a program

查看:151
本文介绍了Bash管道输出和程序输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个独立的屏幕会话中的Linux机器上运行一个minecraft服务器.我不太喜欢屏幕,并且希望能够不断将服务器的输出通过管道传输到文件(如管道),并将一些输入从文件通过管道传输到服务器(以便我可以输入和输出)从远程程序(例如python脚本)连接到服务器).我对bash不太有经验,所以有人可以告诉我该怎么做吗?

I'm running a minecraft server on my linux box in a detached screen session. I'm not very fond of screen very much and would like to be able to constantly pipe the output of the server to a file(like a pipe) and pipe some input from a file to the server(so that I can input and output to the server from remote programs, like a python script). I'm not very experienced in bash, so could somebody tell me how to do this?

谢谢,尼基塔·乌蒂(NikitaUtiu)

Thanks, NikitaUtiu.

推荐答案

目前尚不清楚您是否需要屏幕.我不知道Minecraft服务器,但是通常对于服务器软件,您可以从crontab条目运行它,然后将输出重定向到日志文件.

It's not clear if you need screen at all. I don't know the minecraft server, but generally for server software, you can run it from a crontab entry and redirect output to log files.

假设您的服务器在星期天的午夜杀死自己(如果每周重新启动1x太少或太多,或者您需要临时重新启动,我们可以讨论更改此方法),但是对于执行该操作的基本想法,请在这里是一个crontab条目,该条目在每个星期一的午夜1点后启动服务器.

Assuming your server kills itself at midnight sunday night, (we can discuss changing this if restarting 1x per week is too little or too much OR you require ad-hoc restarts), but for a basic idea of what to do, here is a crontab entry that starts the server each monday at 1 minute after midnight.

01 00 * * 1 dtTm=`/bin/date +\%Y\%m\%d.\%H\%M\%S`; export dtTm; {  /usr/bin/mineserver -o ..... your_options_to_run_mineserver_here ... ; } > /tmp/mineserver_trace_log.${dtTm} 2>&1

请向您的手册页咨询crontab,以确认星期几的范围是0-6(0 =星期日),如果星期几是0!= Sunday,则更改星期几的值.

consult your man page for crontab to confirm that day-of-week ranges are 0-6 (0=Sunday), and change the day-of-week value if 0!=Sunday.

通常,我会将代码分解以便于阅读,但对于crontab条目,每个条目必须全部位于一行上(有一些怪异的例外),并且通常限制为1024b-8K行.请注意,;"就在结束'}'之前是非常关键的.如果不这样做,您将收到无法解码的错误消息,或者根本没有错误消息.

Normally I would break the code up so it is easier to read, but for crontab entries, each entry has to be all on one line, (with some weird exceptions) AND usually a limit of 1024b-8K to how long the line can be. Note that the ';' just before the closing '}' is super-critical. If this is left out, you'll get un-deciperable error messages, or no error messages at all.

基本上,您会将任何输出重定向到文件(包括std-err输出).现在,您可以对输出执行很多操作,使用moreless查看文件,使用grep ERR $ {logFile},编写用于grep查找错误消息的脚本,然后向您发送发现错误的电子邮件等等.

Basically, you're redirecting any output into a file (including std-err output). Now you can do a lot of stuff with the output, use more or less to look at the file, grep ERR ${logFile}, write scripts that grep for error messages and then send you emails that errors have been found, etc, etc.

您可能需要一些sys-admin工作才能获得mineserver用户,以便它可以运行crontab条目.另外,如果您不习惯使用vi或emacs编辑器,则创建crontab文件可能需要其他人的帮助.发布到superuser.com以获得有关Linux管理员问题的答案.

You may have some sys-admin work on your hands to get the mineserver user so it can run crontab entries. Also if you're not comfortable using the vi or emacs editors, creating a crontab file may require help from others. Post to superuser.com to get answers for problems you have with linux admin issues.

最后,关于两点过时的日志文件,我想提出两点.

Finally, there are two points I'd like to make about dated logfiles.

  1. 好:如果您的应用程序死了,则无需重新运行它即可捕获输出并弄清为什么某些东西停止工作了.对于长时间运行的程序,这可以节省很多时间. b.保留陈旧的文件使您能够向您,您的老板和其他人证明它过去工作正常,请参阅此处的日志文件. C.假设其中包含有用的信息,保留日志文件将使您有机会挖掘这些文件来了解事实. IE. :程序过去需要1秒钟进行处理,现在需要1个小时,依此类推.

  1. Good: a. If you app dies, you never have to rerun it to then capture output and figure out why something has stopped working. For long running programs this can save you a lot of time. b. keeping dated files gives you the ability to prove to you, your boss, others, that It used to work just fine, see here are the log files. c. Keeping the log files, assuming there is useful information in them, gives you the opportunity to mine those files for facts. I.E. : program used to take 1 sec for processing, now it is taking 1 hr, etc etc.

错误:a.您将需要建立一种机制来清除旧的日志文件,否则在某些时候一切都将停止,并且当您最终弄清楚问题出在哪里时,您会发现/tmp或选择使用的任何目录都完全是IS满的.

Bad: a. You'll need to set up a mechanism to sweep old log files, otherwise at some point everything will have stopped, AND when you finally figure out what the problem was, you discover that your /tmp OR whatever dir you chose to use IS completely full.

有一个自我维护的解决方案,可以在日志文件上使用日期,我可以告诉您有关此方法是否有用的信息.这将需要一些解释,因此如果您不觉得crontab解决方案有用,那么我不想花时间编写它.

There is a self-maintaining solution to using dates on the logfiles I can tell you about if you find this approach useful. It will take a little explaining, so I don't want to spend the time writing it up if you don't find the crontab solution useful.

我希望这会有所帮助!

这篇关于Bash管道输出和程序输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆