[Plugin] CA User Scripts


Recommended Posts

On 10/22/2023 at 3:06 AM, hasown said:

 

Did you ever find a fix for this? I'm finding today that "on array start" scripts aren't running for me either. I'm not sure when the issue started since I don't often reboot my server, but I'm also on 6.12.4.


No, I personally stayed with my /boot/config/go solution because all that it does is create crontab entries - and that works fine the way I did it. I really don't have any need to wait for array start too ... however I do check in the individual cron jobs if the /mnt/user/[share] actually exists and cancel the execution if it doesnt.

Link to comment

Hi all,

 

in the german sub we are currently trying to share a rclone mount via SMB. When mounting rclone via Unraid console => "rclone mount --max-read-ahead 1024k --allow-other --vfs-cache-mode writes pdata: /mnt/disks/pcloud &" everthing is working as expected. When trying to mount rclone within a user script with the same code "rclone mount --max-read-ahead 1024k --allow-other --vfs-cache-mode writes pdata: /mnt/disks/pcloud &" the access via SMB to the shared folder is denied.

 

Does anyone have an idea, at which point the user scripts are handled different than via console?

 

Thanks for your feedback.

Link to comment

Morning guys (and ladies)

 

I created little python script that sends data to an arduino nano every second.

To make this work I took the following stept:

- Installed python3, python3 setup tools and pip from the nerdpack - no issues there

- Created the following directory for the python-script and pythons virtual enviroment: /mnt/user/appdata/python/cpuusage

- Inside I created the virtual environment with

python3 -m venv venv
source ./venv/bin/activate
pip install --upgrade pip
pip install psutil pyserial

- Then I copied the script to this directory and started it with python cpuusage-to-serial.py. All good. The display show how busy the server is and follows (h)top quite good.

 

Since I want this to run when the array starts (Its not available because the script is placed on the array - I thought I would not be a good idea to place it on the stick) I created a little user.script:

 

#!/bin/bash
#description=Sends cpu usage to arduino nano
#arrayStarted=true 
cd /mnt/user/appdata/python/cpuusage
. ./venv/bin/activate
python cpuusage-to-serial.py 2>&1

 

I started to script in the foreground and the script started and the display showed  what I expected. After setting the schedule to "At Startup Of Array" and hitting Apply I rebooted the array. After the array got back up the script got started and the display starts to show cpu usage - sweet.

 

But now it gets weird:

When I go to user.scripts and abort the script, unraid says "aborted" but the script is still running in the background. Also. whenever I stop the script using user.scripts or let it run. When I want to stop the array I get this message in the log:

 

Oct 27 09:10:47 towerpve root: rmdir: failed to remove '/mnt/user': Device or resource busy
Oct 27 09:10:47 towerpve emhttpd: shcmd (279462): exit status: 1
Oct 27 09:10:47 towerpve emhttpd: shcmd (279464): /usr/local/sbin/update_cron
Oct 27 09:10:47 towerpve emhttpd: Retry unmounting user share(s)...
Oct 27 09:10:52 towerpve emhttpd: shcmd (279466): /usr/sbin/zfs unmount -a
Oct 27 09:10:52 towerpve emhttpd: shcmd (279467): umount /mnt/user
Oct 27 09:10:52 towerpve root: umount: /mnt/user: target is busy.
Oct 27 09:10:52 towerpve emhttpd: shcmd (279467): exit status: 32
Oct 27 09:10:52 towerpve emhttpd: shcmd (279468): rmdir /mnt/user

 

When I kill /mnt/user/appdata/python/cpuusage/cpuusage-to-serial.py the array continues the stopping procedure.

 

I think I need to make this user.script thingy a true daemon but I have no idea how and probably can't because everything is on the stick and having a entire python venv on it is imho not a good idea. I have no idea where to place the script (well except on /boot/config/plugins/user.scripts/scripts but that is rather limited since it only can execute "script" and I though it is not even executed from there.

And why does the script not abort like it should?

 

This is the "heart" of the script - I left out all helper methods since they do not matter:

 

