[Plugin] CA User Scripts


Recommended Posts

Is there ever a chance that this plugin will be incorporated into the unraid system itself ? 

I have been using it since I started on unrain many years ago and I have so many scripts that it is getting difficult to find them. 

I just embarked on a script delete, rename and combine process to make it a bit better, but its still in a bit of a mess. 

Ideally, an ability to sort them in order, or be able to drag and drop like the VM's and Dockers would be ideal.   

Link to comment
7 hours ago, JonathanM said:

@scolcipitato? Want another project?🤣

 

You are doing such good work with folderview, maybe take a quick look and see if it's something you could fold into your view?

I don't know why, but I can't, I think it's an unraid thing, that it won't let me make changes to already existing plugins.

I tried to make it, I mean I already have everything done, just need to adapt it to the scripts style.

Link to comment
On 7/28/2023 at 2:18 PM, Daniel92 said:

Hello everyone,

I'm not sure but I found a bug, can someone confirm this?

If I create a new script and add a character @ in the title, in my example "energy save @startup". Then this line of the script can no longer be changed.

 

Greetings daniel :)

Hello Folks, please dont forget me to answer.  :)
Or is it possible to delete from a file in the unix system?


Thanks :)

Edited by Daniel92
Link to comment
13 hours ago, Greyberry said:

Is it possible to invoke a script via commandline, so that it is registered by the GUI, like i clicked on the button "Run in Background"?

may describe what your intention is as i dont see what you mean ...

 

you want to run a script from cli terminal and then it shoud "somehow" come into user scripts plugin page ?

 

if so, i guess not ...

Link to comment

Na, i got a user-script already in place, but want to call it from the terminal.

 

This in itself would not be the problem, because one could run:

/boot/config/plugins/user.scripts/scripts/[Name_Of_Script]/script

 

But if you do that, it would run as a standard script in terminal.

 

I wanted to know if there is a way to run it, so that the progress is also represented in the User-Script Plugin GUI, like running it from there.

 

Unbenannt.PNG

Link to comment
45 minutes ago, Greyberry said:

Na, i got a user-script already in place, but want to call it from the terminal.

 

This in itself would not be the problem, because one could run:

/boot/config/plugins/user.scripts/scripts/[Name_Of_Script]/script

 

But if you do that, it would run as a standard script in terminal.

You want to be careful doing that.  Scripts will not run directly from the flash drive for security reasons.  You would need to copy it ti /tmp/ somewhere, change it to execute, and run it from there.

Link to comment
On 8/20/2023 at 3:39 PM, dlandon said:

You want to be careful doing that.  Scripts will not run directly from the flash drive for security reasons.  You would need to copy it ti /tmp/ somewhere, change it to execute, and run it from there.

Thats why I am asking if there is a method to use User-Scripts (the plugin) somehow to start a script, not from gui but via terminal.

Link to comment

I have found some very strange behaviour with CA User Scripts and was hoping to get some help on it.

 

TLDR: Behaviour different to executing script in terminal. When making sqlite database query to pull info it is consistently leaving some of it out.

 

The specific command I am running is to grab chapter information out of an audiobook in the audiobookshelf database.

echo "$(sqlite3 -readonly "/mnt/[path-to_db].sqlite" \ "SELECT chapters FROM books WHERE title = '[booktitle]';")"
  • Running this command in terminal behaves as expected.
  • Running the script in user scripts causes some of the data to be excluded. It consistently breaks at the same point for the same book and for some books it might not break at all, for some it might remove some data from the middle, or it might only take some data off the end, but it always has the same behaviour for the same book.
  • Running from user scripts but in the background also broken.
  • Running the script in terminal but directly from "/boot/config/plugins.../script" (so the script created by user plugins) works as expected too....

There is no correlation between where the data is missing from either. For example it doesn't break after 1024 characters, it doesn't break when encountering a certain letter or symbol. It's just so weird.

Any help or insight would be much appreciated. I can only assume it's something different in the environment or how user scripts prints out stuff.

Link to comment
On 8/20/2023 at 11:39 PM, dlandon said:

You want to be careful doing that.  Scripts will not run directly from the flash drive for security reasons.  You would need to copy it ti /tmp/ somewhere, change it to execute, and run it from there.

Oh lol I literally just did that today? I can run any of my User Scripts scripts from the usb using "bash script" 
I suppose I can't make them executable with "chmod +x script" or run them like "./script".

Link to comment
On 7/30/2023 at 5:57 AM, Alex.b said:

Hello,

 

I'm using user scripts (v. 2023.07.16) for months, I saw today that missed some view logs / download / delete icons on some scripts, do you know why ?

 

image.thumb.png.abf05aac0feea1720d125ace69129190.png

They need to have actually run in the background for the logs to be created. If you just run them manually it won't pop up unless you use the run in background button. It's most likely they just haven't executed yet.

Link to comment

How can I add the logs of the script in the notification of Unraid. Currently I'm just sending a notification if something has failed like this:

 

if [ $? -eq 0 ]; then
    echo "Script succeeded"
