Force FSCK on reboot

Many people of you out there might highly disagree with me but I am very lazy ;). Just last night I had to run a FSCK on a box at work. Initially, I was a bit hesitant only because it meant that I had to get off my “forth point of contact” and I just didn’t want too. Then it hit me… just force a FSCK on reboot and I would be killing two birds with one stone. Why you ask? Well let me tell you. First, I wouldn’t be getting off my rear and second, the host box would reboot and a FSCK would run to correct the drives’ I/O problems.

But how do I do it? I knew that you could skip a FSCK on reboot with the “-f” switch to /sbin/shutdown but how do I force a FSCK? I told myself, “RTFM n00b!!!” So I did. A quick check of the man page for /sbin/shutdown revealed the “-F” option:

From the man page:

-F Force fsck on reboot.

Aha! The magical “-F” option to /sbin/shutdown is here to save the day! So, the complete command that I used was a combination of ssh and /sbin/shutdown. Why did I use ssh? Don’t ask me, read about it in a previous entry that I wrote.

$ ssh [email protected] "/sbin/shutdown -F -r now"

Bamm! That command:

  1. Opened a ssh connection to the from my CLI
  2. Unmounted all drives cleanly
  3. Rebooted the box
  4. Forced a FSCK on all the drives set to mount in /etc/fstab
  5. And thats it!

Once the FSCK was complete, the system booted like normal. Damn, it feels good to be lazy.

Alternatively you could run a FSCK on all partitions if for some reason you wanted to schedule a reboot. You could set up a cron or at job that would reboot the box at a certain time. Perhaps during a maintenance window or something. All you would have to do is log into the system or run it from a script:

$ touch /forcefsck

Don’t forget to issue the actual reboot command in your script or if you are logged in to run the /sbin/shutdown command with the “-r” option. The only drawback to all this automation is that if the the FSCK fails for whatever reason, it may require physical access to the terminal if any errors are found. Then, and only then will I have to get off my lazy ass and plug in a console to the box. Damn, it was too good to be true!

About this entry