def get_cpu_usage():
    threading.Timer(SPEED, get_cpu_usage).start()
    serial_data = []
    pct_per_core = psutil.cpu_percent(percpu=True)
    for core, pct in enumerate(pct_per_core):
        if core < CORES:
            level = pct2bar(pct)
            serial_data.append(str(level))
    send_data(f"<{';'.join(serial_data)}>")

get_cpu_usage()

 

On my test rig I created a systemd unit-file and works a charm but that's not unraid, not even slack.

Any thoughts how to make this work the proper way? I'm not even sure I need to use user.scripts.

Edited by sjoerd
Link to comment
On 10/22/2023 at 12:17 AM, alturismo said:

actually personally i cant confirm this, here i also user scripts on array start and they sure work

 

samples which i would directly be aware off if not ...

image.thumb.png.a98189351b38e2b6ad841b036d058229.png

 

image.thumb.png.1ff1490b44d2c0a0c6fca08bf15d1c0a.png

 

also on 6.12.4

image.thumb.png.0dee2ec24b77e837afaf49ce888bc0de.png

 

did you check your logs if there are any fails ?

 

I restarted today to install a gfx driver. The backgrounding scripts during server restart redirect to /dev/null so no logs, but I found these when I tried to manually background a script:

Nov  1 14:49:10 unRAID emhttpd: cmd: /usr/local/emhttp/plugins/user.scripts/backgroundScript.sh /tmp/user.scripts/tmpScripts/jellyfin_mergerfs/script
Nov  1 14:49:10 unRAID atd[3975]: PAM unable to dlopen(/lib64/security/pam_unix.so): /lib64/libc.so.6: version `GLIBC_2.38' not found (required by /lib64/libcrypt.so.1)
Nov  1 14:49:10 unRAID atd[3975]: PAM adding faulty module: /lib64/security/pam_unix.so
Nov  1 14:49:10 unRAID atd[3975]: Module is unknown
Nov  1 14:49:16 unRAID emhttpd: cmd: /usr/local/emhttp/plugins/user.scripts/backgroundScript.sh /tmp/user.scripts/tmpScripts/zsh/script
Nov  1 14:49:16 unRAID atd[4052]: PAM unable to dlopen(/lib64/security/pam_unix.so): /lib64/libc.so.6: version `GLIBC_2.38' not found (required by /lib64/libcrypt.so.1)
Nov  1 14:49:16 unRAID atd[4052]: PAM adding faulty module: /lib64/security/pam_unix.so
Nov  1 14:49:16 unRAID atd[4052]: Module is unknown

 

Link to comment
1 hour ago, hasown said:

I restarted today to install a gfx driver. The backgrounding scripts during server restart redirect to /dev/null so no logs, but I found these when I tried to manually background a script:

 

1/ you did check if your script is working running nativ from terminal ? install a gfx driver ?

2/ as you see it is actually executed, but failing ...

 

so in your case it looks like a different case i would say, at least what i see ...

Link to comment
On 11/2/2023 at 1:32 AM, alturismo said:

1/ you did check if your script is working running nativ from terminal ? install a gfx driver ?

2/ as you see it is actually executed, but failing ...

 

so in your case it looks like a different case i would say, at least what i see ...

1. The scripts work fine if I click "Run Script" in the UserScripts plugin options. For now, I have just been clicking "Run Script" on all these scripts manually when the server restarts. These are old scripts that I have not changed. Something in the plugin is not working. The graphics driver is unrelated. I just mentioned it because it was the reason I had to restart.

2. Only "Run Script" button works. I don't believe they're successfully running when in the background, like it does when the server restarts. The "Run in Background" button from UserScripts plugin options produces the log I shared: the script logline followed by several atd[3975] loglines.

Edited by hasown
Link to comment
On 10/22/2023 at 6:10 PM, itimpi said:

As far as I know they do.

 

You can control the start times to be exactly when you want using the Custom option.

 

