[Plugin] CA User Scripts


Recommended Posts

Just upgraded to 6.8.1 and for whatever reason the code in my go file that loaded my custom array fan speed script into cron is now broken. Searching for cron help lead me to User Scripts, which I already had installed but was not using as a way to schedule my fan speed script. I'm trying to run a script on a custom cron schedule to have the script run every 2 minutes. I want this script to run automatically whenever the server is powered up and regardless of array state. A quick search of the forums didn't catch any posts on how to enter a custom cron schedule for a script. Do I select Custom from the schedule menu then enter "2 * * * *" into the Custom Cron Schedule box and then select either Run Script or Run Script in Background?

Link to comment

Edit: ignore. I did not read the release notes of 6.8.0

All i needed was to call "bash" in front of the script. 

root@Tower:/boot/config/plugins/user.scripts/scripts/temp# bash ./script
Hello, World!
root@Tower:/boot/config/plugins/user.scripts/scripts/temp#

 

Hi, 

Problem:

I upgraded to 6.8.1 from 6.7.2 and now i am unable to run the user scripts directly from SSH.

If i run the script through the user scripts web page it works.

If I run try to run the script directly through SSH I get error permission denied. I am doing nothing different than previously than before and even tried a few other things.

 

Script permissions:

root@Tower:/boot/config/plugins/user.scripts/scripts# ls -la temp/
total 112
drwx------  2 root root 16384 Aug 27 16:04 ./
drwx------ 33 root root 16384 Sep 11 12:39 ../
-rw-------  1 root root   946 Aug 27 16:04 .script.un~
-rw-------  1 root root     4 Feb 14  2019 name
-rw-------  1 root root 20742 Jan 20 00:34 script
-rw-------  1 root root  4645 Aug 27 15:56 script~

 

What i tried:

Making new script - I can edit the the file so It seems i have R/W access but cannot execute.

root@Tower:/boot/config/plugins/user.scripts/scripts/temp# chmod +x script
root@Tower:/boot/config/plugins/user.scripts/scripts/temp# ./script
-bash: ./script: Permission denied

 

Newperms - doesn't work but i guess that is expected being a FAT32 partition

root@Tower:/boot/config/plugins/user.scripts/scripts# newperms temp/
temp/
processing temp/
... chmod -R u-x,go-rwx,go+u,ugo+X temp/
... chown -R nobody:users temp/
chown: changing ownership of 'temp/script': Operation not permitted
chown: changing ownership of 'temp/name': Operation not permitted
chown: changing ownership of 'temp/script~': Operation not permitted
chown: changing ownership of 'temp/.script.un~': Operation not permitted
chown: changing ownership of 'temp/': Operation not permitted
... sync

completed, elapsed time: 00:00:00

I don't know if i'm looking the right direction but Googling it says the USB drive needs to be mounted with the correct permissions.

Any ideas? Thanks
 

 

Edited by pmeozbgg
Did not RTFM
Link to comment
On 1/18/2020 at 12:50 AM, z0ki said:

I also noticed that sometimes after say a reboot it seems LFTP will only work is I SSH into my seedbox and accept the KEY then LFTP will work again. Anyway to have my script do this automatically?

 

You can use:
set sftp:connect-program 'ssh -o StrictHostKeyChecking=no'

To disable the hostkeychecking, but this isn't super secure. Someone could (in theory) MITM your seedbox and you'd be none the wiser. It's probably not a huge deal, but it could be.

The best way would be to use instead:
set sftp:connect-program 'ssh -o UserKnownHostsFile=/boot/config/known_hosts' 
Copy your known_hosts file from /root/.ssh/known_hosts to /boot/config 

As far as the getting stuck on sync, I'm not sure as I'm not super familiar with mirror and lftp.

Link to comment
5 hours ago, Xaero said:

You can use:
set sftp:connect-program 'ssh -o StrictHostKeyChecking=no'

To disable the hostkeychecking, but this isn't super secure. Someone could (in theory) MITM your seedbox and you'd be none the wiser. It's probably not a huge deal, but it could be.

