[Plugin] CA User Scripts


1316 posts in this topic Last Reply

Recommended Posts

  • 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

17 hours ago, Squid said:

You'd have to post the script as a start

Sure thing here you go,

 

#!/bin/bash

time lftp << EOF
set ssl:verify-certificate no
set sftp:auto-confirm yes
open sftp://user:password@host.com

mirror --use-pget-n=8 --Remove-source-dir /home/user/downloads/deluge/LFTP/"4K Movies"/ /mnt/cache/Downloads/incoming/"4K Movies"

quit
EOF

chmod -Rv 770 /mnt/cache/Downloads/incoming/"4K Movies"
chown -Rv nobody:users /mnt/cache/Downloads/incoming/"4K Movies"

mv /mnt/cache/Downloads/incoming/"4K Movies"/* /mnt/disk3/Media2/"4K Movies"

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?

Edited by z0ki
Link to post

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?

Link to post

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 post

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 post

You are no longer allowed to have any files on the flash drive with execute permission.   You either need to copy files elsewhere to be able to change the permissions or precede the script name with the command needed to run it (e.g. bash path-to-script)

Link to post
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 post
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 post
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 post

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

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 post

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 post

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

 

Link to post
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 post
  • 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 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.