Ubuntu filesystem becomes corrupt after reboot (cron)

OS: Ubuntu 20.04.3 LTS

Basically I set my root cron to reboot every week. At first this was the line I used:

0 1 * * 0 /sbin/shutdown -r now

As the weeks went by I began to notice small but annoying issues after every reboot. So I decided to switch it up about two weeks ago. I instead made it run a script with just reboot in it.

0 1 * * 0 /home/user/reboot.bash

I've been noticing less issues based on these two weeks but there was this one error that once happened with the old crontab and just happened today again even with the new crontab - the filesystem becoming read-only.

Sure, a quick fsck fixes it no problem. But I have no idea why it would corrupt the filesystem when I reboot using cron. When I reboot using the GUI or by reboot manually, it works just fine.

Keep in mind that this doesn't always happen; everything was fine last week. Sometimes the problem is different too - one time some of my drivers stopped working.

I have seen posts where people have this problem while dual-booting. However I don't think it applies since I only have ubuntu on the machine. I have no idea which logs to post so if you need any please tell me in the comments.


Solution 1:

Scheduling a "weekly reboot" is an old, old practice that is no longer needed. Linux isn't Windows. Blindly rebooting, without synchronizing the filesystems with the hardware causes filesystem corruption and data loss. Much of the corruption can be fixed automatically at boot time, but not all. Lost data is irreparably lost. Don't do automatic reboots.

Rather, identify what caused you to "need" a reboot, diagnose the problem and fix it.

One helpful tool is journalctl, a system log viewer. Read man journalctl. Do sudo journalctl --list-boots to see the index and times (this takes a while since it needs to read ALL the logs ever logged). Index 0 is from the current boot to the current time, -1 is for the previous boot, etc.

Logging advice:

# do this once (or forever have to `sudo journalctl`)
sudo adduser $USER systemd-journal 
# then either logout/login or `newgrp systemd-journal`

# add to ~/.bash_aliases or ~/.bashrc 
# tsjou "18-11-01 01:55:28" for journalctl --since 
alias tsjou="date '+%y-%m-%d %H:%M:%S'" 
# use other `date` options, e.g. `tsjou --date=yesterday`
# make "journalctl" easier to type
alias jc=journalctl 

# record beginning for journalctl (a thumbtack in the log flow)l
alias jstart="my_JSTART_TIME=\"\$(tsjou)\"" 
# show all logs since `jstart`
alias jend="journalctl --since=\"\$my_JSTART_TIME\""

# what happened in the last 5 or 10 minutes?
alias jc5="journalctl --since=\"-5 minutes\"" 
alias jc10="journalctl --since=\"-10 minutes\"" 
# what has NetworkManager logged since boot?
alias jcnet="journalctl -b 0 _SYSTEMD_UNIT=NetworkManager.service"