The best way would be to use instead:
set sftp:connect-program 'ssh -o UserKnownHostsFile=/boot/config/known_hosts' 
Copy your known_hosts file from /root/.ssh/known_hosts to /boot/config 

As far as the getting stuck on sync, I'm not sure as I'm not super familiar with mirror and lftp.

Thanks for the reply, but I got this fixed :)

 

I just added the known_hosts file to my config/ssh folder so now no longer need to manually accept SSH key then use LFTP, basically as you described. So all good thanks :)

Edited by z0ki
Link to comment
On 1/19/2020 at 1:12 PM, Kaizac said:

I'm trying to run a ssh command through User Scripts. So I expect creating a .sh file is the right way to do that. But then how do I trigger it from User Scripts?

 

And if I want to make it more complex by giving it this command when running:


PYTHONIOENCODING=utf8

* * * * * /path/to/script.sh

How would I go about that? And what exactly should be the path if I for example put the .sh file in the User Scripts/scripts folder?

There isn't enough information in this post to accomplish what you are trying to do.
PYTHONIOENCODING=utf8 
Is an environment variable for the Python processor. Are you trying to set this environment locally on unraid, or remotely on the server the code will end up running on?

Here's an example using a `heredoc` for the python script to be run on the SSH target:


ssh -q user@host << ENDSSH
PYTHONIOENCODING=utf8

/path/to/script.sh

...

...
ENDSSH



You can get fancier with stuff like capturing the return code etc, but this basic example shows how you can use data in a heredoc to execute effectively an entire script. Everything between << ENDSSH and ENDSSH is sent as commands over the established ssh connection.
Link to comment

Is there any way to get rid of "Full logs for this script are available at /tmp/user.scripts/tmpScripts/xxxxxx/log.txt" in the log ?

My script for ddns runs every 5 minutes for 24h/7day, so my log gets very long.

If there is a better way to handle the log with a script that has a so short schedule ?

Link to comment
58 minutes ago, Technikte said:

Is there any way to get rid of "Full logs for this script are available at /tmp/user.scripts/tmpScripts/xxxxxx/log.txt" in the log ?

My script for ddns runs every 5 minutes for 24h/7day, so my log gets very long.

If there is a better way to handle the log with a script that has a so short schedule ?

You can have the script only log the last execution.  See the 2nd post

Link to comment

edit:  I figured it out.  The %T part seems to be the issue, I'm guessing because it uses colons.  Replaced with more specific values to get HH.MM.SS rather than HH:MM:SS and it's okay now.  Cheers.

 

Hello.  Weird issue.  My script is writing to a logfile in my appdata share (works fine).  I'm trying to move the file to a unique, dated file for each run of the script (daily).  I'm using $(date) to append the date to the log file.  Works fine in a shell session, but when I run it from my script, the resulting filename is something like BEZTLJ~Z.LOG.

mv /path/to/logs/logfile /path/to/logs/logfile.$(date "+%F.%T").log

Normal logfile is named correctly.  Dated logfiles have garbled filenames.  Any ideas what I'm doing wrong?

 

Edited by jimbobulator
solved
Link to comment

I'm trying to do:

FoldersToCreate="folder1/subfolder,folder2,folder3,folder4"
mkdir -p /mnt/user/public/test/{$FoldersToCreate}

which ends up making these folders:

 

- {folder1

            - subfolder,folder2,folder3,folder4}

 

I've read up on bracket expansion, but I can't find a solution to my problem.  Help please.  Thanks in advance.

Link to comment

[edit] oh my god I'm dumb... My timezone was not set correctly.. That fixed the issue. [/edit]

 

Hi, thank you for this very useful plugin!

I am however having an issue using the 'Custom Schedule'

I have created 2 (very very simple) scripts, one to start a docker container at midnight, and one to stop that same docker container at noon.

both scripts work fine when running them manually ('Run Script' and 'Run In background'), but they do not start automatically when I use the custom schedule.

