What's new

New feature - Remove USB Drive Safely on a Timer

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

surajvuk

Occasional Visitor
I like to request a nice to have feature. I have my router on timer plug which switches off and on automatically.

So I like to disconnect the USB drive safely before the router loses power. But I don't want USB drive to lose power suddenly.
 
Unmount it through a cronjob.
 
So I like to disconnect the USB drive safely before the router loses power. But I don't want USB drive to lose power suddenly.
Why? Maybe because you have several users? Btw, is it a spinner HDD or flash drive?

I have had three usb flash drives connected to my router all the time. I also use passive usb hub. And I manually turn off (6-outlet extension cable with switch) my router almost every day when I go to bed. No problemo with the drives, no corrupted data. But I'm the only user of the network, although with several devices connected.
 
Last edited:
Rmerlin I'm looking into cronjob this is all new to me. If I can get this to work then will be perfect solution.

Diamond67 I'm looking to connect a 1tb SSD drive. I wanna leave the hard drive connected. I've had drives corrupted without safely removing the drive in the pass. With the USB stick I pulled it out of PCs and then when I plugged the USB stick back in I had warning message to scan for errors. So I wanna do this properly by setting a timer.
 
Last edited:
Seem like I need help at the first hurdle. On the rooter I have enabled the JFFS customer scripts and configs. (Administration --> System)

I need help in the next step. how do I create script ? do I create scripts in notepad and save the file and then where I do upload the scripts?
 
I've had drives corrupted without safely removing the drive in the pass. With the USB stick I pulled it out of PCs and then when I plugged the USB stick back in I had warning message to scan for errors.
A long time ago I had a strange problem with one of my computers and one certain usb flash drive. Even with safe removal I got random error messages, and scan for errors was suggested next time. I think there was a weird compatibility problem or something like that.
 
I have had a look at the user manual on GitHub -wiki. But I still cant make heads or tails. I cannot script in Linux but am willing to try now as I like to have this feature.

