[Plugin] CA User Scripts


Recommended Posts

lol  sure right when I finish the easy version.  Gotta eat and spend time with the wife.  I'll get back to it in a bit

 

Thanks! No rush, I'll try this one tomorrow.

BTW, have you checked if mover has nothing to move that it doesn't spin up any of the disks?
Link to comment

lol  sure.. catch me as I'm doing the numReads / Writes version.  Gimme a couple min.  Assuming you want to exclude cache drive

 

If you're doing the other one I would still prefer it, avoids the server being on for an extra 15 minutes unnecessarily, the least you can set to spin down.

Try this:

 

Your script that'll call mine:

 

/usr/local/sbin/mover
/sbin/fstrim -v /mnt/cache
/boot/config/plugins/user.scripts/scripts/johnny_script/checkSleep.php 180
if [ $? -eq 0 ]
then
  /usr/local/sbin/powerdown
else
  echo "Drive activity still happening"
fi

 

And my script (takes the sleep time as an argument.  Sleep time has to be larger than tuneable (poll_attributes)

 

#!/usr/bin/php
<?PHP
function startsWith($haystack, $needle) {
  return $needle === "" || strripos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

function getReadWrites($disks) {
  foreach ($disks as $disk) {
    if ( startsWith($disk['name'],"cache") ) { continue; } # delete this line if you want rw to cache to count
    if ( $disk['name'] == "flash" ) { continue; }
    $readWrites = $readWrites + $disk['numReads'] + $disk['numWrites'];
  }
  return $readWrites;
}

$sleepTime = $argv[1];

$initial = parse_ini_file("/var/local/emhttp/disks.ini",true);
$initialRW = getReadWrites($initial);
sleep($sleepTime);
$final = parse_ini_file("/var/local/emhttp/disks.ini",true);
$finalRW = getReadWrites($final);

if ( $initialRW == $finalRW ) {
  exit(0);
} else { 
  exit(1);
}

 

Link to comment

Would it be possible to integrate this with Powerdown Package and specifically run scripts from shutdown / startup events. I'd like to run a script at startup instead of a cron schedule. So basically i need the script to be inside /boot/config/plugins/powerdown/rc.unRAID.d/

But if you already have the power down plugin, why is the script plugin involved?

Not that big a deal to handle (and then you basically will have a GUI to handle something you would normally have to do via the command line to do it with the powerdown functions)

 

Will add  in array start and array stop to the frequency options.  Should take about 10 minutes for me tonight....

 

Exactly. This script is about not having to touch the command line.

 

Sounds good, will try it out when it's updated :-)

Updated
Link to comment

Try this:

 

Your script that'll call mine:

 

/usr/local/sbin/mover
/sbin/fstrim -v /mnt/cache
/boot/config/plugins/user.scripts/scripts/johnny_script/checkSleep.php 180
if [ $? -eq 0 ]
then
  /usr/local/sbin/powerdown
else
  echo "Drive activity still happening"
fi

 

Working great, many thanks!

 

Would it be easy to have the sleep repeat in case of drive activity, i.e., call the sleep script again and wait another xx seconds, until there's no activity and shutdown.

 

Link to comment

Two arguments: first is sleep time, second is # of iterations to loop through before giving up.

 

#!/usr/bin/php
<?PHP
function startsWith($haystack, $needle) {
  return $needle === "" || strripos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

function getReadWrites($disks) {
  foreach ($disks as $disk) {
    if ( startsWith($disk['name'],"cache") ) { continue; } # delete if you want cache to count
    if ( $disk['name'] == "flash" ) { continue; }
    $readWrites = $readWrites + $disk['numReads'] + $disk['numWrites'];
  }
  return $readWrites;
}

$sleepTime = $argv[1];
$iterations = $argv[2];

for ( $i=0; $i<$iterations; $i++) {
  $initial = parse_ini_file("/var/local/emhttp/disks.ini",true);
  $initialRW = getReadWrites($initial);
  sleep($sleepTime);
  $final = parse_ini_file("/var/local/emhttp/disks.ini",true);
  $finalRW = getReadWrites($final);

  if ( $initialRW == $finalRW ) {
    exit(0);
  } 
}
exit(1);
?>

Link to comment

Would it be possible to integrate this with Powerdown Package and specifically run scripts from shutdown / startup events. I'd like to run a script at startup instead of a cron schedule. So basically i need the script to be inside /boot/config/plugins/powerdown/rc.unRAID.d/

But if you already have the power down plugin, why is the script plugin involved?

Not that big a deal to handle (and then you basically will have a GUI to handle something you would normally have to do via the command line to do it with the powerdown functions)

 

Will add  in array start and array stop to the frequency options.  Should take about 10 minutes for me tonight....

 

Exactly. This script is about not having to touch the command line.

 

Sounds good, will try it out when it's updated :-)

Ahh... You're one of those guys ;)

So you must miss 6.0B6 with no dockerMan, and strictly docker run commands then

What? There is something else than docker run commands?

Link to comment

great plugin, very useful, I just set up a schedule to back up the listing of my media folders.

 

would it be possible to add a script title and the actual script name to the left?

done.  Gotta love the one liners  ;D

Wow! That easy?!

It looks great, thanks!

 

Sent from my SM-G901F using Tapatalk

 

 

Link to comment
  • 3 weeks later...

Well, I've created my first script for this, and overall the experience has been both nice - and frustrating.  The frustrating part is mainly fighting bash syntax!

 

I've created the Clear an array drive script, for use in removing drives from unRAID without losing parity.  I created it first to run standalone, then adapted it to work within User Scripts.  Some comments -

 

* Really needed a way to interact with the user, but developed ideas, kludges to work around that lack.  I suppose some might say I should have kept it for standalone use only, where I could possibly interact more, but I wanted this to be useful for new users, with no command line usage necessary.  Perhaps some day, you'll figure out ways to add a bit of interactivity?  I don't know enough to help, yet.

 

* I was a little disappointed to discover that the script display box won't display a line until you terminate it, with a newline.  I like to keep the display clean, fewer lines, and had hoped to display a bit of progress info on the same line, using \r to overwrite the current line.  But it won't display until you go to the next line.  I doubt there's anything you can do about that, so not your fault.  I've experienced this in other 'venues' too.  It would have been nice for the clearing progress though, where I could keep updating the same spot with the percentage complete.

 

* This script necessarily runs in the foreground.  I suppose it could run in the background, but not sure it should.  I suspect there are some scripts that are always best in one or the other but not both.  A low priority suggestion - utilize user.scripts variable(s) in the early comments.  For example, "# user.scripts.foreground=off", which would mean only the 'Run in background' button would appear.  For my script, I would put "# user.scripts.background=off", which would mean no 'Run in background' button.

 

* Running in the foreground of course, it hangs the browser tab, for the entire time it's clearing, which is hours!  No help for that, I do need to show some stuff to the user, and allow them to either proceed or cancel.  I don't think there's any way to do that in the background.

 

* In the script display box, are bold or colors possible?  It doesn't accept bash shell escaped color codes.

 

* Might be nice to add a "Add user script" button, which would check the root of the flash for a zip file with a folder that had 'description' and 'script' in it, and installed the folder in the right place.  It would make it easier for less-technical users (like those that might use my script) to install a script (just download the one they want to the flash and click this button).  The tool zipinfo is built-in, and using zipinfo -1 - it gives you a simple and clean string blob of the zip contents.

 

But I'm sure you have lots of other things to work on!    ;)

Link to comment

* Really needed a way to interact with the user, but developed ideas, kludges to work around that lack.  I suppose some might say I should have kept it for standalone use only, where I could possibly interact more, but I wanted this to be useful for new users, with no command line usage necessary.  Perhaps some day, you'll figure out ways to add a bit of interactivity?  I don't know enough to help, yet.

Maybe some day

 

* I was a little disappointed to discover that the script display box won't display a line until you terminate it, with a newline.  I like to keep the display clean, fewer lines, and had hoped to display a bit of progress info on the same line, using \r to overwrite the current line.  But it won't display until you go to the next line.  I doubt there's anything you can do about that, so not your fault.  I've experienced this in other 'venues' too.  It would have been nice for the clearing progress though, where I could keep updating the same spot with the percentage complete.

With the way that I'm doing it, not possible.  Not saying that its impossible, but right now its not.  The problem is that an output from a script in the first line could possibly affect the display of the 1000th line that gets outputted (and vice versa).  The easiest solution is actually to setup a putty shortcut that logs in an executes a command automatically.  Not great for noobs, but that way there is a full terminal emulation available.

* This script necessarily runs in the foreground.  I suppose it could run in the background, but not sure it should.  I suspect there are some scripts that are always best in one or the other but not both.  A low priority suggestion - utilize user.scripts variable(s) in the early comments.  For example, "# user.scripts.foreground=off", which would mean only the 'Run in background' button would appear.  For my script, I would put "# user.scripts.background=off", which would mean no 'Run in background' button.

I can see something like that being implemented.  I'll add it to the white board.

* Running in the foreground of course, it hangs the browser tab, for the entire time it's clearing, which is hours!  No help for that, I do need to show some stuff to the user, and allow them to either proceed or cancel.  I don't think there's any way to do that in the background.

Hangs more than the browser tab.  Might also completely hang emhttp due to the way that its designed (Never checked on a very long running script if this happens or not).  A long time ago Tom mused about switching emhttp to something like Apache which would presumably solve this problem (which affects everything not just user.scripts) but its probably safe to assume that that idea of his has long been forgotten.

* In the script display box, are bold or colors possible?  It doesn't accept bash shell escaped color codes.

Finally I can answer yes to something  :D  You are able to change fonts, colors, and play around quite a bit.  The box will accept any HTML code.  Things like <font color='red' size='2'> or <b> or <em>, etc.  You can also technically add javascript and buttons into it also.*  Not quite what you are used to (and really only makes sense if the script runs foreground only, as if you run background and then read the log in a text editor it won't look right, but the options are there.

 

* This would be one way to get around the interactive limitation, by adding appropriate javascript and doing everything in PHP instead of bash (user scripts fully supports other interpreters).  For an idea of what is possible, load up CA, go to installed apps, and click the icon for a running docker app.  You'll get a continuously updated memory and cpu usage on it.  (source is /usr/local/emhttp/plugins/community.applications/scripts/showDesc.php and showDescExec.php).  But even without the javascript, that popup still show how you can play around with using html tables, etc in the output display.

 

* Might be nice to add a "Add user script" button, which would check the root of the flash for a zip file with a folder that had 'description' and 'script' in it, and installed the folder in the right place.  It would make it easier for less-technical users (like those that might use my script) to install a script (just download the one they want to the flash and click this button).  The tool zipinfo is built-in, and using zipinfo -1 - it gives you a simple and clean string blob of the zip contents.

ok.  I'll investigate...

 

Link to comment

When you schedule scripts to be run, how are they scheduled? I expected through crontab, but when i show all cron jobs with "crontab -l" i don't see it.

They are part of the /usr/bin/run-parts /etc/cron.daily (as an example)

 

Install the dynamix scheduler plugin to adjust the actual time of execution (once installed, you'll see under settings - scheduler - fixed schedule a user.script.start.daily.sh (as an example) that in turn executes any scripts that are scheduled to start daily

 

Link to comment

great plugin, very useful, I just set up a schedule to back up the listing of my media folders.

 

would it be possible to add a script title and the actual script name to the left?

 

Hi dheg... Is there any chance you could share your script for the listing of your media folders? I really want to have something like that.

 

Thanks,

 

H.

Link to comment

great plugin, very useful, I just set up a schedule to back up the listing of my media folders.

 

would it be possible to add a script title and the actual script name to the left?

 

Hi dheg... Is there any chance you could share your script for the listing of your media folders? I really want to have something like that.

 

Thanks,

 

H.

Sure, how do I do that?

 

Sent from my SM-G901F using Tapatalk

 

 

Link to comment

Started using this to try it out so I could get a UI for Cron, as well as a way to run scripts one-off if I wanted.

 

I wish the folder structure to add scripts wasn't like the way it was with a sub-folder and 2 separate files.  I think a better approach would be to just use a single file that had comment markup within the scripts that provided the description that the plugin used.  IOW, I'd like to have a script called /boot/config/plugins/user.scripts/scripts/delete_dangling_images.sh instead of /boot/config/plugins/user.scripts/scripts/delete_dangling_images/script

 

Benefit is that the script purpose is easier with the filename, rather than a name of "script" and needing to rely on the folder it's in to understand context (or looking at the script itself).  I think it would reduce friction in adding new scripts too.

 

Question about scheduling: how does the scheduling actually work?  When I choose "Daily" or "weekly" there doesn't seem to be options for what time or day of week.  Does it just choose the same time to run all scripts or is there something else going on that I'm missing?

Link to comment

great plugin, very useful, I just set up a schedule to back up the listing of my media folders.

 

would it be possible to add a script title and the actual script name to the left?

 

Hi dheg... Is there any chance you could share your script for the listing of your media folders? I really want to have something like that.

 

Thanks,

 

H.

Sure, how do I do that?

 

Sent from my SM-G901F using Tapatalk

 

Just open the script file in text editor and copy all the text into a new message in the thread wgstarks mentioned above. Provide a brief description of what it does so others can benefit as well.

 

Many thanks,

 

:)

H.

Link to comment

Question about scheduling: how does the scheduling actually work?  When I choose "Daily" or "weekly" there doesn't seem to be options for what time or day of week.  Does it just choose the same time to run all scripts or is there something else going on that I'm missing?

These are standard cron schedules rather than a custom schedule for each script. The standard cron schedules can be adjusted in Settings - Scheduler - Fixed Schedules.
Link to comment

great plugin, very useful, I just set up a schedule to back up the listing of my media folders.

 

would it be possible to add a script title and the actual script name to the left?

 

Hi dheg... Is there any chance you could share your script for the listing of your media folders? I really want to have something like that.

 

Thanks,

 

H.

 

Here it is: https://lime-technology.com/forum/index.php?topic=50416.msg495777#new

Enjoy it  ;)!

 

Link to comment

 

To add your own scripts:

 

Within the flash drive folder config/plugins/user.scripts/scripts create a new folder (each script is going to have its own folder) - The name doesn't matter but it can only contain the following characters: letters ([A-Za-z]), digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), periods ("."), and spaces (" ")

 

I haven't had any luck making a directory with spaces in the name.

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.