Adding jobs to the crontab
could someone guide me on how to write a script to mount my HDD at 12pm and unmount it at 3am.
Hi,
Here we go - hope it helps and there are not to many typos in the text...
With kind regards
Joe
Prerequesits:
- Enable and format the Persistent JFFS2 partition on the router under Administration / System.
- Make yourself familiar with Telnet (you can use PuTTY or the default Windows telnet command).
- Make yourself comfortable with the vi editor (or install Entware and nano for a more comfortable editor).
a) Find the USB attached HDD:
Code:
chief@RT-N66U:/tmp/home/root# fdisk -l
Disk /dev/sda: 750.1 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda2 66 73 64260 82 Linux swap
/dev/sda4 139 91201 731463547+ 83 Linux
Disk /dev/sdb: 1018 MB, 1018691584 bytes
30 heads, 29 sectors/track, 2286 cylinders
Units = cylinders of 870 * 512 = 445440 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 2287 994691+ 6 FAT16
Looking on the fdisk output my HDD is mapped to the device
/dev/sda.
The partion to be mounted and unmounted is mapped to the device name
/dev/sda4
To be able to remove the HDD in my case the Linux swap also need to be stopped.
Of course you may have different device names!
Now we need to know the standard mount points:
Code:
chief@RT-N66U:/tmp/home/root# mount
rootfs on / type rootfs (rw)
/dev/root on / type squashfs (ro)
proc on /proc type proc (rw)
tmpfs on /tmp type tmpfs (rw)
devfs on /dev type tmpfs (rw,noatime)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw)
/dev/mtdblock4 on /jffs type jffs2 (rw,noatime)
usbfs on /proc/bus/usb type usbfs (rw)
/dev/sda4 on /tmp/mnt/Data type ext3 (rw,nodev,noatime,data=ordered)
/dev/sdb1 on /tmp/mnt/sdcard type ext3 (rw,nodev,noatime,data=ordered
The Data partition (
/dev/sda4) is by default mounted to the mount point
/tmp/mnt/Data.
The file systems type is
ext3 and the mount options are
rw,nodev,noatime,data=ordered.
b) Unmount the HDD:
The test the unmount command:
umount /tmp/mnt/Data
Create a unmount script:
nano (or vi) /jffs/scripts/unmount-hdd.sh
Enter the text:
#!/bin/sh
/bin/umount /tmp/mnt/Data
For nano type: "Ctrl-O" plus "Enter" to save and exit
c) Mount the HDD:
The test the mount command:
/bin/mount -t ext3 -o rw,nodev,noatime,data=ordered /dev/sda4 /tmp/mnt/Data
Remember to use your specific device path and mount point!
Creat the mount script:
nano (or vi) /jffs/scripts/mount-hdd.sh
Enter the text:
#!/bin/sh
/bin/mount -t ext3 -o rw,nodev,noatime,data=ordered /dev/sda4 /tmp/mnt/Data
For nano type: "Ctrl-O" plus "Enter" to save and exit
d) Elaborate on cron jobs and cru:
The full syntax of the cron jobs can be found in the
cron wiki page
What we need is the syntax for adding a cron job to mount the HDD at 12:00pm (and 3:00am), every day:
Code:
chief@RT-N66U:/jffs/scripts# cru a mount "0 12 * * * /jffs/scripts/mount-hdd.sh"
chief@RT-N66U:/jffs/scripts# cru a unmount "0 3 * * * /jffs/scripts/unmount-hdd.sh"
Let's check the if the cru add was a success:
Code:
chief@RT-N66U:/jffs/scripts# cru l
0 12 * * * /jffs/scripts/mount-hdd.sh #mount#
0 3 * * * /jffs/scripts/unmount-hdd.sh #unmount#
Let's delete the mount cru:
Code:
chief@RT-N66U:/jffs/scripts# cru d mount
chief@RT-N66U:/jffs/scripts# cru d unmount
chief@RT-N66U:/jffs/scripts# cru l
e) Create a cron setup script:
Add the script to jffs to execute the cron scheduling on each boot of the router. Details on the different user scripts can be found in
Merlin's wiki page.
nano (or vi) /jffs/scripts/services-start
Enter the text:
Code:
[B]#!/bin/sh
/usr/bin/logger -t START_$(basename $0) "started [$@]" [/B]# logs the start of the script into the syslog.log
[B]SCRLOG=/tmp/$(basename $0).log [/B]# creates a log for this script (services-start.log)
[B]touch $SCRLOG [/B]# creates the log file
[B]echo "START_$(basename $0) started [$@]" >> $SCRLOG [/B]# writes the start into the log
[B]/usr/sbin/cru a mount "0 12 * * 0 /jffs/scripts/mount-hdd.sh" >> $SCRLOG [/B]# adds the mount cron job
[B]/usr/sbin/cru a unmount "0 3 * * 0 /jffs/scripts/unmount-hdd.sh" >> $SCRLOG [/B]# adds the unmount cron job
[B]if [ "$?" -ne 0 ] [/B]# checks for error
[B]then
echo "Error in services-start execution! Script: $0" >> $SCRLOG [/B]# writes error message into the log file
[B]exit $? [/B]# exit with error code
[B]else
echo "Services-start execution OK. Script: $0" >> $SCRLOG[/B] # writes OK (if no error) into the log file
[B]exit 0 [/B]# exit with OK code
[B]fi[/B]
f) Make all scripts in JFFS executable:
chmod a+rx /jffs/scripts/*
g) Reboot the router to verify that everything is working
Check the syslog.log for start of the user script:
Code:
chief@RT-N66U:/jffs/scripts# more /tmp/syslog.log | grep started
Jan 1 01:00:13 START_services-start: started []
Check the services-start.log for messages:
Code:
chief@RT-N66U:/jffs/scripts# more /tmp/services-start.log
START_services-start started []
Services-start execution OK. Script: /jffs/scripts/services-start
Check cru for the cron events:
cru l
h) Have fun with your automated enabled / disabled HDD!