else
    echo "Script failed"
    /usr/local/emhttp/webGui/scripts/notify -i "alert" -s "Auto-Archiving" -d "Something went wrong" -l "http://192.168.178.39:86/Main/Userscripts"
fi

It would be awesome if I could send the logs directly in the notification because I get a mail for that. I always have to log in and download the ZIP because the show logs window does never work.

Link to comment

I needed a way to move files off drives on the array without needing to give Unbalance exclusive access and wasn't able to find a good solution. So I wrote this script; I'm not sure if there's a better place to post it but I'm posting it here. All the obvious warnings apply (and are re-iterated in the comments at the top of the script). In particular, there is a reason Unbalance requires exclusive access. I've done the bare minimum by adding "system" and "appdata" to the ignored shares/directories, but this is not a "safe" operation.
 

#!/bin/bash

# source (array) and external drive directories
ARRAY_DIR="/mnt/user" # should be the same for all unraid installations, I think
EXTERNAL_DIR="/mnt/disks/YOUR_EXT_DRIVE" # I used Unassigned Devices to create a mount point

# disks within the array that you want to clear; files paths relative to these
# must be the same as that file's path relative to ARRAY_DIR
PATHS_TO_CLEAR=("/mnt/disk1" "/mnt/disk3") # disk(s) you want to clear
IGNORE_DIRS=("system" "appdata") # these directories/shares will not be processed

# minimum file size in bytes (files smaller than this will not be processed)
# MIN_SIZE=$((100 * 1024 * 1024)) # 100MB
MIN_SIZE=0 # no minimum size

# OVERVIEW
# The purpose of this script is to get files off one or more drives without exclusive access (a la
# Unbalance). Start by excluding the disk(s) you plan to remove from all shares, set the variables
# above, then run the script (either directly or with User Scripts plugin). The script will copy
# files one by one to the non-array directory, then back onto the array into the correct drives,
# finally deleting it from the source drive(s) to be removed.

# WARNING: When this script runs, if the copy from the external drive is successful, the version on
# array will be deleted (replaced by the version copied to the external drive). So don't use this 
# script on a drive that contains files that change frequently and could cause problems if overwritten
# such as configuration files.

# WARNING: This script will skip past files that are currently in use (as evidenced by not being able
# to delete them from the array). Try not to use this script on files that will be in use; it won't
# break the script, but it might result in unpredictable behavior.

# WARNING: Only tested once on 6.12.4. Use at your own risk.

copy_and_replace() {
  local disk_file="$1"
  local external_file="$2"
  local array_file="$3"
  local temp_array_file="${array_file}.new"

  echo "Processing file: $disk_file"

  # check if file already exists on the external drive
  if [ -e "$external_file" ]; then
    disk_size=$(stat -c %s "$disk_file")
    external_size=$(stat -c %s "$external_file")

    # if sizes match, assume file has already been copied
    if [ "$disk_size" -eq "$external_size" ]; then
      echo "File already exists on external drive with matching size. Skipping copy to external drive."
    else
      echo "File exists on external drive but sizes differ. Overwriting..."
      cp "$disk_file" "$external_file"
    fi
  else
    echo "Copying $disk_file to external location $external_file..."
    cp "$disk_file" "$external_file"
  fi

  # copy file back to the array with a ".new" extension
  echo "Copying to array at temporary location $temp_array_file..."
  cp "$external_file" "$temp_array_file"

  # check if copy back to array was successful
  if [ $? -eq 0 ]; then
    echo "Copy back to array successful. Replacing original file in array and deleting from external drive..."
    mv "$temp_array_file" "$array_file"
    rm -f "$external_file"
  else
    echo "Error: Could not copy $external_file back to $array_file"
  fi
}

# generate find command's prune parameters for ignored directories
PRUNE_CMD=""
for dir in "${IGNORE_DIRS[@]}"; do
  PRUNE_CMD+=" -o -path '$dir' -prune "
done

# Iterate through all specified disks within the array
for disk_path in "${PATHS_TO_CLEAR[@]}"; do
  echo "Starting the file transfer process for $disk_path..."

  # Iterate through all files in the current disk directory
  eval "find '$disk_path' -type d \( -false $PRUNE_CMD \) -o -type f -size +'${MIN_SIZE}c' -print0" | while IFS= read -r -d '' disk_file; do
    # calculate relative path and corresponding external and array file paths
    relative_path="${disk_file#$disk_path/}"
    external_file="$EXTERNAL_DIR/$relative_path"
    array_file="$ARRAY_DIR/$relative_path"

    # create external directory if it doesn't exist
    mkdir -p "$(dirname "$external_file")"

    copy_and_replace "$disk_file" "$external_file" "$array_file"
  done
done

echo "File transfer process completed."

 

Link to comment

@Squid I think there must be something wrong with User Scripts running on 6.12.4.

 

I own three Unraid Server. During the last week I did migrate all my servers from 6.11.5 to 6.12.4.

 

From that time, after every reboot, the "Custom Schedules" are gone from the "/etc/cron.d/root" file. I have to change a schedule (replace a single digit in the custom schedule and hit Apply) to get them back in the file above.

 

