2

I am trying to make my raspberry pi automatically restart every week. But I am unable to create the cronjob as I get a error message that the temporary crontab file was unable to be created.

/home/pi $ crontab -e
/tmp/crontab.Kd1gSa: Read-only file system
Creation of temporary crontab file failed - aborting

But looking at the permissions of tmp it seems that all is in order:

/home/pi $ sudo ls -lsa /
total 80
 4 drwxr-xr-x  21 root root  4096 Aug 29 16:51 .
 4 drwxr-xr-x  21 root root  4096 Aug 29 16:51 ..
 4 drwxr-xr-x   2 root root  4096 Aug 13 11:28 bin
 4 drwxr-xr-x   2 root root  4096 Feb 13  2020 boot
 0 drwxr-xr-x  17 root root  3860 Sep 14 23:08 dev
 4 drwxr-xr-x 121 root root  4096 Sep  8 09:26 etc
 4 drwxr-xr-x   4 root root  4096 Aug 22 01:45 home
 4 drwxr-xr-x  18 root root  4096 Aug 22 01:45 lib
16 drwx------   2 root root 16384 Feb 13  2020 lost+found
 4 drwxr-xr-x   5 root root  4096 Aug 29 16:14 media
 4 drwxr-xr-x   2 root root  4096 Feb 13  2020 mnt
 4 drwxr-xr-x   6 root root  4096 Aug 22 01:45 opt
 0 dr-xr-xr-x 176 root root     0 Dec 31  1969 proc
 4 drwx------  10 root root  4096 Sep  8 09:24 root
 0 drwxr-xr-x  26 root root   880 Sep 18 06:02 run
 4 drwxr-xr-x   2 root root  4096 Aug 13 11:30 sbin
 4 drwxr-xr-x   2 root root  4096 Feb 13  2020 srv
 0 dr-xr-xr-x  12 root root     0 Dec 31  1969 sys
 4 drwxrwxrwt  18 root root  4096 Sep 19 00:00 tmp
 4 drwxr-xr-x  12 root root  4096 Aug 21 23:55 usr
 4 drwxr-xr-x  12 root root  4096 Sep  6 10:27 var

What could be the problem here?


As Seamus pointed out my file-system seems to be read-only. I used apple-pi-baker to flash this drive from a backup. I just don't have physical access to this device since its across the country atm at my university.

/home/pi $ mount -l -t ext4
/dev/mmcblk0p7 on / type ext4 (ro,relatime) [root]

I have added the fs check commands to the bootline which was in a different location than /boot

/home/pi $ sudo ls -lsa /media/pi/boot/cmdline.txt
1 -rw-r--r-- 1 pi pi 179 Dec 31  1979 /media/pi/boot/cmdline.txt

/home/pi $ sudo cat /media/pi/boot/cmdline.txt console=serial0,115200 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes fsck.mode=force rw rootwait quiet splash plymouth.ignore-serial-consoles

I think my raspberry pi is able to boot to desktop due to the rw in my cmdline. B/c without it I get read-only file system errors at boot. But I also this this might be leaving parts of the filesystem to be read-only?

/home/pi $ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        26G   21G  4.1G  84% /
devtmpfs        3.8G     0  3.8G   0% /dev
tmpfs           3.9G   52M  3.8G   2% /dev/shm
tmpfs           3.9G  370M  3.5G  10% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           788M     0  788M   0% /run/user/1000
/dev/mmcblk0p6  253M   55M  198M  22% /media/pi/boot
/dev/mmcblk0p1  2.4G  2.3G   87M  97% /tmp/tmp.5LW77DmOAt

gparted output

Gabriel Fair
  • 123
  • 7

2 Answers2

1

The mode of /tmp is supposed to be 1777.

sudo chmod 1777 /tmp

The 777 part means anyone can read, write or search the directory. The 1 part means only the owner of each file in the directory is allowed to delete it.

Waxrat
  • 556
  • 4
  • 2
1

First problem:

Your /tmp permissions are in order - that's not the problem.

First guess is that your filesystem is broken. The solution is to re-flash your SD card (You do have a backup, right?).

You can also check to make sure you've not somehow inadvertently mounted your filesystem as ro:

$ mount -l -t ext4
/dev/mmcblk0p2 on / type ext4 (rw,noatime)

If you get an output that's different from this (on /dev/mmcblk0p2), then you may have other issues. You can edit your question to share this & we'll have a look.

If you don't have a backup, or if you want to gain some experience, you can attempt to repair your filesystem with fsck. However, the thing with fsck is that it can only be run on an unmounted file system, so you will need to run it during the boot process - before / is mounted. There are a couple of ways to do this, but the preferred method for RPi (and all other distros that use systemd) is to add the following kernel boot parameters to /boot/cmdline.txt:

fsck.mode=force
fsck.repair=yes

There's already at least two good answers here on how to do this, so check this answer or this answer if you decide to do this.

Second problem

And one other thing to do once your filesystem issue is sorted:

  1. reboot requires root privileges; i.e. sudo reboot

  2. you should not use sudo in a crontab for user pi

  3. instead, create a crontab for root:

sudo crontab -e

in your crontab editor, add this line to run every Sunday at 02:00:

0 2 * * MON reboot

save & close editor

See man 5 crontab & the "crontab guru" for details.

Seamus
  • 21,900
  • 3
  • 33
  • 70
  • Thank you so much for your help. I have added the additional information. This device was restored from a backup but I currently don't have physical access to the pi. :( – Gabriel Fair Sep 27 '20 at 16:05
  • I am a bit confused on how to create the root crontab. Would it be sudo crontab -u root -e or crontab -e -u root? Or do you normally go about it a different way? – Gabriel Fair Sep 27 '20 at 17:01
  • @GabrielFair: sudo crontab -e will do it. And since it runs with root privileges, you don't need to use sudo. Using sudo in a cron job would be problematic b/c sudo would have to authenticate the cron user & there's no good way to do that. – Seamus Sep 27 '20 at 18:07