[Plugin] CA User Scripts


Recommended Posts

18 hours ago, 1971camaroguy said:

I have a question, I am not 100% familiar with scripting so someone on another group helped me out with this.

 

It's supposed to look at the trailers folder and compare it to my movies folder for a movie match and if a trailer isn't in that movies folder, and there is a match in the Trailers folder,  it will copy it over, otherwise ignore the movie folder and move on.

 

He wrote it assuming the script would be sitting in the same share (Storage-1) as the trailer and movie folder sits in. But I think with this pugin the script is in it's own folder in /boot/config/plugins/user.scripts/scripts

 

I was wondering if someone could help me put the direct paths in this script so I can get it working? Right now the paths of my share and the two folders are structured like this, with Storage-1 being the main share folder.

 

Storage-1  /  Trailers

 

Storage-1  / Movies

 

Thanks for the help! Much appriciated 

 

 

#!/usr/bin/bash

for trailer in Trailers/*; do
	echo "Searching for movie for Trailer - '$trailer'"
	basename=$(basename "$trailer" .mkv)
	basename=${basename%-trailer}
	basename=${basename/%.[[:digit:]][[:digit:]][[:digit:]][[:digit:]]/}
	basename=${basename/% ([[:digit:]][[:digit:]][[:digit:]][[:digit:]])/}
	basename=${basename/[/\\[}
	basename=${basename/]/\\]}

	matches=`find Movies -iname "$basename"\*`
	if [[ -n "${matches}" ]]; then
	    num=$(echo -n "${matches}" | grep -c '^')
	    if [[ "$num" == "1" ]]; then
		    echo "--> Found match for trailer ${matches}"
		    echo mv "$trailer" "${matches}"
	    else
		echo "Multiple matches for trailer: ${trailer}"
		echo "${matches}"
	    fi
	#else
	#    echo "--> No match found for trailer ${matches}"
	fi
done

 

I got my issue sorted out, moving and matching trailers as expected now

 

 

 

Link to comment

Could you be convinced to add a variable to pipe stdout to a specific path?

 

My target: prometheus_node_exporter contains a textfile collector.

 

There are a handful of community scripts designed to

- be run from cron (ie your plugin on a custom schedule)

- collect useful (I'd argue critical) system metrics (ie smartmon stats like temps and failure indicators) for prometheus-node-exporter
- output data to stdout, to be piped to a file

 

The inconvenience is, to use these is not straightforward. They would require extensive modification most users can't grok.

 

Thanks to your plugin, an alternative is to manually save them to disk someplace, then use your plugin to write a script which runs them with stdout piped to a file, scheduled cron-alike.

image.png.5dbd3ee8538b00037bf220a351c58925.png

EDIT: These are the same script, ignore the name difference I used an old screenshot for the one by mistake

image.thumb.png.7cf4bd44bedf74e0c836472c69a64ee3.png

 

This is workable but some users may still find this unapproachable.

 

If the user could paste the entire script (in my case, /boot/config/scripts/smartmon.sh aka smartmon.sh [raw] among others) into a User Script, schedule them as cron-alikes, and mark their output to be sent to a specific path, it would make these more approachable.

 

It could be implemented similar to the variables you've recently added; an STDOUT variable could be changed to request redirection.

 

Regardless of your decision, keep up the great work. The plugin has been quite valuable for many of us!

Edited by codefaux
derp old screenshot
Link to comment
On 3/9/2023 at 1:48 PM, Presjar said:

Is it a problem if a CA script I have set to run on start of the array never completes when I test with "RUN SCRIPT"?

 

During my script I call another script that is first copy into /usr/local/bin

 

In the unraid WEBUI Shell, If I run ./monitor_arrayAudioChanges.sh I am returned to the # prompt and the process runs in background fine.

If I close the RUN SCRIPT window the PID started by the monitor_arrayAudioChanges.sh is also terminated. I have tried sever suggestions I found online to have this .sh start in a different way / in background but have had no luck. Any suggestions?

 

 

## Install inotify-tools package ##
installpkg /boot/packages/inotify-tools-3.22.6.0-x86_64-1.txz

## Setup monitoring of music and audiobooks array folders ##
cp -r /boot/custom/audioArrayToSSD /usr/local/bin && chmod +x /usr/local/bin/audioArrayToSSD/sync_arrayAudioToSSD.sh && chmod +x /usr/local/bin/audioArrayToSSD/monitor_arrayAudioChanges.sh
source /usr/local/bin/audioArrayToSSD/monitor_arrayAudioChanges.sh

## Install  docker compose 
BLABLABLA


 

Chasing some feedback on this one please if anyone knows what I can do.

Link to comment

Hello, I came across this plugin to remove the .DS_store files. I also have issues with needing to remove ._.DS_Store files as well as ._filename that mimics my actual file but tiny size (I believe its a mac related file and that "_.*" would remove them. Can anyone check if the script I have below is correct for removing the two additional types of files?

 


#!/bin/bash
echo "Searching for (and deleting) ._.DS_Store Files in array and external backup drives"
echo "This may take a awhile"
find /mnt/user -maxdepth 9999 -noleaf -type f -name "._.DS_Store" -exec rm "{}" \;
find /mnt/disks -maxdepth 9999 -noleaf -type f -name "._.DS_Store" -exec rm "{}" \;

 

#!/bin/bash
echo "Searching for (and deleting) ._* Files"
echo "This may take a awhile"
find /mnt/user -maxdepth 9999 -noleaf -type f -name "._*" -exec rm "{}" \;
find /mnt/disks -maxdepth 9999 -noleaf -type f -name "._*" -exec rm "{}" \;
 

Also, is it ok to run a second script that targets my external drive in the same script like above? Very new to unraid so any help is much appreciated, thank you!

 

Edited by BlackMamba24
Link to comment
On 4/9/2023 at 12:56 PM, Squid said:

Flash drive read only???  Also any weird punctuation etc in the script names?

Is there a good way to check if its read only?

 

Edit : This was resolved by manually deleting user scripts folder and files within the Flash drive and reinstalling fresh.

Edited by Nano
Link to comment
  • 3 weeks later...

I would like to run a script after the server is started. Can I achieve this by adding "@reboot" under crontab? Because this does not work for me.

 

Furthermore, the script should also start a Docker container. Are the permissions sufficient for this?

 

Currently it seems that the script does not start after restarting the server. If I run it manually in the background, then a part is processed, but the Docker container does not start. When I run it a second time, the Docker container also starts.

Link to comment
On 4/30/2023 at 4:19 PM, olco said:

I would like to run a script after the server is started

Will the Users Scripts option "At Startup of Array" work for this if you have the array set to autostart on a server boot?

 

As to starting the docker container, I don't have any thoughts on that.

Link to comment

So now I chose the "At Startup of Array" as start option for the script.

 

The script creates the two folders, which get removed everytime I reboot the server, but it doesn't mount the rclone remote and it doesn't start the embyserver docker. When I abort the script and manually run it again, one time, the remote gets mounted and embyserver docker starts.

 

It doesn't seem like a complex script to me ;)

 

#!/bin/bash
sleep 60
mkdir /mnt/gsuite
sleep 5
mkdir /mnt/gsuite/media
sleep 5
rclone mount gsuitemedia:/Medien /mnt/gsuite/media --allow-other --buffer-size 100M --dir-cache-time 72h --drive-chunk-size 32M --umask 002 --vfs-read-chunk-size 128M --vfs-read-chunk-size-limit off --read-only
sleep 10
docker container start EmbyServer

 

Link to comment
5 minutes ago, olco said:

So now I chose the "At Startup of Array" as start option for the script.

 

The script creates the two folders, which get removed everytime I reboot the server, but it doesn't mount the rclone remote and it doesn't start the embyserver docker. When I abort the script and manually run it again, one time, the remote gets mounted and embyserver docker starts.

 

It doesn't seem like a complex script to me ;)

 

#!/bin/bash
sleep 60
mkdir /mnt/gsuite
sleep 5
mkdir /mnt/gsuite/media
sleep 5
rclone mount gsuitemedia:/Medien /mnt/gsuite/media --allow-other --buffer-size 100M --dir-cache-time 72h --drive-chunk-size 32M --umask 002 --vfs-read-chunk-size 128M --vfs-read-chunk-size-limit off --read-only
sleep 10
docker container start EmbyServer

 

its one of the schedule options.

Link to comment

One of my scripts recently stopped working and I'm not sure why. It's an amateur bash script I wrote a while ago that reformats some files and moves them. The second for loop has just stopped working for some reason. It doesn't ever echo anything. The first one does just fine... I've been using this same script for almost 2 years now...

EDIT: I figured it out. I was being stupid. I had all these text files reformatted to .md files and then didn't change the for loop.
 

#!/bin/bash

folder="/mnt/cache/Real_Dropbox/Apps/Otter/Voice_Memos_New"

if [[ -z "$(ls -A -- "$folder")" ]] ; then
    echo "$folder is empty"
else
    echo "$folder is not-empty and contains $(ls $folder)"
cd $folder


for file2 in *;
do if echo "$file2" | grep -q '\s'; then
    echo "File title contains spaces"
    mv "$file2" `echo $file2 | tr ' ' '_'`; 
    echo "changed $file2 to underscores";
else
    echo "File title does not contain spaces"
fi
done

for file in $(find $folder -name '*.txt'); 
do filename=$(basename -s .txt $file);
echo "stored filename $filename";
audio=$filename.m4a;
echo "identified audio file $audio";
time_created=$(stat -c %y $audio|tr ' ' '_'|sed 's/-//g; s/://g; s/\.[^\.]*$/_/');
yamldate_created=$(stat -c %y $audio);
echo "stored time created $time_created";
newaudio=$time_created$audio;
echo "added time created to audio name $newaudio";
newtranscription=$time_created$filename.md;
echo "added time created to transcription name $newtranscription";
mv $file $newtranscription;
echo "changed $file to $newtranscription";
mv $audio $newaudio;
echo "changed $audio to $newaudio";
sed -i "1i \#\# Transcript" "$newtranscription"; sed -i "1i \!\[\[$newaudio\]\]" "$newtranscription"; sed -i "1i \-\-\-" "$newtranscription"; sed -i "1i date\:\ $yamldate_created" "$newtranscription"; sed -i "1i tags\:\ primarysource" "$newtranscription"; sed -i "1i type\:\ primary\ source" "$newtranscription"; sed -i "1i \-\-\-" "$newtranscription";
echo "added formatting to transcription $newtranscription"; 
mv $newtranscription /mnt/user/Syncthing/The\ Galactic\ Mining\ Operation/My\ Media/File\ Notes/Primary\ Sources\ \(Transcripts\)/;
echo "moved $newtranscription to syncthing";
mv $newaudio /mnt/user/Syncthing/The\ Galactic\ Mining\ Operation/Media/My\ Files/My\ Audio\ Files/Transcribed\ and\ Connected/;
echo "moved $newaudio to syncthing";
done
fi

 

Edited by kouru225
Link to comment

I've searched the forums for a script that pings an ip and if it doesnt respond it starts a docker. If anyone has such a script or can point me in the right direction id be immensly thankful.I've searched the forums for a script that pings an ip and if it doesnt respond it starts a docker. If anyone has such a script or can point me in the right direction id be immensly thankful.

Link to comment
8 hours ago, goa200 said:

I've searched the forums for a script that pings an ip and if it doesnt respond it starts a docker. If anyone has such a script or can point me in the right direction id be immensly thankful.I've searched the forums for a script that pings an ip and if it doesnt respond it starts a docker. If anyone has such a script or can point me in the right direction id be immensly thankful.

You will have to rewrite this to do what you want, but I think the basic concept is there for you. This script pings an IP, waits until it responds, then runs a command. If you research the ping command you should be able to use the logic the way you want.

#!/bin/bash
printf "%s" "waiting for IP to respond ..."
while ! ping -c 1 -n -w 1 192.168.1.1 &> /dev/null
do
    printf "%c" "."
done
printf "\n%s\n"  "IP is responding"
docker start "Container"

 

Link to comment
23 minutes ago, JonathanM said:

You will have to rewrite this to do what you want, but I think the basic concept is there for you. This script pings an IP, waits until it responds, then runs a command. If you research the ping command you should be able to use the logic the way you want.

#!/bin/bash
printf "%s" "waiting for IP to respond ..."
while ! ping -c 1 -n -w 1 192.168.1.1 &> /dev/null
do
    printf "%c" "."
done
printf "\n%s\n"  "IP is responding"
docker start "Container"

 

Thx.

I do however wanna do the opposite i.e. if the ip isnt responding then i wanna start a docker.

Doing some sort of plex backup where i have 2 servers in diff location and if one goes down i wanna start the other plexdocker

Link to comment
1 hour ago, JonathanM said:

So change it to do what you want. That's the point of my post, here are the building blocks, assemble as needed.

Yup Thx to your script i manage to come up with a version of my own. Big Thx for pointing me in the right dir.

Link to comment

I think I solved it and post it here for others.

 

The order displayed is defined by the order of the folders in config/plugins/user.scripts/scripts

It appears you can rename these without having to alter the name of the script in User Scripts.  At least that seems to work for me.

 

I hope that helps

 

D

Link to comment
On 5/10/2023 at 3:11 PM, ramiro said:

Has anybody else the problem that scripts with custom quene are not added to the /etc/cron.d/root file after a reboot ?

I have to change something in the scripts and after i click apply they are back in the file.

 

I have the same problem....

Link to comment
  • 2 weeks later...

Is there support for environment variables? I made a python script to backup my databases and wanted to use a Bearer Token in it which I obviously don't want inside my source code but in a seperate .env file. When I execute the script through python everything works as expected but the plugin doesn't recognize the environment variables and sets them as none. Any solution to that problem?

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.