是否可以安排“fsck”命令并在后台运行它?

是否可以安排“fsck”命令并在后台运行它?

我想定期运行fsck终端命令,所以它会运行,比如说,每周五上午 10 点。此外,我希望它在后台运行并将输出保存在文件中,比如说 ~/Documents/errors.log。

我正在使用 Ubuntu 18.04.3 LTS,它以 Live Ubuntu 形式运行,并使用 USB 记忆棒的持久存储。

我可以做到吗?怎么做?

答案1

误区——“不,这不可能”

所有这些最受欢迎的谷歌搜索都忽略了这个问题,或者说无法完成。但事实并非如此。fsck计划在文件系统挂载为rw(读/写)之前在启动期间运行。因此,大多数答案都说它不能在系统完全启动后运行:

为什么需要这样做

对于某些类型的用户来说,这是一个好问题。

  • 有时我可能需要数周时间才重新启动笔记本电脑,但不会定期获得好处fsck
  • 其他时候,我每小时可能会重启几十次,因为我要尝试新的 grub 主题或在发行版之间切换以比较功能。在这种情况下,我不想等待额外的 30 秒才能fsck运行。因此,我在启动时将其禁用。

手册上是如何说明如何操作的

您可以运行fsck -n,但它不会准确报告 ReiserFS 的错误(无论该文件系统是什么)。还有另一种鲜为人知的文件系统,它拒绝完全检查。

$ man fsck

FSCK(8)                              System Administration                             FSCK(8)
    NAME

       fsck - check and repair a Linux filesystem

SYNOPSIS
       fsck  [-lsAVRTMNP]  [-r  [fd]] [-C [fd]] [-t fstype] [filesystem...] [--] [fs-specific-
       options]

DESCRIPTION
       fsck is used to check and optionally repair one or more Linux filesystems.  filesys can
       be a device name (e.g.  /dev/hdc1, /dev/sdb2), a mount point (e.g.  /, /usr, /home), or
       an ext2 label or UUID  specifier  (e.g.   UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd  or
       LABEL=root).   Normally,  the  fsck program will try to handle filesystems on different
       physical disk drives in parallel to reduce the total amount of time needed to check all
       of them.

       If  no  filesystems  are specified on the command line, and the -A option is not speci‐
       fied, fsck will default to checking filesystems in /etc/fstab serially.  This is equiv‐
       alent to the -As options.

       The exit code returned by fsck is the sum of the following conditions:

              0      No errors
              1      Filesystem errors corrected
              2      System should be rebooted
              4      Filesystem errors left uncorrected
              8      Operational error
              16     Usage or syntax error

OPTIONS

       -n     For some filesystem-specific checkers, the -n option will cause the  fs-specific
              fsck to avoid attempting to repair any problems, but simply report such problems
              to stdout.  This is however not true for all filesystem-specific  checkers.   In
              particular,  fsck.reiserfs(8)  will  not  report  any  corruption  if given this
              option.  fsck.minix(8) does not support the -n option at all.

检查已安装分区的样子

我有三个分区;旧的(损坏的)Ubuntu 16.04、Ubuntu 19.04(称为Ubuntu 18.04)和新的 Ubuntu 16.04。运行时fchk它们看起来像这样:

$ sudo fsck -n /dev/nvme0n1p7
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
Warning!  /dev/nvme0n1p7 is mounted.
Warning: skipping journal recovery because doing a read-only filesystem check.
Old_Ubuntu_16.04 has been mounted 358 times without being checked, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Old_Ubuntu_16.04: 433493/1515520 files (0.8% non-contiguous), 4956946/6061568 blocks

$ sudo fsck -n /dev/nvme0n1p10
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
Warning!  /dev/nvme0n1p10 is mounted.
Warning: skipping journal recovery because doing a read-only filesystem check.
Ubuntu_18.04: clean, 719735/1782368 files, 5770105/7129088 blocks

$ sudo fsck -n /dev/nvme0n1p6
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
Warning!  /dev/nvme0n1p6 is mounted.
Warning: skipping journal recovery because doing a read-only filesystem check.
New_Ubuntu_16.04: clean, 833786/2953920 files, 8256858/11829504 blocks

如您所见,fsck它告诉我们旧版 Ubuntu 16.04 需要fsck在系统挂载(只读模式)的情况下运行,ro以便必要时进行修复。但是我已经知道它坏了。


fsck稍后我将使用在仅检查模式下在三个已挂载的 Ubuntu 分区上运行的 cron 每周作业来更新这个答案。

相关内容