[Plugin] CA User Scripts


Recommended Posts

Does someone got autossh to work?
My Problem is that it works in a normal ssh session but if i try to start it through the user scripts plugin it wont work.
Do I have to set some special env variable? I tried to copy some of the env variables from another session.
This is my script ATM the first run always stucks at the end and wont terminate and if i run it a second time it runs and terminates but the autossh processes aren't started

#!/bin/bash
SHELL=/bin/bash
LESS=-RM
G_BROKEN_FILENAMES=1
HOSTNAME=Tower
PWD=/root
LOGNAME=root
MANPATH=/usr/local/man:/usr/man
HOME=/root
LANG=en_US.UTF-8
TERM=linux
G_FILENAME_ENCODING=@locale
LESSOPEN=|lesspipe.sh %s
USER=root
NCURSES_NO_UTF8_ACS=1
SHLVL=1
INPUTRC=/etc/inputrc
NODE_PATH=/usr/lib64/node_modules
LC_COLLATE=C
HUSHLOGIN=FALSE
PATH=.:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
HZ=100
env
if [ -f /usr/bin/autossh ]
then
echo "already installed. Trying to start sessions now"
else
echo "not installed. Installing..."
wget https://packages.slackonly.com/pub/packages/14.2-x86_64/network/autossh/autossh-1.4g-x86_64-1_slonly.txz
installpkg autossh-1.4g-x86_64-1_slonly.txz
echo "starting Sessions..."
fi
sleep 5
autossh -M 21010 -R :14002:0.0.0.0:14002 -N -f -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -i /mnt/user/Private/sshKeysForAutossh/id_ed25519 username@mydomain.de
autossh -M 21030 -R :14003:0.0.0.0:14002 -N -f -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -i /mnt/user/Private/sshKeysForAutossh/id_ed25519 username@mydomain.de
autossh -M 21020 -R :32400:0.0.0.0:32400 -N -f -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -i /mnt/user/Private/sshKeysForAutossh/id_ed25519 username@mydomain.de
echo "all started"
exit 0

okay I did solve it by myself a simple sudo -u root was necessary in front of each autossh command

Edited by unraidnh
solved
Link to comment

I currently run two scripts that I'd like to combine into one.  They check to see if my cache disk has got to a certain amount of % used

 

- if it's less than a certain amount it moves files that I don't need on the cache to the array so that files I want on the cache can stay on for longer

- if it's over a certain amount then it starts the mover (I've disabled the scheduled mover)

 

Here's the first script:

 

#!/usr/bin/php

<?PHP

$min = 70;
$max = 90;
$moveAt = 90;

$diskTotal = disk_total_space("/mnt/cache");
$diskFree = disk_free_space("/mnt/cache");
$percentUsed = ($diskTotal - $diskFree) / $diskTotal * 100;

if ( $percentUsed > $moveAt ) {
  $my_string = 'Starting mover';
  echo $my_string;
  echo "<br>";
  exec("/usr/local/sbin/mover");
} else {
    $my_string = 'Not running mover';
	echo $my_string;
	echo "<br>";
}

if (($min <= $percentUsed) and ($percentUsed <= $max)) {
  $my_string2 = 'starting diskmv_all';
  echo $my_string2;
  echo "<br>";
  echo "<br>";
  exec("bash /boot/config/plugins/user.scripts/scripts/diskmv_all/script");
} else {
    $my_string = 'Not running diskmv_all';
	echo $my_string;
	echo "<br>";
}
?>

and the second that runs the diskmv script:

#!/bin/bash

#######  Check if script already running  ##########

echo "$(date "+%d.%m.%Y %T") INFO: Creating Tracker file."
if [[ -f "/mnt/user/appdata/other/rclone/diskmv_running" ]]; then

echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running."

exit

else

touch /mnt/user/appdata/other/rclone/diskmv_running

fi

if [ -f /var/run/mover.pid ]; then
  if ps h `cat /var/run/mover.pid` | grep mover ; then
      echo "$(date "+%d.%m.%Y %T") INFO: mover already running.  Removing Tracker file."
      rm /mnt/user/appdata/other/rclone/diskmv_running
      exit 0
  fi
fi

# /usr/local/sbin/mdcmd set md_write_method 1

# echo "Turbo write mode now enabled"

echo "$(date "+%d.%m.%Y %T") INFO: moving backup."

bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/backup" cache disk2

# echo "$(date "+%d.%m.%Y %T") INFO: moving local."

bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/local/tdrive_vfs/downloads/complete" cache disk1

echo "$(date "+%d.%m.%Y %T") INFO: moving media."

bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/media/music" cache disk2
bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/media/other_media/books" cache disk2
bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/media/other_media/photos" cache disk2
bash /boot/config/plugins/user.scripts/scripts/diskmv/script -f -v "/mnt/user/media/other_media/videos" cache disk2

