[Plugin] CA User Scripts


Recommended Posts

On 2/10/2021 at 7:51 AM, TexasUnraid said:

Does anyone know of a way to setup a button in the GUI to 1-click run a script?

 

Custom Tabs does not seem to support this but something along those lines is what I am looking for.

 

Looking for a way to easily switch between power saving mode (which the server stays in 95% of the time) and performance (only when having it render or do something intensive).

 

Also be nice for some other uses as well such as a sync script I have, or snapshots etc

 

You would need to do this:

 

Create the file called /usr/local/emhttp/plugins/myscript/script.page

Menu="Tasks:80"
Name="My Script"
Type="xmenu"
Tabs="true"
Code="e942"

You can move where the tab is located by adjusting the number within "Menu" as per the tab numbering here: https://forums.unraid.net/topic/57109-plugin-custom-tab/

 

 

 

Create another file called /usr/local/emhttp/plugins/myscript/myscript.page

Menu="script"
---
<?
echo "<h1>Running script</h1><br><br>";

passthru("/bin/bash /boot/myscript.sh");
?>

Change the bash command appropriately within "passthru"

 

Note: The page will keep loading while the script executes.  If you're planning on doing something like starting a long running script (rsync?) by doing this, then the script that you call has to start another script via the "AT" command in bash.

 

The screen does not update as the script executes, but will only be updated once the script is finished.

 

If after starting the script you want the GUI to go back to where ever you were at prior to clicking the custom tab, then add to the second file

<script>history.back();</script>

 

ALL of these files need to be saved with standard linux line endings (use Notepad++)

 

Copy the files to the folder in RAM via the userscripts plugin (or go file) at first boot

  • Thanks 1
Link to comment
On 2/15/2021 at 7:17 AM, Squid said:

 

You would need to do this:

 

Create the file called /usr/local/emhttp/plugins/myscript/script.page



Menu="Tasks:80"
Name="My Script"
Type="xmenu"
Tabs="true"
Code="e942"

You can move where the tab is located by adjusting the number within "Menu" as per the tab numbering here: https://forums.unraid.net/topic/57109-plugin-custom-tab/

 

 

 

Create another file called /usr/local/emhttp/plugins/myscript/myscript.page



Menu="script"
---
<?
echo "<h1>Running script</h1><br><br>";

passthru("/bin/bash /boot/myscript.sh");
?>

Change the bash command appropriately within "passthru"

 

Note: The page will keep loading while the script executes.  If you're planning on doing something like starting a long running script (rsync?) by doing this, then the script that you call has to start another script via the "AT" command in bash.

 

The screen does not update as the script executes, but will only be updated once the script is finished.

 

If after starting the script you want the GUI to go back to where ever you were at prior to clicking the custom tab, then add to the second file



<script>history.back();</script>

 

ALL of these files need to be saved with standard linux line endings (use Notepad++)

 

Copy the files to the folder in RAM via the userscripts plugin (or go file) at first boot

 

Perfect! That sounds like just what I was looking for.

 

I do have an rsync script I was wanting to run like this, I will add the command you listed to see if that works without interfering with the GUI.

 

Is it possible to run it the same way user scripts works with the "run in background" option? Aka, it will keep the log in user scripts for later reference?

 

Or possibly open the script in a new window with the output visible like running from user scripts? I can make it work without logging, just reduces the uses a bit.

 

Having power outages right now due to weather so the server is shut down but will give it a try when I turn it back on.

Edited by TexasUnraid
Link to comment
On 2/15/2021 at 7:17 AM, Squid said:

 

You would need to do this:

 

Create the file called /usr/local/emhttp/plugins/myscript/script.page



Menu="Tasks:80"
Name="My Script"
Type="xmenu"
Tabs="true"
Code="e942"

You can move where the tab is located by adjusting the number within "Menu" as per the tab numbering here: https://forums.unraid.net/topic/57109-plugin-custom-tab/

 

 

 

Create another file called /usr/local/emhttp/plugins/myscript/myscript.page



Menu="script"
---
<?
echo "<h1>Running script</h1><br><br>";

passthru("/bin/bash /boot/myscript.sh");
?>

Change the bash command appropriately within "passthru"

 

Note: The page will keep loading while the script executes.  If you're planning on doing something like starting a long running script (rsync?) by doing this, then the script that you call has to start another script via the "AT" command in bash.

 

The screen does not update as the script executes, but will only be updated once the script is finished.

 

If after starting the script you want the GUI to go back to where ever you were at prior to clicking the custom tab, then add to the second file



<script>history.back();</script>

 

ALL of these files need to be saved with standard linux line endings (use Notepad++)

 

Copy the files to the folder in RAM via the userscripts plugin (or go file) at first boot

 

Ok, finally got the server back up and running so setting this up.

 

I am FAR from an expert but doesn't /usr/local/emhttp/plugins/ get wiped on reboot?

 

I thought the only persistent folder was /boot?

 

could I put the files in /boot/config/plugins instead?

 