I like to know how the scripts are saved i.e saved in text file and with a specific name? and how to upload to the router and to the specific directory? (do I have to upload as compressed .tar file?

Reading the Scheduled tasks (cron jobs) on wiki. seems like I have to create two scripts one script to copy another script into a specific location /var/spool/cron/crontabs/.

If I can get help with this I am willing to help update the wiki notes as well. thanks
 
Before trying to automate the process with cron, make sure the command(s) you will be using work from the command line.
 
I like to request a nice to have feature. I have my router on timer plug which switches off and on automatically.

So I like to disconnect the USB drive safely before the router loses power. But I don't want USB drive to lose power suddenly.

Take a look here, here and here.

For example, if your timer plug typically powers off your router at 11:00PM each day, make sure your /jffs/scripts/init-start has this line:

Code:
#!/bin/sh
cru a ScheduledOff  "58  22  *  *  *  /sbin/halt"

to unmount your usb drives and halt your router at 10:58 PM
(In my example it's 2 minutes earlier to make sure that this happens first, before your rotary timer "pulls the plug", so to speak)

The halt call should trigger the services-stop and unmount, much like a /sbin/reboot call, except your router will remain in halted state (and not reboot)

If you are using entware, make this your /jffs/scripts/unmount:
Code:
#!/bin/sh
#
# /jffs/scripts/unmount
#
# stop entware prior to attempting to unmount the "entware volume"
#

# determine if this is the entware volume by comparing the '/opt' mountpoint to $1
OPT=$(dirname $(readlink /tmp/opt))
if [ "$1" == "$OPT" ] ; then
        # this should be the same code as in 'services-stop', so you could just call services-stop instead
        /opt/etc/init.d/rc.unslung stop
        swapoff /opt/swap
fi

The above code is from the second link I posted on top.

Anyways, always make sure these scripts are executable:
Code:
chmod +x /jffs/scripts/*

...and when your timer plug restores power to your router, it should restart normally

Note: You should also test this on the command line, just like @ColinTaylor mentioned in the earlier post
 
Last edited:
cru a ScheduledOff "58 22 * * * /sbin/halt"
to unmount your usb drives and halt your router at 10:58 PM
(In my example it's 2 minutes earlier to make sure that this happens first, before your rotary timer "pulls the plug", so to speak)
Be aware that halting the device only shuts it down for about a minute, or even less.
Depending when the watchdog kicks in it will resume normal operation in no time, starting up all services again, including the scripts in /jffs/*.
There is no way to shutdown the router with scripts and software. Unplugging or pushing the power button is the only way.
 
Be aware that halting the device only shuts it down for about a minute, or even less.
Depending when the watchdog kicks in it will resume normal operation in no time
My experience has been different. The halt command totally makes my router unresponsive till I reset power on it.
 
Nice one! Redhat27


#!/bin/sh
cru a ScheduledOff "58 22 * * * /sbin/halt"

1) How to test the command?
2) If I paste the command into notepad. what filename do I need to give it?
3) How would I upload this to the router now? (Do I need to save the Backup JFFS partition and add it to script folder in the compress format and upload the compress file?
 
You'd really need to follow the guide to setup your jffs partition if you have not set it up. If you have set it up, and assuming there is no entware installed, simply either ssh or telnet in to your router:

ssh <router_ip> if you are using a *nix distro or putty <router_ip> if on windows
or, if you have enabled telnetd on the router, and you have the telnet client on your windows PC,
telnet <router_ip>

from inside your network.

Login with the same credentials as your web ui.

To test the halt command, simply type halt at the router command prompt. Be aware that you'll lose connection to your router at this point, and the router will behave as if it is off. You'll need to reset the router to reboot it again.

To schedule the halt at 10:58PM everyday, login again as mentioned above then list the files in /jffs/scripts with the ls /jffs/scripts command.

If you do not see a file called init-start, you can simple do this at the router prompt:
cat >/jffs/scripts/init-start
paste the 2 lines after Code:
Code:
#!/bin/sh
cru a ScheduledOff "58 22 * * * /sbin/halt"
then hit [return] and press [ctrl]+D

If there is a file already existing called /jffs/scripts/init-start then type cat >>/jffs/scripts/init-start and just paste cru a ScheduledOff "58 22 * * * /sbin/halt" then hit [return] and press [ctrl]+D

Then make it executable with chmod +x /jffs/scripts/init-start
It would need another reboot of your router to make the init-start take effect. You can verify that the rule is active by logging on again and issuing the command cru l
Then wait till 10:58PM to see that your router will automatically halt.

You can read up on scp or winscp to transfer files later on when you want to edit the files on your machine and move them to your router.
I'm sorry if you know how to work on the router already. I wrote this post assuming you do not.
 
Last edited:
I like to request a nice to have feature. I have my router on timer plug which switches off and on automatically.

Not really a good idea - if you have a power socket on a timer, and yanking the power out from underneath and not allowing the router to shutdown normally - you risk corruption of the router itself.
 
I suppose if one were to do a sync, sync, shutdown -h now and put in in a crontab entry before the timer turns off mains power...

This way all the file systems are synced up, and then linux executes the init scripts to shutdown the system politely...

that would probably be best...
 
I suppose if one were to do a sync, sync, shutdown -h now and put in in a crontab entry before the timer turns off mains power...

This way all the file systems are synced up, and then linux executes the init scripts to shutdown the system politely...

that would probably be best...
Yes, that's how it's normally done... but remember this isn't Linux. There is no shutdown command and no init scripts. The busybox halt command effectively does the same thing. First it sync's the filesystem, then it sends a terminate signal to all the running processes.

Code:
Mar 16 14:46:17 kernel: SysRq : Emergency Sync
Mar 16 14:46:17 kernel: Emergency Sync complete
Mar 16 14:46:17 WEBDAV Server: daemon is stopped
Mar 16 14:46:17 NFS Server: daemon is stopped
Mar 16 14:46:19 Timemachine: daemon is stopped
Mar 16 14:46:19 MediaServer: daemon is stopped
Mar 16 14:46:19 iTunes: daemon is stopped
Mar 16 14:46:19 FTP Server: daemon is stopped
Mar 16 14:46:19 Samba Server: smb daemon is stopped
Mar 16 14:46:19 kernel: gro disabled
Mar 16 14:46:19 miniupnpd[1180]: shutting down MiniUPnPd
Mar 16 14:46:20 dnsmasq[3300]: exiting on receipt of SIGTERM
Mar 16 14:46:20 dnscrypt-proxy: stop dnscrypt-proxy
Mar 16 14:46:22 dropbear[24645]: Exit (admin): Terminated by signal
Mar 16 14:46:22 dropbear[593]: Early exit: Terminated by signal
Mar 16 14:46:22 wan6: wan6 interface v6in4 is down
Mar 16 14:46:22 stop_wan(): perform DHCP release

Edit: @redhat27 beat me to it. :)
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top