Yeah thats true, but it still would be nice for example a global setting, when to run a daily job or for minimum to show (documentation) when the job will be executed as daily job.

 

An idea as another feature request, show in the GUI when the script was the last time executed, that would clarify the above problem. So the user can check when was the last time the script ran and you also know when a daily/monthly script will be executed.

Link to comment
  • 2 weeks later...

I am having trouble with a python script.

 

Error is :

/usr/local/emhttp/plugins/user.scripts/startScript.sh: line 6: /tmp/user.scripts/tmpScripts/Sync-Emby-Play-States/script: cannot execute: required file not found

 

The script is :

#!/bin/python3
python3 /mnt/user/appdata/syncseen/syncseen.py -shost="http://192.168.1.200:8096" -skey="xxxxxxxxxxxxxxxxxxx" -dhost="http://192.168.1.6:8096" -dkey="xxxxxxxxxxxxxxxxxxxxx"

 

Python 3 is installed with nerdtools, and which python3 shows ok : /usr/bin/python3

The .py file being called is there, and executable :

image.png.94f879dd66e1f904200f23aeb0aa60dd.png

 

I have been going around and around for a few hours on this!  My first time with python!

 

Please help - has to be something stupid.  I tried a cd bvefore the python3 comand also - no difference.

 

 

 

Link to comment
8 hours ago, vw-kombi said:

I am having trouble with a python script.

 

Error is :

/usr/local/emhttp/plugins/user.scripts/startScript.sh: line 6: /tmp/user.scripts/tmpScripts/Sync-Emby-Play-States/script: cannot execute: required file not found

 

The script is :

#!/bin/python3
python3 /mnt/user/appdata/syncseen/syncseen.py -shost="http://192.168.1.200:8096" -skey="xxxxxxxxxxxxxxxxxxx" -dhost="http://192.168.1.6:8096" -dkey="xxxxxxxxxxxxxxxxxxxxx"

 

Python 3 is installed with nerdtools, and which python3 shows ok : /usr/bin/python3

The .py file being called is there, and executable :

image.png.94f879dd66e1f904200f23aeb0aa60dd.png

 

I have been going around and around for a few hours on this!  My first time with python!

 

Please help - has to be something stupid.  I tried a cd bvefore the python3 comand also - no difference.

 

 

 

 Turns out I have a disk read error!  users shares not there - so it could not CD unto /mnt/user........  bnest I fix that and retry!

Link to comment

I wrote a daemon script that watches a share for changes via inotifywait, and I would like to suggest a few features that would really help in this usecase:

  • a config option to prevent two instances of a script running in parallel
  • Sending SIGTERM instead of SIGKILL when you click "Abort" (or offer this as a config option, or provide two buttons) because I want a simpler way to shut down the daemon. Currently, I use htop, I've thought about implementing sth with a pidfile, but it gets complicated fast. (Edit: SIGKILL also prevents the use of flock to achieve that only one instance can run at a time because the lockfile is not released.)
  • (Providing an option to) terminate running processes that have arrayStarted=true when the array is stopped (currently, I have to do this manually, because inotifywait blocks the unmounting).
Edited by xieve
Link to comment

HI and thanks for this!

 

I tryed making a script with this:

"#!/bin/bash
docker exec PhotoPrism photoprism index --cleanup"

 

But it dosent run?

 

I tryed running it and it run fine?

 

Log:

Script Starting Dec 07, 2023 21:47.01

Full logs for this script are available at /tmp/user.scripts/tmpScripts/PhotoPrism Index/log.txt

