Jump to content
DisplayNerd

How to stop array from terminal (not shutdown server)

19 posts in this topic Last Reply

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?

Share this post


Link to post
Posted (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 by Squid
  • Like 1

Share this post


Link to post

You can also install the Dynamix S3 sleep plugin, which allows the system to go to sleep or shutdown on predefined conditions.

  • Like 1

Share this post


Link to post
Posted (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 by DisplayNerd

Share this post


Link to post
Posted (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 by Squid
  • Like 1

Share this post


Link to post
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.

  • Like 1

Share this post


Link to post
Posted (edited)

Thanks guys! Is 15 seconds good? So, just to be clear /mnt/user will disappear after the array stops. Right?

Edited by DisplayNerd

Share this post


Link to post

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

Share this post


Link to post
1 hour ago, DisplayNerd said:

So, just to be clear /mnt/user will disappear after the array stops

yes

  • Like 1

Share this post


Link to post
Posted (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 by DisplayNerd

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post
Posted (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 by Squid

Share this post


Link to post

You must have missed the memo :D

 

Share this post


Link to post
Posted (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 by DisplayNerd

Share this post


Link to post
Posted (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 by DisplayNerd

Share this post


Link to post

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

 

Share this post


Link to post
Posted (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 by DisplayNerd
Stopping the array pauses the script until finished

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now