我有盒子通过RSYNC拉下视频内容。 + - 700盒,我目前正在测试四个盒子。
每个盒子上都有一个bash脚本,用于从中央服务器通过反向RSYNC将内容拉下来。
我想通过以下方式管理这些与服务器的连接:
以下数据在视频所在的服务器上显示如下。
root@TESTSRV01:~# ps aux | grep rsync
1029 13357 0.0 0.0 4308 604 ? Ss 11:46 0:00 sh -c rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
1029 13358 0.1 0.0 30852 1444 ? S 11:46 0:00 rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
1029 13382 0.0 0.0 4308 604 ? Ss 11:47 0:00 sh -c rsync --server --sender -vre.iLsf --bwlimit=100 --append --append . /home/test/Videos/
1029 13383 0.1 0.0 39432 1800 ? S 11:47 0:00 rsync --server --sender -vre.iLsf --bwlimit=100 --append --append . /home/test/Videos/
1029 13400 0.0 0.0 4308 604 ? Ss 11:47 0:00 sh -c rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
1029 13401 0.1 0.0 39432 1800 ? S 11:47 0:00 rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
1029 13451 0.0 0.0 4308 608 ? Ss 11:48 0:00 sh -c rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
1029 13452 0.0 0.0 71128 2248 ? S 11:48 0:00 rsync --server --sender -vre.iLs --bwlimit=100 --append --append . /home/test/Videos/
从框中的脚本,到目前为止我有以下内容:
ps -u test | grep rsync | awk '{ print $1 }'
这将返回以下内容:(哪个是PID)
13358
13383
13401
13452
请记住,第1点和第2点。我将如何实现这一目标。谢谢!
你可以做到这一点。提取按start_time排序的所有pid并将它们放在数组中。
declare -a pids
pids=($(ps -u test --sort=start_time | grep '[r]sync' | awk '{print $1}'))
if [[ "${#pids[@]}" -gt 50 ]]; then
# I am not sure how many process you want to kill.
# Let say you want to kill the first one.
kill -9 "${pids[0]}"
fi
注意:grep模式是'[r]sync'
而不是'rsync'
,因为它排除了grep命令本身。
如果您想将进程总数限制为50.您可以执行以下操作:
declare -a pids
pids=($(ps -u test --sort=start_time | grep '[r]sync' | awk '{print $1}'))
if [[ "${#pids[@]}" -gt 50 ]]; then
# You want to kill all execess processes
excess=$((${#pids[@]} - 50))
echo "Excess processes: $excess"
kill -9 "${pids[@]:0:$excess}"
fi