Jump to content
Squid

[Plugin] CA User Scripts

1165 posts in this topic Last Reply

Recommended Posts

Hi!
 

Great little plugin, thanks a lot.

 

I have a question though: I want to start a script that runs in background after the disk array starts.

 

This is the content of the script:

 

#!/bin/bash
tail -F "/mnt/cache/appdata/plex/Library/Application Support/Plex Media Server/Logs/Plex Media Server.log" | \
while read line ; do
        echo "$line" | grep "authenticated user"
        if [ $? = 0 ]
        then    
        mdcmd spinup 0    
        mdcmd spinup 1    
        mdcmd spinup 2    
        sleep 30
        fi
done

what it does is it spins up the disks whenever somebody opens plex

 

obviously I want to keep the script running in the background when unraid boots up... is it ok to just start the script with the "At Startup of Array"- option? will it keep running in the background then?

 

thanks

 

MJFox

Share this post


Link to post
10 minutes ago, MJFOx said:

This is the content of the script:

Hopefully somebody else can answer your actual question, but as a side thought you could replace...

        mdcmd spinup 0    
        mdcmd spinup 1    
        mdcmd spinup 2

...with...

disks=$(ls /dev/md* | sed "sX/dev/mdXX");
for disknum in $disks; do
    /usr/local/sbin/mdcmd spinup $disknum;
done

I currently run this on a CRON schedule every 10 minutes between 19:00 and 23:00 to speed up Plex.  I'd therefore be interested in your question too, as it's more useful.

Share this post


Link to post
2 hours ago, Cessquill said:

Hopefully somebody else can answer your actual question, but as a side thought you could replace...


        mdcmd spinup 0    
        mdcmd spinup 1    
        mdcmd spinup 2

...with...


disks=$(ls /dev/md* | sed "sX/dev/mdXX");
for disknum in $disks; do
    /usr/local/sbin/mdcmd spinup $disknum;
done

I currently run this on a CRON schedule every 10 minutes between 19:00 and 23:00 to speed up Plex.  I'd therefore be interested in your question too, as it's more useful.

thanks for your suggestion

 

I just tried and tested my script and it works well in the background, I just changed "tail -F " to "tail -n 1 -F " so my script only checks the last line of the log-file

 

give it a try

 

Cheers

 

MJFox

 

 

Share this post


Link to post
5 hours ago, MJFOx said:

is it ok to just start the script with the "At Startup of Array"- option? will it keep running in the background then?

Yes, because "tail -F" monitors file changes, it will run forever.

 

But I don't know if it could be killed somehow. Maybe it's better to set it to hourly and execute it atomic. I use this in my scripts:

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then
    exit 1
fi
trap 'rmdir "/tmp/${0///}"' EXIT

Explanation: This generates a dir in the temp folder, which is deleted when the script is aborted. If the dir exists it aborts the execution (which prevents executing it twice).

 

And I don't know if it would be a problem to spin up a drive, that is already spinning. Maybe we check the spin status first. I tried to find out which command returns such a status. The only one I found was this:

mdcmd status 1 | grep rdevLastIO

So the complete script would look like this:

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then
    exit 1
fi
trap 'rmdir "/tmp/${0///}"' EXIT
# spin up drives on plex user login
tail -F "/mnt/cache/appdata/plex/Library/Application Support/Plex Media Server/Logs/Plex Media Server.log" | \
while read line ; do
    echo "$line" | tail -n 1 -F | grep "authenticated user"
    if [ $? = 0 ]; then
        disk0_status=$(mdcmd status | grep "rdevLastIO.0=" | cut -d '=' -f 2 | tr -d '\n')
        if [[ $disk0_status == "0" ]]; then
            echo "Spin up disk 0"
            mdcmd spinup 0
        fi
        disk1_status=$(mdcmd status | grep "rdevLastIO.1=" | cut -d '=' -f 2 | tr -d '\n')
        if [[ $disk1_status == "0" ]]; then
            echo "Spin up disk 1"
            mdcmd spinup 1
        fi
        disk2_status=$(mdcmd status | grep "rdevLastIO.2=" | cut -d '=' -f 2 | tr -d '\n')
        if [[ $disk2_status == "0" ]]; then
            echo "Spin up disk 2"
            mdcmd spinup 2
        fi
    fi
done

I removed sleep as even if two users log in, it won't spinup twice as it checks the spinning status first.

 

P.S. If disk0 is your parity drive, you don't need to spin it up ;)

Edited by mgutt

Share this post


Link to post

I thought about it again. What do you think if we monitor the Plex Container CPU load instead? By that it would work without enabling debugging in Plex, which is needed by the above script. I disabled debugging to avoid permanent writes on my SSD.

 

EDIT: I created a version which monitors the load:

 

Edited by mgutt

Share this post


Link to post

I have a VERY basic find+cp script, which works in the web command line, but doesn't, when I want to run it with custom scripts (I just tried it manually)

Any ideas?

#!/bin/bash

find /mnt/user/mini/myfolder/ . -iname '*.mkv'

-exec cp -n {} /mnt/user/mini/myfolder \;

 

When I click Run Script, I get the find results, and "find: './sys/kernel/slab': Input/output error"

 

And the cp can't start with this error in the find 'results'.

 

What am I doing wrong?

Btw i just try to copy files from the subfolders to the main folder. the subfolders will be deleted by the DL client, when seeding is done.

Share this post


Link to post
5 hours ago, LSL1337 said:

When I click Run Script, I get the find results, and "find: './sys/kernel/slab': Input/output error"

I wonder that this script works in the terminal as you passed two paths: "mnt/user/mini/myfolder/" and ".". Simply remove the dot. It means "search everywhere starting at the root folder" and when it tries to find something in "/sys/kernel/slab" it will be blocked, which produces the error. Other examples:

https://stackoverflow.com/a/26234223/318765

 

Note: I would write the command in one line as "-exec" is part of the "find" command.

Share this post


Link to post

it worked with the ., but it was in 1 line in the web terminal.

 

I removed the dot and the extra line, now it seems to be working.

thanks!

I didn't know the different line makes a difference even, it was just more readable for me

Share this post


Link to post

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 ... failed (Network failure)
plugin: wget: https://raw.githubusercontent.com/Squidly271/user.scripts/master/plugins/user.scripts.plg download failure (Network failure)

Updating Support Links



Finished Installing. If the DONE button did not appear, then you will need to click the red X in the top right corner

how do i fix that ?

Share this post


Link to post

Looks like a network config problem.  Double check your DNS settings, Gateway, etc.

Share this post


Link to post

Updated today, now "RUN IN BACKGROUND" button does not work anymore. Popup window opens, but stays white and script is not executed.

Share this post


Link to post

I'm seeing the same issue  ^ , 2020.10.21.  White screen, no actual script execution when using "run in background".  I have not tested a scheduled task.r

Share this post


Link to post
10 hours ago, mgutt said:

Updated today, now "RUN IN BACKGROUND" button does not work anymore. Popup window opens, but stays white and script is not executed.

All the scripts are chmod to 600 which prevents them from being run.  Additionally, running the command "chmod 700 script" has no effect but also generates no error.

Share this post


Link to post
52 minutes ago, nerbonne said:

All the scripts are chmod to 600 which prevents them from being run.  Additionally, running the command "chmod 700 script" has no effect but also generates no error.

User Scripts does not directly execute the scripts, so permissions don't matter.  They are changed to be linux style line endings (so that a DOS editor will work on the files if you want), and then executed from /tmp with permissions of 0777.

 

But, yeah run in background isn't working for some reason.... Will look into it

Share this post


Link to post
7 hours ago, Squid said:

User Scripts does not directly execute the scripts, so permissions don't matter.  They are changed to be linux style line endings (so that a DOS editor will work on the files if you want), and then executed from /tmp with permissions of 0777.

 

But, yeah run in background isn't working for some reason.... Will look into it

This is the error message i get if i try to start a script running in the background:

 

Oct 22 20:58:35 Tower emhttpd: cmd: /usr/local/emhttp/plugins/user.scripts/showLog.php Out of Memory

Oct 22 20:58:35 Tower emhttpd: cmd: /usr/local/emhttp/plugins/user.scripts/showLog.php Out of Memory

 

Share this post


Link to post
On 10/21/2020 at 8:42 PM, mgutt said:

Updated today, now "RUN IN BACKGROUND" button does not work anymore. Popup window opens, but stays white and script is not executed.

Fixed

Share this post


Link to post

When running in background the "Abort script" button does work leaving many phantom processes still running without the ability to end them without detective work and the kill command. Is there a way to fix this?

Share this post


Link to post

I don't know why, but downloading one of my scripts log files killed the complete server (WebGUI dead, Samba service dead, no server HDMI output). At the moment the parity check is running. After it has been finished I will:

  • delete the log of the script
  • restart the script, so a fresh log file is generated
  • stop the array
  • copy the log file through terminal to my SSD
  • download the log file
  • download the log zip file through CA User Scripts and see if the error happens again

If I should check something else before the last step, post it ;)

 

At the moment I suspect the CPU undervolting, although it worked for a long time now.

Edited by mgutt

Share this post


Link to post

I have a script that is running great when I manually tell it to do so, or in the background but does not seem to be responding to my custom CRON schedule. I would like it to run every Thursday at 2am so I have set the CRON fiel to be 02**THU. The log however indicates a failure to parse CRON job and goes into a loop.

 

I saw that weekly is an option, but am unsure how to set the day and time of the weekly schedule. Can someone help?

Share this post


Link to post
9 minutes ago, Ryguy said:

I saw that weekly is an option, but am unsure how to set the day and time of the weekly schedule. Can someone help?

I have a cron job that runs every Monday at 1 am.  In User Scripts it is 0 1 * * 1.

 

Yours should be 0 2 * * 4  (perhaps THU also works but I use the ordinal number).  You need a space between each element as well.

Share this post


Link to post

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.