首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果bash脚本中的“shred”命令遇到错误,就停止它

如果bash脚本中的“shred”命令遇到错误,就停止它
EN

Ask Ubuntu用户
提问于 2019-12-12 18:18:07
回答 2查看 1.6K关注 0票数 5

我有一个工作站,我们已经设置了消毒多个硬盘驱动器。我运行一个脚本来检测硬盘驱动器,然后在每个硬盘上运行'shred‘命令。问题是,如果任何硬盘驱动器出现故障( Ubuntu不再看到驱动器),而不是停止运行“shred”,“shred”就会输出到无穷大的范围内。

代码语言:javascript
复制
shred: /dev/sdd: error writing at offset 103456287104: Input/Output error

我没有看到“shred”的任何选项可以使它在遇到错误时退出,而且我显然不希望脚本在I/O错误的情况下永远运行。因为当它遇到这个错误时,'shred‘不会自动停止,所以会有其他东西并行运行来执行某种错误检查。在我的脚本中,我的详细输出“shred”重定向到一个日志文件,我实际上使用该日志文件检查脚本的另一部分中的“shred”是否成功完成。但是,我不知道如何在 'shred‘仍在运行时,继续检查日志文件<#>。

有人对我如何完成这种“并行错误检查”有什么想法吗?

当“擦除”命令检测到I/O错误时,我知道它会退出,但是由于我们无法控制的原因,我们只能使用“shred”。令人沮丧的是,“切碎”并没有起到同样的作用。如果but.....it没有错误的话,让它停下来似乎是一件轻而易举的事。

这是我剧本中的“撕碎”部分:

代码语言:javascript
复制
#!/bin/bash
log=/root/sanilog.txt
## get disks list
drives=$(lsblk -nodeps -n -o name |grep "sd")
for d in $drives; do
     shred -n 3 -v /dev/$d >> $log 2>&1
done
EN

回答 2

Ask Ubuntu用户

发布于 2020-10-26 11:53:20

我在做一个脚本来用碎片。

我对你也有同样的问题,但也有可能这样做:

代码语言:javascript
复制
# My device become the variable "$dev"
dev=/dev/sdd

# Running Shred using my variable and put the output 2 & 1 (msg and error) in a file, then put & to launch in background
# The log file will be shred_sdd.log
# ${dev:5} means /dev/sdd without the first 5 characters, because '/' is nod good in a file name
shred -fvzn1 "$dev" >"shred_${dev:5}.log" 2>&1 &

# So while the pid concerning sdd is running, check word 'error' in the the shred_sdd.log, if yes then write a message and kill the PID, else wait 60 sec before re-checking 
while ps aux|grep "shred.*${dev}"|grep -v 'grep' >/dev/null; do
< "shred_${dev:5}.log" grep -i 'error' >/dev/null
if [ $? = 0 ]; then
  echo "Too much sector defect on the device ${dev}, shred can not continue"
  PID=$( ps aux|grep "shred.*${dev}"|grep -v 'grep'|awk '{print $2}' )
  kill -9 "$PID"
  break        
else
  sleep 60
fi
done

您可以使用一个函数对所有设备执行相同的任务。

代码语言:javascript
复制
# Variables of my devices (You can use lsblk, depends your configuration)
devices=$(lsscsi -t | grep disk | grep sas | awk '{print $NF}')

function_shred() {
# Put the code here that I wrote previously
}

for dev in $devices; do
function_shred &
done
票数 1
EN

Ask Ubuntu用户

发布于 2020-02-05 18:18:26

在bash脚本顶部的set -e将导致脚本退出,如果任何命令返回非零退出代码。

您还可以尝试一个EXIT trap来使脚本在其本身之后清理干净。

如果不介意尝试一种替代的do shreddd可以做类似的工作:

代码语言:javascript
复制
dd if=/dev/urandom of=/dev/sdd bs=4096

如果你疑神疑鬼,给它两张通行证:)

而且ubuntu也有wipe

代码语言:javascript
复制
wipe /dev/sdd

通过使用fsync()调用和/或O_SYNC位强制磁盘访问,擦除重复地将特殊模式覆盖到要销毁的文件中。在正常模式下,使用34种模式(其中8种是随机的)。

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1195716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档