Partly asking about this situation but also for future information.

Edited by TexasUnraid
Link to comment
29 minutes ago, TexasUnraid said:

could I put the files in /boot/config/plugins instead?

 

Won't work

29 minutes ago, TexasUnraid said:

I am FAR from an expert but doesn't /usr/local/emhttp/plugins/ get wiped on reboot?

 

Yes

 

On 2/15/2021 at 8:17 AM, Squid said:

Copy the files to the folder in RAM via the userscripts plugin (or go file) at first boot

 

  • Thanks 1
Link to comment
22 minutes ago, Squid said:

Won't work

Yes

 

 

Ok, was not sure if there was an easier way.

 

Is it possible to put a sleep command into the .page file? I would like for it to pause for ~2 seconds after running so I can see what state the CPU was switched to. I tried putting the sleep a few places but it caused an error.

 

Otherwise it seems to be working great!

Link to comment

Just in case someone finds this on google later, figured I would toss up my script for changing the CPU governer using the above settings.

 

I store the script.page files in a sub-folder of the user-script to keep things tidy.

 

/boot/config/plugins/user.scripts/scripts/Power\ Governer\ switcher/cpu-power-switch

 

then I have a script that runs at array start that copies the files to the right location

 

cp -r /boot/config/plugins/user.scripts/scripts/Power\ Governer\ switcher/cpu-power-switch /usr/local/emhttp/plugins/

 

The script itself is really simple, you need Tips & tweaks plugin installed for it to work

 

#!/bin/bash
#description=This script switches the CPU Governer between Powersave and Performance (schedutil)
#backgroundOnly=
#arrayStarted=
#noParity=
#argumentDescription=
#argumentDefault=

#set -x

GOVERNER=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

if [ "$GOVERNER" = powersave ]
  then
    /usr/local/sbin/rc.tweaks set_governor schedutil
    echo "CPU is being changed to Performance Mode"
  else
    /usr/local/sbin/rc.tweaks set_governor powersave
    echo "CPU is being Changed to PowerSave Mode"
fi

echo "The CPU Governer is now set to $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)"

/usr/local/emhttp/webGui/scripts/notify -i normal -e "New CPU Governer" -s "The new CPU governer is set to" -d "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)"

 

It is working perfect, I can now change the governer with a single button. It saves 20-40w of power easily, much more under higher loads but also allows for full performance if I need it.

 

Also gives me ideas for other uses of this method.

 

Thanks for the help Squid!

Link to comment

I was working on the rsync script now and was curious, where are the background logs for user scripts stored? Would it be possible to send the output from the script directly into the log file to make viewing it later easy?

 

The syntax for this would be apprishiated as well, I always get confused by the > vs >> and it seems a half dozen other methods of sending the output to another place.

Link to comment
  • 2 weeks later...

How dose the "custom" tab work in schedule?

 

Like how would I make a script run every hour for example...

 

Also how would I add code to the script to make sure is is not running when it tries to execute?


 

if pgrep -x "rclone_Sync.sh" > /dev/null then

      #echo "Running"

      rclone sync /sourceDir remote:

   else

      #echo "Stopped"

fi

 

 

Link to comment

is my script correct? Will that do what I want it to.. simply run my rclone sync thing but if the script is already running... not run it.

 

Like the name of the file is

"/boot/config/plugins/user.scripts/scripts/rclone_sync/script" right?

 

so
 

if pgrep -x "/boot/config/plugins/user.scripts/scripts/rclone_sync/script" > /dev/null

   then

      #echo "Running"

      rclone sync /sourceDir remote:

   else

      #echo "Stopped"

fi

 

Edited by questionbot
Link to comment

Hello guys,

 

i've tried searching within this huge thread but I couldn't find a working answer. I want to call from one user script another user script. For this the following command was used.

echo "/boot/config/plugins/user.scripts/scripts/backup_onedrive/script" | at now

 

But I got following error

sh: line 18: /boot/config/plugins/user.scripts/scripts/backup_onedrive/script: Permission denied

 

I tried via the console to 

sudo chmod +x /boot/config/plugins/user.scripts/scripts/backup_onedrive/script

 

But the execute x flag is never applied.

 

Any tips or hints?

 

Thanks in advance

 

tschinz

Link to comment
53 minutes ago, tschinz said:

Hello guys,

 

i've tried searching within this huge thread but I couldn't find a working answer. I want to call from one user script another user script. For this the following command was used.


echo "/boot/config/plugins/user.scripts/scripts/backup_onedrive/script" | at now

 

But I got following error


sh: line 18: /boot/config/plugins/user.scripts/scripts/backup_onedrive/script: Permission denied

 

I tried via the console to 


sudo chmod +x /boot/config/plugins/user.scripts/scripts/backup_onedrive/script

 

But the execute x flag is never applied.

 

Any tips or hints?

 

Thanks in advance

 

tschinz

Put 'bash' in front of the command. Like 'bash /boot/config/plugins/user.scripts/scripts/backup_onedrive/script'