# /usr/local/sbin/mdcmd set md_write_method 0

# echo "Turbo write mode now disabled"

echo "$(date "+%d.%m.%Y %T") INFO: Removing Tracker file."
rm /mnt/user/appdata/other/rclone/diskmv_running

exit

Is it possible to combine?

 

Thanks in advance

Edited by DZMM
Link to comment
On 11/30/2018 at 4:42 AM, kizer said:

I have all mine goto a Cache/SSD drive. No spin ups required. 😃

 

I have a few shares

(Uploads)

   ../incoming

   ../outgoing

(Movies)

(Tv)

 

When ever I rip a movie or tv show I always send it to my (Upload) share. Then unraid via user scripts checks that upload folder and rips thru handbrake and a few other processes and then it places it in outgoing folder which FileBot looks at and then renames things and places it either in a (TV) or (Movie) share. Mover runs in the AM when my Plex Docker spools up to check my drives for new stuff moves the files onto the Protected Array off the SSD drive. 

 

My scripts run every 30 minutes so I don't have to Babysit, but the final drop to my Array/Spinner drives are at 6AM along with Plex. 

This sounds like something I want to do, albeit not as complex but a more simple version.

 

BTW, sorry for the quote on an old ass post but that's what search is for right? :)

 

I have a seedbox and I've got a LFTP script that will periodically check for files on there and pull them from my seedbox to my unRaid server. I want to know if a script can scan a specific folder (in this case, where my LFTP script dumps new downloads on my unRaid server) rename ONLY the Movie Folder name and then move them to the appropriate final destination automatically? Is this possible?

 

Seedbox > LFTP pulls files > mnt/cache/downloads/incoming > Filebot script runs > rename movie folder > move to /mnt/disk3/Media2/"4K Movies"

 

Someone said Filebot doesn't rename folders but this doesn't sound right as i've seen many posts of people doing exactly that. 

Link to comment
17 minutes ago, kizer said:

@z0ki

 

I use Filebot and it creates folders just fine for me. This is the format for mine. 
 

Transformers(2007)(720p)

/Transformers.mkv

 

So how would I create a bash script that will run after the files are pulled to my unRaid server?

 

Say you download a movie and it's folder is The.Irishman.2019.4K.HDR and I want it to be renamed only to The Irishman and then have that moved to media/"4K Movies"?

 

It sounds simple enough but the people I've asked have made it seem like it's impossible to do?

Link to comment

I have question..  I have a cron job running rysnc   but over the network or over the internet to my off site unraid..

I loose connection with rysnc it fails on its own etc..

 

how do I make a do loop to run till rsync finishes the job properly then quit...

I get this error "rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.3]"

 

so it should keep resuming till its done

 

how can I do this is it even possible

 

Link to comment
On 12/10/2019 at 10:12 AM, caplam said:

I think i solved my problem.

i had to change the first line

from


#!/usr/bin/python

to


#!/usr/bin/python3

I also had to install influxdb in a shell window:


pip3 install influxdb

Now i'm waiting for the first push to come.

If i understand right the script has to be lauched with "&" at the end off the command line in order to run without the window opened.

My bad the "run in background" button is huge but i had not seen it.

So i started the script with run un background. I also sheduled a run at array startup

 

Hi, did you get this to work?

I have a basic Python script which require a module loading but I was under the impression I had to pip3 install the module within the python script?

When you pip3 installed the influxdb module in a shell window does the ca users script plugin detect that this module is installed? I thought the shell and the ca user script were separate 'environments'?

 

Link to comment
6 minutes ago, mcfetti said:

I thought the shell and the ca user script were separate 'environments'?

They are completely different.  There are circumstances because of that environment difference where the odd script will not operate via user scripts but will at the command prompt.  Whether this affects you or not, I cannot say.  (The usage of this plugin has significantly diverged from its original design goal of doing something like simple like creating a symlink)

Link to comment
9 minutes ago, Squid said:

They are completely different.  There are circumstances because of that environment difference where the odd script will not operate via user scripts but will at the command prompt.  Whether this affects you or not, I cannot say.  (The usage of this plugin has significantly diverged from its original design goal of doing something like simple like creating a symlink)

Thanks.  I don't understand how caplam got it working then by installing a library in shell and then running his python script in the ca scripts plugin loading a module from that library

Link to comment

Hello,

 

I have a script which moves TV shows from local storage to my Google drive crypt. Can someone explain how the scheding works?

 

For example I want to set the move script to run every 10mins. However if there are a large amount of files in the directory it could take a while to finish.

 

How does user scripts handle this? Will it just not run the script or do I need to add some code in my script to handle this?

 

Hope this makes sense.

Link to comment
8 minutes ago, Squid said:

this

Okay, so I added the following to the start of the script that should work right?

if pidof -o %PPID -x "$0"; then
   exit 1
