我有一个 bash 脚本文件,在其中执行一堆命令。
#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2
Run Code Online (Sandbox Code Playgroud)
但由于文件中后面的命令与已卸载的硬盘一起使用,因此我需要确保卸载成功后再继续。
我当然可以检查 umount 是否失败并退出 1,但这并不理想。
所以基本上,我想做的是以某种方式让 umount 命令等待,直到设备不忙,然后卸载 HDD 并继续执行脚本。
因此它会像这样工作:
#!/bin/bash
umount /media/hdd1 # Device umounted without any problems continuing the script..
umount /media/hdd2 # Device is busy! Let's just sit around and wait until it isn't... let's say 5 minutes later whatever was accessing that HDD isn't anymore and the umount umounts the HDD and the script continues
something1
something2
Run Code Online (Sandbox Code Playgroud)
谢谢。
我认为下面的脚本可以完成这项工作。它应该以(超级用户权限)运行sudo
。
有一个doer
带有while
循环的函数,它检查mountpoint
设备是否安装在指定的安装点,如果是,则尝试使用 卸载它umount
。当逻辑变量busy
为 false 时,while
循环结束,脚本可以开始“做一些事情”。
#!/bin/bash
function doer() {
busy=true
while $busy
do
if mountpoint -q "$1"
then
umount "$1" 2> /dev/null
if [ $? -eq 0 ]
then
busy=false # umount successful
else
echo -n '.' # output to show that the script is alive
sleep 5 # 5 seconds for testing, modify to 300 seconds later on
fi
else
busy=false # not mounted
fi
done
}
########################
# main
########################
doer /media/hdd1
doer /media/hdd2
echo ''
echo 'doing something1'
echo 'doing something2'
Run Code Online (Sandbox Code Playgroud)