Link to comment
2 hours ago, ICDeadPpl said:

Put 'bash' in front of the command. Like 'bash /boot/config/plugins/user.scripts/scripts/backup_onedrive/script'

Thanks ICDeadPpl, that worked. I've in one main script multiple sub user scripts I would like to launch. Is it possible to wait for one command to finish before launching the next? Currently both are started at the same time because of the "at now" command. I there a method to launch the second after the first is finished.

 

Thanks for the help

Link to comment
25 minutes ago, tschinz said:

Thanks ICDeadPpl, that worked. I've in one main script multiple sub user scripts I would like to launch. Is it possible to wait for one command to finish before launching the next? Currently both are started at the same time because of the "at now" command. I there a method to launch the second after the first is finished.

 

Thanks for the help

just do everything in one script, dont think there is a easy way. you could save a text file in the sub-scripts and wait for the file to appear in the main script

Link to comment
3 hours ago, Marcel40625 said:

Hey there,

 

i would like to know how i can mail the whole output of

 


rsync -av --delete --ignore-errors -e

 

as notification

 

thanks in advance

You could call '/usr/local/emhttp/webGui/scripts/notify' from your script, and have the message include the output of the rsync job.
The rsync output could be temporarily stored as a file, piped argument, named pipe or whatever.

Run the command '/usr/local/emhttp/webGui/scripts/notify' once without any arguments to see which there are for you to use.

You may want to check the notifications in Unradid Web UI "Settings/Notifications settings" so they are set to send email.

Link to comment
3 hours ago, Marcel40625 said:

Hey there,

 

i would like to know how i can mail the whole output of

 


rsync -av --delete --ignore-errors -e

 

as notification

 

thanks in advance

with the at command you can get an email

command | at now

 

Link to comment
10 minutes ago, ICDeadPpl said:

You could call '/usr/local/emhttp/webGui/scripts/notify' from your script, and have the message include the output of the rsync job.
The rsync output could be temporarily stored as a file, piped argument, named pipe or whatever.

Run the command '/usr/local/emhttp/webGui/scripts/notify' once without any arguments to see which there are for you to use.

You may want to check the notifications in Unradid Web UI "Settings/Notifications settings" so they are set to send email.

 

yeah but how can i attach the file or output from rsync to the notify command

 

/usr/local/emhttp/webGui/scripts/notify -i normal -s "Test" -d "Test" -m < test.txt 

 

does not work the content of the file is not showing in notification

Link to comment
2 minutes ago, Marcel40625 said:

 

yeah but how can i attach the file or output from rsync to the notify command

 


/usr/local/emhttp/webGui/scripts/notify -i normal -s "Test" -d "Test" -m < test.txt 

 

does not work the content of the file is not showing in notification

Try
 

MESSAGE=`rsync -av --delete --ignore-errors -e`
/usr/local/emhttp/webGui/scripts/notify -i normal -s "Test" -d "Test" -m "$MESSAGE"

 

Link to comment
51 minutes ago, ICDeadPpl said:

Try
 


MESSAGE=`rsync -av --delete --ignore-errors -e`
/usr/local/emhttp/webGui/scripts/notify -i normal -s "Test" -d "Test" -m "$MESSAGE"

 

 

thanks, but found out tail works

 

/usr/local/emhttp/webGui/scripts/notify -i normal -s "Test" -d "Test" -m "`tail logfile.log`"

 

Link to comment
3 minutes ago, Marcel40625 said:

thanks, but found out tail works

 

One problem with all of this is that IF there's a double quote within the output of the tail then you'll either get weird results in the message, or the notify command itself will fail.

Link to comment
13 minutes ago, Squid said:

One problem with all of this is that IF there's a double quote within the output of the tail then you'll either get weird results in the message, or the notify command itself will fail.

thanks for the advice, but seems to work in this case :) and doesnt help ` ` for that?

Edited by Marcel40625
Link to comment

plugin: installing: https://raw.githubusercontent.com/Squidly271/user.scripts/master/plugins/user.scripts.plg
plugin: downloading https://raw.githubusercontent.com/Squidly271/user.scripts/master/plugins/user.scripts.plg
plugin: downloading: https://raw.githubusercontent.com/Squidly271/user.scripts/master/plugins/user.scripts.plg ... done

+==============================================================================
| Installing new package /boot/config/plugins/user.scripts/user.scripts-2021.03.10-x86_64-1.txz
+==============================================================================

Verifying package user.scripts-2021.03.10-x86_64-1.txz.
Unable to install /boot/config/plugins/user.scripts/user.scripts-2021.03.10-x86_64-1.txz: tar archive is corrupt (tar returned error code 2)





----------------------------------------------------
user.scripts has been installed.
Copyright 2016-2020, Andrew Zawadzki
Version: 2021.03.10
----------------------------------------------------

plugin: user.scripts.plg installed
 

what is the problem when I install this plugin...pls help, thx

what is the problem when I install this plugin...pls help, thx

what is the problem when I install this plugin...pls help, thx a lot

 

 

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.