DisplayNerd Posted April 15, 2018 Share Posted April 15, 2018 I've been wanting to make a script to put my server to sleep when not using it and want to stop the array after the drives have spun down (for extra security) and then put the server to sleep. I kind of know how to put it to sleep and find out if the drives have spun down as there's threads on that, but I can't find out how to stop the array through the terminal. Anyone can help me? Quote Link to comment
Squid Posted April 15, 2018 Share Posted April 15, 2018 (edited) You pretty much need a script nowadays because of csrf tokens This should work for you #!/usr/bin/php <? $vars = parse_ini_file("/var/local/emhttp/var.ini"); exec('wget -qO /dev/null "http://localhost:80/update.htm?cmdStop=Stop&csrf_token='.$vars['csrf_token'].'"'); ?> BTW, stopping the array automatically spins up the drives. Edited April 15, 2018 by Squid 1 Quote Link to comment
bonienl Posted April 15, 2018 Share Posted April 15, 2018 You can also install the Dynamix S3 sleep plugin, which allows the system to go to sleep or shutdown on predefined conditions. 1 Quote Link to comment
DisplayNerd Posted April 15, 2018 Author Share Posted April 15, 2018 (edited) Thanks Squid for your help, and thank you bonienl for the suggestion. I doubt the S3 sleep plugin is supported on my system as cat /sys/power/mem_sleep only prints s2idle and standby. Not deep as s3 sleep would give (and visual conformation, my server didn't turn off when I put it to sleep when I used it as an actual computer for a little while). I was thinking to write "standby" to /sys/power/mem_sleep and then write "mem" to /sys/power/state. P.S. Does anyone a way you could check the state of whether the array is fully stopped so I can sleep when the operation is complete? I don't really care if the drives spin up after the array stops, putting it in sleep spins them down again, I think. Edited April 16, 2018 by DisplayNerd Quote Link to comment
Squid Posted April 16, 2018 Share Posted April 16, 2018 (edited) 12 hours ago, DisplayNerd said: P.S. Does anyone a way you could check the state of whether the array is fully stopped Seeing if /mnt/user doesn't exist and then waiting another couple of seconds should be sufficient Edited April 16, 2018 by Squid 1 Quote Link to comment
itimpi Posted April 16, 2018 Share Posted April 16, 2018 1 hour ago, Squid said: Seeing if /mnt/user doesn't exist and then waiting another couple of seconds should be sufficient Or /mnt/diskX if there are no User Shares. 1 Quote Link to comment
DisplayNerd Posted April 16, 2018 Author Share Posted April 16, 2018 (edited) Thanks guys! Is 15 seconds good? So, just to be clear /mnt/user will disappear after the array stops. Right? Edited April 16, 2018 by DisplayNerd Quote Link to comment
bonienl Posted April 16, 2018 Share Posted April 16, 2018 If you want execution at the absolute correct point in time, you better make use of the built-in event system of unRAID. Copy your script to the folder /usr/local/emhttp/plugins/dynamix/event/stopped and script execution begins as soon as the array is stopped. Two things to keep in mind: - The folder stopped does not exist by default and needs to be created. - These entries live in RAM, so folder and script need to be recreated each time the server reboots. 1 Quote Link to comment
Squid Posted April 16, 2018 Share Posted April 16, 2018 1 hour ago, DisplayNerd said: So, just to be clear /mnt/user will disappear after the array stops yes 1 Quote Link to comment
DisplayNerd Posted April 16, 2018 Author Share Posted April 16, 2018 (edited) Thanks squid and bonienl. So I think what would be best would be to make a script to stop the array when the disks spin down using squid's PHP. Then make another script that uses the event system in unraid bonienl suggested to put the array in sleep. Is there an unraid event state that the disks have spun down as well? Edited April 16, 2018 by DisplayNerd Quote Link to comment
Squid Posted April 16, 2018 Share Posted April 16, 2018 There is a downside though to using the event, depending upon how you do all this. If you want to actually stop the array (change global share settings, replace a drive, etc), then that event is going to run which will wind up sleeping the server. Quote Link to comment
DisplayNerd Posted April 16, 2018 Author Share Posted April 16, 2018 Yeah. Should've thought about that Quote Link to comment
bonienl Posted April 16, 2018 Share Posted April 16, 2018 The most common approach is to add a copy statement in your go file before starting emhttp. Assume you have the folder "scripts" on your flash device, then add the following to the go file mkdir -p /usr/local/emhttp/plugins/dynamix/event/stopped cp /boot/scripts/my_script /usr/local/emhttp/plugins/dynamix/event/stopped There are no events for all disks spun up or spun down. To add on squid's comment: yes the script is executed each time the array is stopped, so it needs to have intelligence before activating sleep. The upside of this method is that no dependency on timers or checking for the presence of a mount is required. This would prevent unwanted side effects when for example it takes longer to stop the array than the script expects. 1 Quote Link to comment
Squid Posted April 16, 2018 Share Posted April 16, 2018 (edited) 9 minutes ago, bonienl said: mkdir -p /usr/local/emhttp/plugins/dynamix/event/stopped shouldn't it be mkdir -p /usr/local/emhttp/plugins/dynamix/event "stopped" isn't a folder. Its the executable script that's run (unless there's been a big change to the event system and I missed the memo somewhere) Edited April 16, 2018 by Squid Quote Link to comment
bonienl Posted April 16, 2018 Share Posted April 16, 2018 You must have missed the memo Quote Link to comment
DisplayNerd Posted April 16, 2018 Author Share Posted April 16, 2018 (edited) Correct me if im wrong (i don't know any PHP but i have programmed before) but it seems Squid's script for stopping down the array shuts it down as if you clicked the stop button from the browser. I would assume you'd have to pass data (like a boolean value saved in a file whether the stop script was executed or not) from one script to the next if using the event system would be the path you took. And as I've said before, I don't know any PHP (though this does seem like a pretty simple feat). I'll see if i can find any vids that teach you if statements and saving flies in PHP. P.S. Where would be a good place to store one such file? Somewhere in /tmp I presume. Can you do this without saving a file? Edited April 16, 2018 by DisplayNerd Quote Link to comment
DisplayNerd Posted April 16, 2018 Author Share Posted April 16, 2018 (edited) Just learned a little PHP. If the end result can take fewer lines or there's errors in my script, please feel free to edit it. I'm a total noob. Add on to Squid's script: $file = "file location"; $handle = fopen($file, "w"); fwrite($handle, "Execute"); fclose($handle); Script to check if executed by user or sleep script after array stop: #!/usr/bin/php <? $file = "file location"; $output = file($file); $start = FALSE; foreach($output as $line) { if(strcmp($line, "Execute") == 0) { $start = TRUE; } } if($start == TRUE) { $handle = fopen($file, "w"); fwrite($handle, "DontExecute"); fclose($handle); exec("bash ./[Location of script]/sleepscript.sh"); } ?> Still don't know best place to save file. Edited April 16, 2018 by DisplayNerd Quote Link to comment
Squid Posted April 16, 2018 Share Posted April 16, 2018 Without thinking too much, add to the start of the original script file_put_contents("/tmp/sleepFlag","execute"); And the other script would be #!/usr/bin/php <? if ( is_file("/tmp/sleepFlag") ) { unlink("/tmp/sleepFlag"); exec("/path/to/sleepScript.sh"); } ?> Simpler. But yours will also work. Or 2nd script in bash would be #!/bin/bash if [[ -f /tmp/sleepFlag ]] then rm /tmp/sleepFlag /path/to/sleepScript.sh fi Quote Link to comment
DisplayNerd Posted April 17, 2018 Author Share Posted April 17, 2018 (edited) Thanks everyone for the help. EDIT: Stopping the array pauses the script until the array stops, so there is no script needed to check for the array to stop! Edited April 22, 2018 by DisplayNerd Stopping the array pauses the script until finished Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.