How to stop array from terminal (not shutdown server)


Recommended Posts

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?

Link to comment

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 by Squid
  • Like 1
Link to comment

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 by DisplayNerd
Link to comment
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 by Squid
  • Like 1
Link to comment

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.

 

  • Like 1
Link to comment

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 by DisplayNerd
Link to comment

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.

Link to comment

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.

  • Like 1
Link to comment
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 by Squid
Link to comment

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 by DisplayNerd
Link to comment

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 by DisplayNerd
Link to comment

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

 

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.