time="2023-12-07T21:47:03+01:00" level=info msg="Become a member today, support our mission and enjoy our member benefits! 💎"
time="2023-12-07T21:47:03+01:00" level=info msg="Visit https://www.photoprism.app/membership to learn more."
time="2023-12-07T21:47:03+01:00" level=info msg="indexing originals in /photoprism/originals"
time="2023-12-07T21:47:03+01:00" level=info msg="classify: loading nasnet"
time="2023-12-07T21:47:06+01:00" level=info msg="classify: loading labels from labels.txt"
time="2023-12-07T21:47:06+01:00" level=info msg="index: found no .ppignore file"
time="2023-12-07T21:47:06+01:00" level=info msg="metadata: 20230415_161418.jpg has time offset UTC+2 (exiftool)"
time="2023-12-07T21:47:06+01:00" level=info msg="metadata: 20230415_161418.jpg has time offset UTC+2 (exiftool)"
time="2023-12-07T21:47:07+01:00" level=info msg="metadata: 20230805_103958.jpg has time offset UTC+2 (exiftool)"
time="2023-12-07T21:47:07+01:00" level=info msg="metadata: 20230805_103958.jpg has time offset UTC+2 (exiftool)"
time="2023-12-07T21:47:07+01:00" level=info msg="media: created 11 thumbnails for 20230415_161418.jpg [971.776134ms]"
time="2023-12-07T21:47:08+01:00" level=info msg="faces: loading facenet"
time="2023-12-07T21:47:08+01:00" level=info msg="media: created 11 thumbnails for 20230805_103958.jpg [1.020408965s]"
time="2023-12-07T21:47:08+01:00" level=info msg="media: 20230805_103958.jpg was taken at 2023-08-05 10:39:59.053 +0000 UTC (meta)"
time="2023-12-07T21:47:09+01:00" level=info msg="index: found 1 face in 20230415_161418.jpg [1.279918588s]"
time="2023-12-07T21:47:09+01:00" level=info msg="media: 20230415_161418.jpg was taken at 2023-04-15 16:14:18.455 +0000 UTC (meta)"
time="2023-12-07T21:47:12+01:00" level=info msg="index: added main jpg file 20230805_103958.jpg"
time="2023-12-07T21:47:12+01:00" level=info msg="index: matched 2 labels with 20230415_161418.jpg [3.373451468s]"
time="2023-12-07T21:47:12+01:00" level=info msg="index: added main jpg file 20230415_161418.jpg"
time="2023-12-07T21:47:12+01:00" level=info msg="index: updated 2 files [8.609189675s]"
time="2023-12-07T21:47:12+01:00" level=info msg="cleanup: removed 0 index entries and deleted 0 sidecar files [354.405µs]"
time="2023-12-07T21:47:12+01:00" level=info msg="cleanup: searching for orphaned cache files"
time="2023-12-07T21:47:12+01:00" level=info msg="cleanup: deleted 19 files from cache [2.639866ms]"
time="2023-12-07T21:47:12+01:00" level=info msg="cleanup: deleted 19 files in total [4.660941ms]"
time="2023-12-07T21:47:12+01:00" level=info msg="indexed 7 files in 8.657699195s"
Script Finished Dec 07, 2023 21:47.12

Full logs for this script are available at /tmp/user.scripts/tmpScripts/PhotoPrism Index/log.txt

 

image.thumb.png.399772b30dfd2d9232d727b8a02239a2.png

Link to comment

Hi All,

 

just discovered this awesome plugin and messing around with automating stuff.

terribly sorry for being extremely noob but how do i find this folder:

/tmp/user.scripts/tmpScripts/backuptest/log.txt

 

I'm trying to access the logs generated by user scripts but i'm unable to find this "tmp" folder anywhere

I'm file browsing/navigating via unraid's  "main" interface or via krusader plugin.

MANY thanks in advance! 

Link to comment
1 hour ago, meekgeek said:

Hi All,

 

just discovered this awesome plugin and messing around with automating stuff.

terribly sorry for being extremely noob but how do i find this folder:

/tmp/user.scripts/tmpScripts/backuptest/log.txt

 

I'm trying to access the logs generated by user scripts but i'm unable to find this "tmp" folder anywhere

I'm file browsing/navigating via unraid's  "main" interface or via krusader plugin.

MANY thanks in advance! 

Neither of those will give you access to that level of the file system in their normal configuration.

 

You can do this from a terminal session.   At that level either use Linux commands, or us the ‘mc’ (Midnight commander) file manager.

