[Plugin] CA User Scripts


Recommended Posts

On 5/31/2020 at 8:26 AM, Squid said:

Long running processes cannot be killed via the abort button.  (I've tried everything to try and kill the PID of children from the PID of the parent, and nothing works because of how everything works under the hood)

I've had a few python scripts that run in the background which I needed to start/stop alongside the array; ex:

#!/bin/bash
#description=Starts Stream Deck Home Assistant Client. Req: python3
#arrayStarted=true
#name=start stream deck HASS client

# Change to app directory
cd /mnt/user/appdata/homeassistant-streamdeck/

# Source python venv
source venv/bin/activate

# Change to app src directory
cd /mnt/user/appdata/homeassistant-streamdeck/src

# Check if already running and stop
if pgrep -f "python3 HassClient.py" &>/dev/null; then
    echo "app is already running, killing"
    pkill -9 -f "python3 HassClient.py"
    sleep 5
fi

# Start app
python3 HassClient.py &
pid=$!

# User Scripts background fix
echo $pid > /tmp/user.scripts/running/start-streamdeck-ha
wait $pid


I've been able to get the intended behavior by saving the child PID manually along with a wait command.

Haven't looked too deep into exec.php for a better workaround but this is working for the moment.

Link to comment

Evening all 

I am new to User Scripts and wanted to know if the below is possible. 

When the UNRAID Server moves over to battery power and the UPS kicks in - is it possiable to create a script that will then trigger a URL? Then the same again when power is restored, a 2nd script that will trigger a 2nd URL? 

Link to comment
On 6/28/2020 at 8:42 PM, Aerodb said:

 

Thank you for the catch. second attempt attached. 

aeroserver2-diagnostics-20200628-2040.zip 346.85 kB · 2 downloads

 

On 6/28/2020 at 8:27 PM, trurl said:

That is not the diagnostics. Diagnostics includes log but also other things.

 

Tools - Diagnostics.

 

On 6/21/2020 at 6:20 AM, Squid said:

Then post the diagnostics

was this helpful at all? im not sure what im looking at with these logs/diagnostics. fresh copy attached.  

aeroserver2-diagnostics-20200712-1007.zip

Link to comment

If I include `git pull` in a script I get `fatal: could not read Username for 'https://github.com': No such device or address` I'm able to `git pull` fine if I use the terminal. Is there a reason why it would work in the terminal, but not with user scripts?

Edited by ddphd
Link to comment
On 7/9/2020 at 3:26 PM, IKWeb said:

Evening all 

I am new to User Scripts and wanted to know if the below is possible. 

When the UNRAID Server moves over to battery power and the UPS kicks in - is it possiable to create a script that will then trigger a URL? Then the same again when power is restored, a 2nd script that will trigger a 2nd URL? 

 

Seems it might be possible but would actually require interacting with apcupsd rather than as a "user script" that's easy to just drop into Unraid.  This would take some custom work, but not overly difficult it doesn't look like.

 

https://www.systutorials.com/docs/linux/man/8-apccontrol/

 

Specifically you'd be wanting to implement a script when apcupsd triggers this notificaiton:

 

powerout

apcupsd generates this event as soon as apcupsd detects that the UPS has switched to battery power and it is not a self-test.

Link to comment
6 hours ago, Alex.b said:

Hello I just want power on for 30 mins a Windows 10 VM to execute a software every day. How Can I achieve this ? I’m a total beginner with user script. I read to use « virsh start »

Yes, here's a basic script to do that.

 

virsh start <name of vm>
sleep 30m
virsh shutdown <name of vm>

 

Or you could just start it and then have the Windows VM shut itself down after a period of time.

Link to comment

Apologies if this has previously been answered, but I'm looking to run a set of light commands every 20 seconds for diagnostic purposes.

 

If I enter a script such as:

 

#!/bin/bash
while true
do
 <my stuff>
 sleep 20
done

And schedule it for run in background  and at first array start only will it run forever?

Link to comment
5 hours ago, Simon said:

will it run forever?

It should, but maybe something goes wrong inside your script or something else causes a crash.

 

Maybe it's better to set the cron interval to "every hour". But you need to think about what happens if your script is running multiple times. If this is a problem, you need to build a condition to be sure it's running only once.

  • Like 1
Link to comment
On 5/1/2020 at 11:26 AM, mgutt said:

Is CA User Scripts race condition safe or do I need to make my own check to be sure that my script runs atomic?

I made a little test and its not, so I created this script:

#!/bin/bash

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then
    exit 1
fi
trap 'rmdir "/tmp/${0///}"' EXIT

# script
# ...

By that a dir is created in /tmp with the name of the script and it is deleted after the script has been executed. And as long the script is running (so the dir is not deleted), the script will not be executed again.

 

Edited by mgutt
Link to comment

hi  i i have couple questions i tried googling didnt find the answers

1... can you  access Raspberry PI to run a command on it  from the user scripts  at a certain time.

2.. i know how to setup rsync to login another unraid  using the pub key stuff..   but can i do the same for raspberry pi kinda thing

 

what i wanan do is

Unraid ====>certain time====>tells Raspberry Pi to flip a relay on  using one of my raspberry pi scripts

then unraid(main)  in the userscript then tells another computer to turn on WOL       and then rsyncs with it..  

then after  unraid tells that computer to shut down.. and then tells Raspberry pi to run another script before ending the user script

 

is there any code that could help me

 

Link to comment

so i was able to get ssh into Raspberry pi

 

but running code  on the Raspberry Pi doesnt work right

i get this error on  Unraid
"Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
/tmp/user.scripts/tmpScripts/raspberry pi/script: line 4: /home/pi/bin/pumpon.py: No such file or directory"

 

my user script is 

ssh [email protected]
/home/pi/bin/pumpon.py 16

 

it says no such file or directory.. but it defently exisits on the pi  so i dunno why its giving me errors 

Edited by comet424
Link to comment

I'm running some python scripts on array startup; each script sends an email when it is started, and also when an exception occurs, but also when the program exits, using the atexit exit handler (https://medium.com/better-programming/create-exit-handlers-for-your-python-appl-bc279e796b6b)

 

If i run the python script manually, ctrl-c will indeed make the program send an email that it has been stopped, but if i call the python script from a user-script, and stop the program from user-script, no exit email is being sent, the exit handler doesn't seem to be called.  I guess this has to do with the way user-script runs these in the background, is there another way to hook up to some exit function in python so this works?

Link to comment
3 hours ago, jowi said:

I'm running some python scripts on array startup; each script sends an email when it is started, and also when an exception occurs, but also when the program exits, using the atexit exit handler (https://medium.com/better-programming/create-exit-handlers-for-your-python-appl-bc279e796b6b)

 

If i run the python script manually, ctrl-c will indeed make the program send an email that it has been stopped, but if i call the python script from a user-script, and stop the program from user-script, no exit email is being sent, the exit handler doesn't seem to be called.  I guess this has to do with the way user-script runs these in the background, is there another way to hook up to some exit function in python so this works?

@Squid can provide the real answer, but I suspect that User Script may be sending a SIGKILL to stop the script.

 

When you are running your script interactively and you push Ctrl-C, a SIGINT signal is sent to your script. If your script has a signal handler (which the atexit exit handler is), this signal can be caught, which allows your script to do a more graceful exit. In your case, that includes sending an email before exiting. However, a script with a signal handler can misbehave, either by choosing to or failing to exit after intercepting the SIGINT. This is where SIGKILL comes in. That's the signal that is sent when you run "kill -9 $PID". SIGKILL cannot be caught by a signal handler. It just shoots the script in the head immediately without waiting for a response.

 

Generally speaking, it's better to send a SIGINT first to allow a process to exit gracefully. And after a certain amount of time (i.e. 10 secs) and the process still has not exited, then send a follow-up SIGKILL to kill it immediately. If I'm right and User Scripts only sends SIGKILL, then perhaps this is a feature request you can put in for User Script.

Edited by Phoenix Down
  • Like 1
Link to comment
2 hours ago, Phoenix Down said:

@Squid can provide the real answer, but I suspect that User Script may be sending a SIGKILL to stop the script.

 

What I'm doing is 

pkill -TERM -P $pid
kill -9 $pid

followed by

ps aux | grep -i ' /usr/bin/php /tmp/user.scripts/tmpScripts/$script' | grep -v grep

And kill -9 on anything returned by the above

 

Killing off scripts is the worst part of user scripts, and something that doesn't quite seem to ever work correctly 100% (especially with long running processes spawned by the script)

Link to comment
7 hours ago, Squid said:

What I'm doing is 


pkill -TERM -P $pid
kill -9 $pid

followed by


ps aux | grep -i ' /usr/bin/php /tmp/user.scripts/tmpScripts/$script' | grep -v grep

And kill -9 on anything returned by the above

 

Killing off scripts is the worst part of user scripts, and something that doesn't quite seem to ever work correctly 100% (especially with long running processes spawned by the script)

I hear you, orphaned processes are a pain in the rear.

 

After you send the SIGTERM, do you wait at all before sending SIGKILL?

Link to comment
3 minutes ago, mgutt said:

Feature Request:

I really like to see echo output inside the logs (maybe as optional setting?). At the moment only errors are logged.

You can always get the script to handle this by sending entries to the syslog by redirecting the output to go through the ‘logger’ command

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.