fi

 

Just thought I should add that I'm planning on having 2 scripts one for my TV Shows and one for Movies.

 

Edited by Conmyster
Link to comment
9 minutes ago, Conmyster said:

Okay, so I added the following to the start of the script that should work right?

if pidof -o %PPID -x "$0"; then
   exit 1
fi

 

Just thought I should add that I'm planning on having 2 scripts one for my TV Shows and one for Movies.

 

What I usually do is

 

  • See if /var/run/scriptName.pid exists
  • If it doesn't, write the pid to that file and execute the script
  • If it does, read the pid from the file and see if that pid is actually running.  If its not then execute the script
Link to comment
6 minutes ago, Squid said:

What I usually do is

 

  • See if /var/run/scriptName.pid exists
  • If it doesn't, write the pid to that file and execute the script
  • If it does, read the pid from the file and see if that pid is actually running.  If its not then execute the script

Hello,

 

How would you go about doing this exactly?

Link to comment
35 minutes ago, Squid said:

Taken from the mover script,


PIDFILE="/var/run/mover.pid"

if [ -f $PIDFILE ]; then
  if ps h $(cat $PIDFILE) ; then
    exit 1
  fi
fi

echo $$ >/var/run/mover.pid

.
.
.

 

Sorry I'm failing to understand how exactly that if statement works.

 

I think if I am understanding it correctly it will check if the pid file exists then if it does it will compare the current pid with the pid in the pid file. However I'm unsure what it does after that...

Link to comment

if the pid file exists and the pid actually exists, then it exits

 

The last line writes the pid of the script to the file that it checks since at that point, a previous instance of the script is no longer running.

 

The double check handles if you forget to delete the pid file when you exit normally, or if the script crashes and exits abnormally without it deleting the pidfile

 

EDIT:  You don't care about what the current PID is until you verify that a previous instance isn't already running (which will have a different PID)

 

Link to comment
3 hours ago, Squid said:

Taken from the mover script,


PIDFILE="/var/run/mover.pid"

if [ -f $PIDFILE ]; then
  if ps h $(cat $PIDFILE) ; then
    exit 1
  fi
fi

echo $$ >/var/run/mover.pid

.
.
.

 

One thing I just noticed is that when you echo the current PID into the pid file you use the full path instead of using the $PIDFILE to reference it?

 

Equally the same once the script has run I should put rm -f $PIDFILE at the end

Link to comment
16 minutes ago, Conmyster said:

One thing I just noticed is that when you echo the current PID into the pid file you use the full path instead of using the $PIDFILE to reference it?

I blame limetech, as the code is lifted from their mover script... (I do the same thing on my stuff, but use PHP -> never could wrap my head around bash scripts, variables, if / then etc

Link to comment
On 1/15/2020 at 3:16 PM, mcfetti said:

Hi, did you get this to work?

I have a basic Python script which require a module loading but I was under the impression I had to pip3 install the module within the python script?

When you pip3 installed the influxdb module in a shell window does the ca users script plugin detect that this module is installed? I thought the shell and the ca user script were separate 'environments'?

 

it worked but i wanted this to work at array startup.

The second script (which should run in background) is executed before the first one finishes installing influxdb module.

So i have to manually execute the first, wait it finishes and then run the second in backgound.

Link to comment

Hi Guys,

 

This may be a weird request.

 

I created a personal web site that I run locally - in the LetsEncrypt docker. I have coded the web site in php. I would like to create a button on the page that executes one or more of my UserScripts. Is this possible?

 

I looked at the html code for the button and it shows:

 

<input type="button" value="Run Script" class="runningNZBGetUIEdits" id="foregroundNZBGetUIEdits" onclick="runScript("/boot/config/plugins/user.scripts/scripts/My script Here/script");">

What can I do to make this executable from my web page?

 

Thank you!

 

H.

 

 

Link to comment

kinda sorta.  Not the easiest, but possible.  Of the top of my head

  • You would have to create a .page file on unRaid that would actually wind up executing the script
  • You would then point your webpage to that .page file

One initial showstopper is that without basic authorization being enabled on 6.8+ you can't actually hit that .page file without also logging in (which you can't do).  There is a hack available to re-enable basic authorization though IIRC

 

All in all, it's a big PITA because you're running head on into cross-site vulnerability issues which are being hardened by both unRaid and browsers constantly.

 

On the other hand, if by some miracle you still have unRaid version 6.0.x running then you can do this quite easily without any special tricks...

 

Link to comment

I've got an LFTP bash script that is scheduled to run every 10 minutes, but I notice if it runs and it doesn't find anything to download from my seedbox the script just keeps running and running in the background. Is there a way to make it stop with a bit of code I can put in my script that will make unRaid say "Hey, I ran the script found nothing I'll abort and try again in 60 minutes"

 

 

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.