首页 \ 问答 \ Bash - 启动和终止进程(Bash - starting and killing processes)

Bash - 启动和终止进程(Bash - starting and killing processes)

我需要一些关于“简单”bash脚本的建议。

我想开始大约500个程序“myprog”的实例,并在x秒后杀死所有这些实例

简而言之,我有一个循环在后台启动程序, sleep xsleep x (秒数) pkill用程序名称调用。

我的问题是:

  1. 如何验证10秒后所有500个实例都在运行? ps和grep与计数组合还是有另一种方式?

  2. 如何计算pkill(或类似的kill函数)实际杀死的进程数(以便在实际时间限制之前没有任何进程终止)?

  3. 如何重定向pkill(或类似的kill函数)的输出,使其不输出被杀死的进程信息,从而./initializeTest: line 250: 7566 Terminated ./$myprog 500行./initializeTest: line 250: 7566 Terminated ./$myprog 。 重定向到/ dev / null并没有成功。


I need some advice on a "simple" bash script.

I want to start around 500 instances of a program "myprog", and kill all of them after x number of seconds

In short, I have a loop that starts the program in background, and after sleep x (number of seconds) pkill is called with the program name.

My questions are:

  1. How can I verify that after 10 seconds all 500 instances are running? ps and grep combination with counting or is there another way?

  2. How can I get a count of how many processes did the pkill (or similar kill functions) actually kill (so that there are not any processes that terminate before the actual timelimit)?

  3. How can one redirect the output of pkill(or similar kill functions) so that it doesn't output the killed process information, so that 500 lines of ./initializeTest: line 250: 7566 Terminated ./$myprog can be avoided. Redirecting to /dev/null didn't do the trick.


原文:https://stackoverflow.com/questions/5066651
更新时间:2022-10-11 11:10

最满意答案

最后,在如何使用select2版本4恢复值的一些试验和错误后,它可能不是干净的,但它的工作原理

<% if f.object.persisted? && f.object.hotel_id  %>
  <% if f.object.hotel_id > 0 %>
    <% value = f.object.hotel_id  %>
    <% text  = Hotel.find(value).name %>
  <% end %>
<% end %>
<%= f.select(:hotel_id, options_for_select([["#{text}", value.to_s]]),  {selected: value},  :class => 'select2-autocomplete' ) %>

关键的答案是底线,

  • 使用f.select,
  • 使用option_for_select生成选项
  • 确保选项使用字符串value.to_s
  • 根据选项使用{selected:value}

希望它可以帮助其他使用select2和ajax搜索的rails用户


Finally after some trial and error below how to use select2 version 4 to restore the value, it's probably not clean but it works

<% if f.object.persisted? && f.object.hotel_id  %>
  <% if f.object.hotel_id > 0 %>
    <% value = f.object.hotel_id  %>
    <% text  = Hotel.find(value).name %>
  <% end %>
<% end %>
<%= f.select(:hotel_id, options_for_select([["#{text}", value.to_s]]),  {selected: value},  :class => 'select2-autocomplete' ) %>

The key answer is bottom line,

  • using f.select,
  • generate option using option_for_select
  • make sure the option is using string value.to_s
  • under option use { selected: value}

hope it can help other rails users that using select2 with ajax search

相关问答

更多
  • 您将指定为initSelection的函数以初始value作为参数进行调用。 因此,如果value空,则不会调用该函数。 当你指定value='[{"id":"IN","name":"India"}]'而不是data-initvalue ,函数被调用并且选择可以被初始化。 The function which you are specifying as initSelection is called with the initial value as argument. So if value is emp ...
  • 你正在做大多数事情正确,看起来你唯一遇到的问题是你在设置新值后没有触发change方法。 没有change事件,Select2不能知道基础值已更改,因此只显示占位符。 将最后一部分更改为 .val(initial_creditor_id).trigger('change'); 应该解决您的问题,您应该立即看到UI更新。 这是假设你有一个
  • 好的,我终于找到了解决方案。 我误解了$ .ajax() - 我并没有真正想到它实际上是异步调用。 检查调用返回的数据的代码是在AJAX实际完成之前运行的,所以我总是得到未定义的。 我为AJAX调用分配了一个变量,并设置了“async:false”,它运行得很好。 fetchSetNotifications: function() { var addresses = $.ajax({ method: 'GET', dat ...
  • 最后,在如何使用select2版本4恢复值的一些试验和错误后,它可能不是干净的,但它的工作原理 <% if f.object.persisted? && f.object.hotel_id %> <% if f.object.hotel_id > 0 %> <% value = f.object.hotel_id %> <% text = Hotel.find(value).name %> <% end %> <% end %> <%= f.select(:hotel_id, ...
  • 在脚本内部,它应该在文档准备好时触发select事件 $(document).ready(function () { ... } 并使用如下所示的select元素声明监视元素