[英]Monitor disk space in an `until` loop
my first question here. 我的第一个问题在这里。 Hope it's a good one.
希望它是一个好人。
So I'm hoping to create a script that kills another script running arecord
when my disk gets to a certain usage. 所以,我希望创建一个杀死运行另一种语言的脚本
arecord
时,我的盘达到一定的使用。 (I should point out, I'm not exactly sure how I got to that df
filter... just kinda searched around...) My plan is to run both scripts (the one recording, and the one monitoring disk usage) in separate screen
s. (我应该指出,我不确定我是如何到达那个
df
过滤器的...只是有点搜索...)我的计划是在以下目录中同时运行两个脚本(一个记录和一个监视磁盘使用情况)分开的screen
。
I'm doing this all on a Raspberry Pi, btw. 我正在Raspberry Pi上进行所有操作,顺便说一句。
So this is my code so far: 到目前为止,这是我的代码:
#!/bin/bash
DISK=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
until [ $DISK -ge 50 ]
do
sleep 1
done
killall arecord
This code works when I play with the starting value ("50" changed to "30" or so). 当我使用起始值(“ 50”更改为“ 30”左右)播放时,此代码有效。 But it doesn't seem to "monitor" my disk the way I want it to.
但这似乎并不能以我希望的方式“监视”磁盘。 I have a bit of an idea what's going on: the variable DISK is only assigned once, not checked or redefined periodically.
我对发生的事情有所了解:变量DISK仅分配一次,而不定期检查或重新定义。
In other words, I probably want something in my until
loop that "gets" the disk usage from df
, right? 换句话说,我可能想要在我的
until
循环中从df
“获取”磁盘使用率的内容,对吗? What are some good ways of going about it? 有什么好的解决方法?
= =
PS I'd be super interested in hearing how I might incorporate this whole script's purpose into the script running arecord itself, but that's beyond me right now... and another question... PS我对听到如何将整个脚本的目的结合到运行记录本身的脚本中非常感兴趣,但是现在这超出了我的范围……还有另一个问题……
You are only setting DISK
once since it's done before the loop starts and not done as part of the looping process. 你只设置
DISK
,因为它是在循环开始之前完成,而不是作为循环过程的一部分进行一次。
A simple fix is to incorporate the evaluation of the disk space into the actual while
loop itself, something like: 一个简单的解决方法是将对磁盘空间的评估合并到实际的
while
循环本身中,例如:
#!/bin/bash
until [ $(df / | awk 'NR==2 {print $5}' | tr -d '%') -ge 50 ] ; do
sleep 1
done
killall arecord
You'll notice I've made some minor mods to the command as well, specifically: 您会注意到我也对该命令做了一些小的修改,特别是:
awk
itself to get the relevant line from the ps
output, no need to use grep
in a separate pipeline stage. awk
本身从ps
输出中获取相关行,而无需在单独的管道阶段中使用grep
。 tr
for deleting single characters, sed
can do it but it's a bit of overkill. tr
删除单个字符, sed
可以做到,但这有点矫kill过正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.