I have one set as 0 0 * * * and the other as 0 12 * * *

 

What am I doing wrong? (see screenshot)

 

ps : I tried looking for an explanation in this thread, but I only found posts from many versions ago so I figured I'd post it anyway. If I shouldn't have, please don't shoot me ;-)

download.png

Edited by sohopunk
Link to comment
8 hours ago, DZMM said:

I'm trying to do:


FoldersToCreate="folder1/subfolder,folder2,folder3,folder4"
mkdir -p /mnt/user/public/test/{$FoldersToCreate}

which ends up making these folders:

 

- {folder1

            - subfolder,folder2,folder3,folder4}

 

I've read up on bracket expansion, but I can't find a solution to my problem.  Help please.  Thanks in advance.



FoldersToCreate=\{"Folder1/SubFolder","Folder2","Folder3","Folder4"\}
eval mkdir -p /mnt/user/public/test/"$FoldersToCreate"

 

This works. You need to store the brackets in the variable, and then also evaluate those brackets.
So originally, you aren't storing the brackets, so that whole long chunk is treated as a single string (note my strings are separated by the commas)
In my case, I store the brackets. mkdir -p takes its arguments literally which means it includes special characters and doesn't do any bash handling. eval continues until the next carriage return (\r) and evaluates that entire chunk. So it expands the variable "$FoldersToCreate" to "{Folder1/SubFolder",...}" We use double quotes to support spaces in the folder names.

 

  • Thanks 1
Link to comment
1 hour ago, Xaero said:

 


FoldersToCreate=\{"Folder1/SubFolder","Folder2","Folder3","Folder4"\}
eval mkdir -p /mnt/user/public/test/"$FoldersToCreate"

 

 

This works. You need to store the brackets in the variable, and then also evaluate those brackets.
So originally, you aren't storing the brackets, so that whole long chunk is treated as a single string (note my strings are separated by the commas)
In my case, I store the brackets. mkdir -p takes its arguments literally which means it includes special characters and doesn't do any bash handling. eval continues until the next carriage return (\r) and evaluates that entire chunk. So it expands the variable "$FoldersToCreate" to "{Folder1/SubFolder",...}" We use double quotes to support spaces in the folder names.

 

@Xaero - you're a star, thanks.

Link to comment
  • 2 weeks later...

First off, you can't add that css via a bash script to anything (or at least do it right).  When executing that, you're going to get errors since <link  isn't a bash command.

 

Now, if you do happen to say via a php script include css

 

#!/usr/bin/php
<?
.
.
.
?>
<link rel='stylesheet' type='text/css' href='/plugins/dynamix/styles/dynamix-gray'>

Then when that script does run in the foreground, you are including replacement css for the entire tab.  Should that css replace any css already present, then yeah, you will see what you're seeing.  More or less to be expected. (And you might not see any change until the browser refreshes things -> ie: when you re-edit a script)

 

That being said, it is possible to include html within descriptions.

 

Alternatively, ensure that you're using a stock unRaid theme, then try and replicate it again.  If you can, then attach the exact script with an href to a publicly available css (yours isn't) and I'll see if I can't replicate.

Link to comment

Yeah I wasn't using it that way, just showing what happens. Bad example.. sorry :P

I'm injecting it with sed. 
 

#!/bin/bash

sed -i -e '\@<style>@i\    <link rel="stylesheet" href="https:\/\/somegithubpage.com\/customlogin\/login.css">' /usr/local/emhttp/login.php

 

I must have done something wrong the first time as it doesnt add the stylesheet to the plugin page now. So now it works as  expected..

Edited by GilbN
Link to comment

I was wongering if there is any chance of getting some more standard scheduling options added to correspond to Unraid events such as 'starting' ,  'stopping' and 'stopped'.   I was was thinking that this would allow me do via User Scripts commands that I currently do via the 'go' and 'stop' files.   Some of the other event types might also be worth considering if you think this is feasible but the ones I mentioned are of interest to me at the moment.

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.