简体   繁体   English

如何在后台运行脚本并控制数量?

[英]How do I run scripts in the background and control the quantity?

I have some test programs, their names are我有一些测试程序,它们的名字是

bingo_64k.llc_pref
bingo.llc_pref
bingo_multitable_pc+addr_addr.llc_pref
bingo_multitable_pc+addr_addr_pc+offs.llc_pref
bingo_multitable_pc+addr_addr_pc+offs_pc.llc_pref

I want to use these programs to run some benchmarks.我想使用这些程序来运行一些基准测试。 I wrote a script to control the parameters.我写了一个脚本来控制参数。 The name of the script is run.sh .脚本的名称是run.sh I only need ./run.sh --benchmark=benchmark_name_index --binary=Executable_file to run.我只需要./run.sh --benchmark=benchmark_name_index --binary=Executable_file即可运行。 The run.sh script will automatically add other parameters at runtime. run.sh脚本会在运行时自动添加其他参数。

Eg ./run.sh --benchmark=1 --binary=bingo_64k.llc_pref例如./run.sh --benchmark=1 --binary=bingo_64k.llc_pref

Each benchmark takes about three hours to run.每个基准测试大约需要三个小时才能运行。 I want them to execute in parallel.我希望它们并行执行。 I plan to put each run in the background to run.我打算把每次运行都放在后台运行。 I wrote the following script file.我编写了以下脚本文件。 The name is tem.sh .名称是tem.sh Note that I wrote the name of all the test program in the evaluate_prefetch file.请注意,我在evaluate_prefetch文件中写了所有测试程序的名称。

BENCHMARK_NUM=5
while read LINE
do
    for ((i=1; i<=$BENCHMARK_NUM; i++))
    do
    
        # sleep 2
        # array=($(pidof $LINE))
        # echo ${#array[@]}
        # while [ ${#array[@]} -ge 10 ]
        # do
        #   sleep 60
        #   array=($(pidof $LINE))
        # done

        cmd="./run.sh --benchmark=$i --binary=$LINE &"
        $cmd
    done
done < evaluate_prefetch
  1. After I run the above script.在我运行上面的脚本之后。 I found that I can see these processes through top command.我发现我可以通过top命令看到这些进程。 But when I use jobs , nothing is displayed.但是当我使用jobs ,没有显示任何内容。

  2. I don't want to run too many in the background at once, it may affect other people.我不想一次在后台运行太多,它可能会影响其他人。 I want to run only 10 at a time.我想一次只运行 10 个。 What should I do?我应该怎么办? I use the following code to determine how many are currently running.我使用以下代码来确定当前正在运行的数量。 But the following function is very slow to execute, and did not get the correct result.但是下面的函数执行起来很慢,并没有得到正确的结果。

      get_process_num(){
            process_num=0
            while read LINE
            do
                sleep 2
                array=($(pidof $LINE))
                let process_num=$process_num+${#array[@]}
                echo $process_num
            done < evaluate_prefetch
        }
  1. ./tem.sh & Should I run the above script like this so that when I exit the terminal, it will still run in the background? ./tem.sh &我是否应该像这样运行上面的脚本,以便当我退出终端时,它仍然会在后台运行?

  2. I always accidentally make some errors when I run it.我在运行它时总是不小心犯一些错误。 How can I kill these processes all at once and then rerun them?我怎样才能一次杀死这些进程,然后重新运行它们? I tried killall sleep .我试过killall sleep But when I execute top , I can still see many sleep processes.但是当我执行top ,我仍然可以看到许多睡眠进程。

There are a total of 140 priorities and two distinct priority ranges implemented in Linux.在 Linux 中实现了总共 140 个优先级和两个不同的优先级范围。 The first one is a nice value (niceness) which ranges from -20 (highest priority value) to 19 (lowest priority value) and the default is 0. The other is the real-time priority, which ranges from 1 to 99 by default, then 100 to 139 are meant for user-space.第一个是nice值(niceness),范围从-20(最高优先级值)到19(最低优先级值),默认为0。另一个是实时优先级,默认范围从1到99 ,然后 100 到 139 用于用户空间。

From the top outputs, you'll notice that there is a column called PR and PRI which show the priority of a process.top输出中,您会注意到有一个名为 PR 和 PRI 的列,它们显示了进程的优先级。

NI – is the nice value, which is a user-space concept, while PR or PRI – is the process's actual priority, as seen by the Linux kernel. NI – 是 nice 值,这是一个用户空间概念,而 PR 或 PRI – 是进程的实际优先级,如 Linux 内核所见。

Total number of priorities = 140 Real time priority range(PR or PRI): 0 to 99 User space priority range: 100 to 139优先级总数 = 140 实时优先级范围(PR 或 PRI):0 到 99 用户空间优先级范围:100 到 139

Nice value range (NI): -20 to 19尼斯值范围 (NI):-20 到 19

PR = 20 + NI PR = 20 + NI

PR = 20 + (-20 to + 19) PR = 20 +(-20 到 + 19)

PR = 20 + -20 to 20 + 19 PR = 20 + -20 到 20 + 19

PR = 0 to 39 which is same as 100 to 139. PR = 0 到 39,与 100 到 139 相同。

But if you see a rt rather than a number as shown in the screenshot below, it basically means the process is running under real-time scheduling priority.但是,如果您看到的是 rt 而不是数字,如下面的屏幕截图所示,这基本上意味着该进程正在实时调度优先级下运行。

to change the niceness:改变美好:

$ nice -n niceness-value [command args]

OR要么

$ nice -niceness-value [command args] 

OR要么

$ nice --adjustment=niceness-value [command args]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM