[Plugin] CA User Scripts


1320 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

Link to post
  • Replies 1.3k
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Just a simple little plugin to act as a front end for any little scripts that you may have that you may need to run every once in a while, and don't feel like dropping down to the command line to do i

I could make up something long, convoluted, and technical as a reason why it doesn't do that but it would all be bs.  The simple answer is that when I did this, I never thought of that, and why it sti

@Squid I tried many commands and by that I found out that pkill does not kill (all) child processes: root@Thoth:/tmp# pgrep -f isleep2m | xargs --no-run-if-empty ps fp PID TTY STAT T

Posted Images

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.

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

 

 

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
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
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.

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.

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 ?

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.

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

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

 

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
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?

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.

Link to post
12 minutes ago, Hoopster said:

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.

Thank you will give this a try. 

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.