I had to reboot 5 times since last weekend and the custom schedules were gone after every reboot (race condition?, Unraid overwriting after User Scripts start?)

 

Thanks for listening.

 

root@Tower:~# cat /etc/cron.d/root
# Generated docker monitoring schedule:
10 0 * * * /usr/local/emhttp/plugins/dynamix.docker.manager/scripts/dockerupdate check &> /dev/null

# Generated system monitoring schedule:
*/1 * * * * /usr/local/emhttp/plugins/dynamix/scripts/monitor &> /dev/null

# Generated plugins version check schedule:
10 0 * * * /usr/local/emhttp/plugins/dynamix.plugin.manager/scripts/plugincheck &> /dev/null

# Generated Unraid OS update check schedule:
11 0 1 * * /usr/local/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck &> /dev/null


# CRON for CA background scanning of applications
13 * * * * php /usr/local/emhttp/plugins/community.applications/scripts/notices.php > /dev/null 2>&1

***
*** These are missing after every reboot
***
# Generated cron schedule for user.scripts
34 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup/script > /dev/null 2>&1
30 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Flash/script > /dev/null 2>&1
40 4 * * * /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Tree/script > /dev/null 2>&1

 

Unbenannt.jpg.9c463ce4d0d3561033fd368582e350a5.thumb.jpg.dd2882e18451aa3575026569f055a2fc.jpg

Edited by hawihoney
Link to comment
11 hours ago, hawihoney said:

gone after every reboot

So reapplying the settings re-adds the cron schedule to /etc/cron.d/root?

 

Next time you reboot, before you fix the settings, see if /config/plugins/user.scripts/customSchedule.cron exists on the flash drive.

 

and if it does will

update_cron

 

fix the issue?

Link to comment
11 minutes ago, Squid said:

So reapplying the settings re-adds the cron schedule to

 

I just change a digit in one of the schedules and hit Apply. This works always. 

 

14 minutes ago, Squid said:

Next time you reboot, before you fix the settings, see if /config/plugins/user.scripts/customSchedule.cron exists on the flash drive.

 

Just to clarify: I check for this file after the reboot?

 

Link to comment
11 hours ago, Squid said:

yes

 

Ok, did reboot to collect required information.

 

1.) Before reboot. You can see that /etc/cron.d/root and /boot/config/plugins/user.scripts/customSchedule.cron do exist and contain all custom schedules from the User Scripts plugin:

 

root@Tower:~# ls -l /etc/cron.d/root
-rw------- 1 root root 1157 Sep  4 14:50 /etc/cron.d/root

root@Tower:~# cat /etc/cron.d/root
[... Standard Unraid cron jobs ignored ...]
# Generated cron schedule for user.scripts
35 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup/script > /dev/null 2>&1
30 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Flash/script > /dev/null 2>&1
40 4 * * * /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Tree/script > /dev/null 2>&1

root@Tower:~# ls -l /boot/config/plugins/user.scripts/customSchedule.cron
-rw------- 1 root root 472 Sep  4 14:50 /boot/config/plugins/user.scripts/customSchedule.cron

root@Tower:~# cat /boot/config/plugins/user.scripts/customSchedule.cron
# Generated cron schedule for user.scripts
35 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup/script > /dev/null 2>&1
30 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Flash/script > /dev/null 2>&1
40 4 * * * /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Tree/script > /dev/null 2>&1

 

2.) After reboot. You can see that /etc/cron.d/root does exist but does not contain the custom schedules. /boot/config/plugins/user.scripts/customSchedule.cron does exist and does contain all custom schedules from the User Scripts plugin:

 

root@Tower:~# ls -l /etc/cron.d/root
-rw------- 1 root root 533 Sep  5 11:44 /etc/cron.d/root

root@Tower:~# cat /etc/cron.d/root
[... Standard Unraid cron jobs ignored ...]

root@Tower:~# ls -l /boot/config/plugins/user.scripts/customSchedule.cron
-rw------- 1 root root 472 Sep  4 14:50 /boot/config/plugins/user.scripts/customSchedule.cron

root@Tower:~# cat /boot/config/plugins/user.scripts/customSchedule.cron
# Generated cron schedule for user.scripts
35 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup/script > /dev/null 2>&1
30 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Flash/script > /dev/null 2>&1
40 4 * * * /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Tree/script > /dev/null 2>&1

 

3. Update the custom schedules via User Scripts command line tool. You can see that after running that tool the custom schedules reappear in /etc/cron.d/root:

 

root@Tower:~# update_cron

root@Tower:~# ls -l /etc/cron.d/root
-rw------- 1 root root 1158 Sep  5 11:49 /etc/cron.d/root

root@Tower:~# cat /etc/cron.d/root
[... Standard Unraid cron jobs ignored ...]
# Generated cron schedule for user.scripts
35 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup/script > /dev/null 2>&1
30 4 * * 0 /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Flash/script > /dev/null 2>&1
40 4 * * * /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/Backup Tree/script > /dev/null 2>&1

 

Edited by hawihoney
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.