我有一个bash脚本文件,在这里我执行了一堆命令。
#!/bin/bash
umount /media/hdd1
umount /media/hdd2
something1
something2但是由于文件后面的组件与umounted一起工作,我需要确保umount在继续之前是成功的。
当然,我可以检查一下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谢谢。
发布于 2018-10-14 09:56:26
我认为下面的脚本将完成这项工作。它应该使用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'https://askubuntu.com/questions/1083624
复制相似问题