Link to comment
11 minutes ago, itimpi said:

Neither of those will give you access to that level of the file system in their normal configuration.

 

You can do this from a terminal session.   At that level either use Linux commands, or us the ‘mc’ (Midnight commander) file manager.

ahh yess "mc" works fine! Thanks so much for the response! appreciate it!!

 

Link to comment

Not sure if this is the right place to ask - but has anyone successfully used this plugin to schedule shutdown of Ubiquiti devices (UDM, UNVR) upon power loss? I already have a UPS with Unraid shutdown enabled, but I saw a post from Squid from years ago suggesting that this plugin could then be used to shutdown other devices via SSH command. Would use the array stop as a trigger to send the command. Thanks!

Link to comment
On 10/26/2023 at 12:41 AM, alturismo said:

you should see your logs on the user scripts page

 

image.thumb.png.b9d3eeeca6301bb18ce2a1cd8cc30f9b.png

 

if you manually want to copy them over

 

they are located in Ram ...

image.thumb.png.8d79b2b94e34019e2700f828772f0993.png

 

so a cp ..../log.txt /mnt/user/whereever ... will copy them to a persistent storage.

 

So, are the user scripts processed by a script?

 

I would like to modify the processing so that instead of log.txt the file name would be something like...

user_script_name_timestamp.txt

 

This would allow the log to be downloaded or stored as a unique file.

 

Thanks.

Link to comment
3 hours ago, DWomack said:

So, are the user scripts processed by a script?

 

they are processed by the internal cron i would say, but about the logs ...

 

either webui, or if you want to process them manually

 

sample

 

image.png.495138b66dd675d82c24f3428f84579c.png

 

it is unique for each script, not for each run ... (which i personally abolutely wouldnt want ...)

Link to comment
8 hours ago, alturismo said:

they are processed by the internal cron i would say, but about the logs ...

 

either webui, or if you want to process them manually

 

sample

 

image.png.495138b66dd675d82c24f3428f84579c.png

 

it is unique for each script, not for each run ... (which i personally abolutely wouldnt want ...)

I understand that but what I would like would look like…

log.Encode.timestamp.txt

 

If I download log.txt from multiple scripts that run, the download directory winds up full of stuff like log.txt, log1.txt, log2.txt…. Names are not tied to the script they came from. 

 

 

Link to comment
  • 2 weeks later...

Hi everyone,

very nice plugin, I only have a problem with the 'Run in background' with scheduled hourly option. It starts, but stops after a few seconds. The log file says only script finished. The script itself is just a oneliner to sync a folder with rclone encrypted to onedrive.
When I click 'Run Script', it runs as expected and all files and folders are encrypted copied to onedrive.

Where is my mistake?
unraid 6.12.6

Thanks for your help.

Link to comment
  • 2 weeks later...

Love this plugin....  Thanks @Squid for all your ongoing efforts...

 

Maybe a script aficionado could help me modify this script?  I do some overly tedious scheduling of media content from a location with everything in it, to a location with "active" content.  This makes my media server constantly cycle in/out content so it's interactive.  Works great, but I have a special case where I don't want to schedule 80 scripts...

 

Don't laugh, but it's Looney Tunes, with like 40 seasons.  😆

 

Here is a basic script I would use for content in...

 


# CREATE NECESSARY SERIES DIRECTORIES
mkdir -p /mnt/user/"Television- Media Server"/"Family Guy (1999)"

# RECREATE MISSING SERIES METADATA
cp /mnt/user/"Television- All Media"/"Family Guy (1999)"/* /mnt/user/"Television- Media Server"/"Family Guy (1999)"/

# ADD CONTENT TO PRODUCTION SERVER
cp -R /mnt/user/"Television- All Media"/"Family Guy (1999)"/"Season 01" /mnt/user/"Television- Media Server"/"Family Guy (1999)"

 

I need to modify this script to create a VARIABLE between "1929" and "1969", and substitute that number for the season number in the copying commands.  All the documentation I found on this is confusing, so any help would be greatly appreciated.

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.