想要运行命令列表,但能够在运行时编辑列表 [英] Want to run a list of commands, but be able to edit the list while it's running

查看:60
本文介绍了想要运行命令列表,但能够在运行时编辑列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要运行的(bash)命令列表:

I have a list of (bash) commands I want to run:

<Command 1>
<Command 2>
...
<Command n>

每个命令需要很长时间才能运行,有时在看到(例如)< Command 1> 的输出后,我想更新< Command的参数5> ,或在列表中的任意位置添加新的< Command k> .但是我希望能够随时离开我的计算机,并让它在我对列表的最后一次更新中继续工作.

Each command takes a long time to run, and sometimes after seeing the output of (e.g.) <Command 1>, I'd like to update a parameter of <Command 5>, or add a new <Command k> at an arbitrary position in the list. But I want to be able to walk away from my machine at any time, and have it keep working through my last update to the list.

这类似于此处的问题:在运行时编辑shell脚本.可以提供一些答案,但是该问题还有其他一些要编辑脚本文件本身的约束,我怀疑有一个更简单的答案,因为我没有那个确切的约束.

This is similar to the question here: Edit shell script while it's running. Some of those answers could be made to serve, but that question had the additional constraint of wanting to edit the script file itself, and I suspect there is a simpler answer because I don't have that exact constraint.

我当前的解决方案是通过调用第二个脚本来结束我的脚本.我可以在第一个文件运行时编辑第二个文件,这使我可以将新命令附加到列表的末尾,但是我无法对第一个文件中的命令列表进行任何更改.在第二个文件中开始执行后,我将无法再进行任何更改.但是我经常停止脚本来插入更新,这有时意味着停止一条几乎完成的长命令,只是为了让我可以在离开机器之前先更新列表中的后续项.我当然可以通过这种方式将许多文件链接在一起,但是(有希望)有一个简单的解决方案,这似乎是一团糟.

My current solution is to end my script with a call to a second script. I can edit the second file while the first one runs, this lets me append new commands to the end of my list, but I can't make any changes to the list of commands in the first file. And once execution has started in the second file, I can't make any more changes. But I often stop my script to insert updates, and this sometimes means stopping a long command that is almost complete, only so that I can update later items on the list before I leave my machine for a time. I could of course chain together many files in this way, but that seems a mess for what (hopefully) has a simple solution.

推荐答案

与我提供完整代码的答案相比,这更是一种概念性的答案.我的想法是运行Redis(此处是Disdis描述)-这很简单安装-并将其用作数据结构服务器.在您的情况下,数据结构将是作业列表.

This is more of a conceptual answer than one where I provide the full code. My idea would be to run Redis (Redis description here) - it is pretty simple to install - and use it as a data-structure server. In your case, the data structure would be a list of jobs.

因此,您基本上将每个作业添加到Redis列表中,您可以在命令行上使用 LPUSH 来完成此操作:

So, you basically add each job to a Redis list which you can do using LPUSH at the command-line:

echo "lpush jobs job1" | redis-cli

然后,您可以根据需要并行地启动一个或多个工人,然后他们坐在一个循环中,重复执行 BLPOP 作业(阻止弹出,等到有作业).列出并处理它们:

You can then start one, or more, workers, in parallel if you wish and they sit in a loop, doing repeated BLPOP of jobs (blocking pop, waiting till there are jobs) off the list and processing them:

#!/bin/bash
while :; do
    job=$(echo brpop jobs 0 | redis_cli)
    do $job
done

然后,当工作人员正在运行时,您可以自由地使用删除和插入内容来修改列表.

And then you are at liberty to modify the list while the worker(s) is/are running using deletions and insertions.

示例此处.

这篇关于想要运行命令列表,但能够在运行时编辑列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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