[Plugin] CA User Scripts


Recommended Posts

On 2/27/2024 at 1:25 AM, FlamongOle said:
root@Odin:~# /usr/local/emhttp/plugins/user.scripts/startSchedule.php hourly
bash: /usr/local/emhttp/plugins/user.scripts/startSchedule.php: Permission denied

root@Odin:~# php /usr/local/emhttp/plugins/user.scripts/startSchedule.php hourly
/usr/local/emhttp/plugins/user.scripts/backgroundScript.sh "/tmp/user.scripts/tmpScripts/dynu_updater_hourly/script" >/dev/null 2>&1/usr/local/emhttp/plugins/user.scripts/backgroundScript.sh "/tmp/user.scripts/tmpScripts/pfsense_firewall/script" >/dev/null 2>&1

 

Noticed that I got a permission error in the logs from the User Scripts plugin, it looks like Unraid does not accept the #!/usr/bin/php anymore. I had to add "php" in front of the startSchedule.php in the crontab file to make it work. Can someone test this, I wanna know if it's just my system or a bug/glitch..

Looks like it resolved after a reboot, dunno why or what happened there.

Link to comment

Hi, First post.... I have tried to use crontab inside a VM but it does not seem to run a script I have, run's manually fine. 
I have seen mention of using User Scripts to run commands in docker....e.g docker exec "etc", is there a way to run a script inside a VM on a schedule. I need wake up an SMB share in Proxmox Backup Server VM from a TrueNAS VM on the same Unraid server. I have the fstab built and it remembers it but needs a manual "mount -a" after a reboot to restore the share.
Many Thanks

Link to comment
5 hours ago, GeeMan said:

is there a way to run a script inside a VM on a schedule.

nope, virsh doesnt support this

 

Alternativ, you could run a script which does

 

ssh into your VM and run the command ... either keypair or install sshpass on unraid (unsafe !!!)

 

but rather you should look into your VM why the cron aint running there ... assuming you already did so ;)

  • Like 1
Link to comment

Is there a way to make the length of the log that's displayed in the dashboard longer?

 

I know I can go to /tmp/user.scripts/tmpScripts/scriptname/log.txt, but I'd rather check it with the little log icon right in the dashboard.

 

Another 26 lines should do it.

Link to comment
  • 2 weeks later...
On 3/22/2024 at 12:28 PM, nraygun said:

Is there a way to make the length of the log that's displayed in the dashboard longer?

 

I know I can go to /tmp/user.scripts/tmpScripts/scriptname/log.txt, but I'd rather check it with the little log icon right in the dashboard.

 

Another 26 lines should do it.

Any ideas anyone?

Link to comment
On 4/1/2024 at 9:31 AM, Dukobpa3 said:

Can I join to running script session somehow?

 

Use tmux 😉

#!/bin/bash

# Start with TMUX
SESSION_NAME="$(basename "$(dirname $0)" | sed 's/ //g')"
export TERM=xterm-256color
if [ -z "$TMUX" ]; then
    if [ $(tmux ls 2> /dev/null | grep "$SESSION_NAME" | wc -l) -gt 0 ]; then
        echo "Attach to exist TMUX-Session: $SESSION_NAME"
        tmux send-keys -t "$SESSION_NAME" "$0" C-m
    else
        echo "Create a new TMUX-Session: $SESSION_NAME"
        tmux new-session -d -s "$SESSION_NAME"
        tmux send-keys -t "$SESSION_NAME" "$0" C-m
    fi
fi

your script here..

 

Then you can show your scripts and attach to a session:

grafik.png.c2b9cbfca75a65661988a2a39f471760.png

 

Thats my variant..

 

 

If you have problems with tmux and 256-color settings (until the restart) :

grep -q 'set -g default-terminal "xterm-256color"' ~/.tmux.conf || sed -i '1iset -g default-terminal "xterm-256color"' ~/.tmux.conf
grep -q 'export TERM=xterm-256color' ~/.bash_profile || echo -e '\nexport TERM=xterm-256color' >> ~/.bash_profile
. ~/.bash_profile

 

For permanent workaround, you must add the following to the go file (/boot/config/go) :

# TMUX 256-color workaround
echo -e '\n# TMUX 256-color workaround' >> /root/.bash_profile
echo 'export TERM=xterm-256color' >> /root/.bash_profile

# TMUX configuration
echo 'set -g default-terminal "xterm-256color"' > /root/.tmux.conf
Edited by Amane
  • Thanks 1
Link to comment

Hey there. I'm maximum confused with a script I created. It works when I trigger it manually but never does on schedule / cronjob. I mean it runs but does not what it should and does when triggered manually.

 

#!/bin/bash
#description=This script updates all apps in Nextcloud.
#arrayStarted=true
#name=Nextcloud Auto-Update Apps

# Befehl ausführen und Exit-Status speichern
docker exec -u www-data nextcloud php /var/www/html/occ app:update --all
docker_exit_status=$?

if [ $docker_exit_status -eq 0 ]; then
    curl -fsS -m 10 --retry 5 --data-raw "$m" http://unraid-server:8003/ping/1505e87d-84e2-4806-a2a0-fa214d96cb17
    echo -e "\nScript succeeded"

This is the script. My docker container named nextcloud should automatically update the newer apps. When I press "Run Script" it works and apps are updated in the logs. On my set schedule "0 3 * * *" it does not work. I know the script is running because the curl request is sent. The logs look like this:

Script Starting Apr 14, 2024  03:00.01

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

OK
Script succeeded
Script Finished Apr 14, 2024  03:00.10

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

Script Starting Apr 15, 2024  03:00.01

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

OK
Script succeeded
Script Finished Apr 15, 2024  03:00.10

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

Script Starting Apr 16, 2024  03:00.01

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

OK
Script succeeded
Script Finished Apr 16, 2024  03:00.02

Full logs for this script are available at /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/log.txt

 

And if I run the script manually now:

Script location: /tmp/user.scripts/tmpScripts/nextcloud_auto_update_apps/script
Note that closing this window will abort the execution of this script
spreed new version available: 18.0.7
spreed updated
keeweb new version available: 0.6.18
keeweb updated
OK
Script succeeded

What could be the reason it does not work on schedule? 

Link to comment

Hi sasbro

 

2 hours ago, sasbro97 said:

On my set schedule "0 3 * * *" it does not work

Cron jobs do not run with the full user environment, which means some environment variables needed by docker or your script might not be set correctly. This can affect the execution of commands that rely on these variables.

 

Add the line on top of your script:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

 

I hope it helps.

If not, test whether you can copy the script into the container and execute it with the scheduled user script - just for test

 

Grüsse ;)

Edited by Amane
  • Like 1
Link to comment

 

22 hours ago, sasbro97 said:

Hey there. I'm maximum confused with a script I created. It works when I trigger it manually but never does on schedule / cronjob. I mean it runs but does not what it should and does when triggered manually.

 

What could be the reason it does not work on schedule? 

  

20 hours ago, Amane said:

Cron jobs do not run with the full user environment, which means some environment variables needed by docker or your script might not be set correctly. This can affect the execution of commands that rely on these variables.

 

Try adding the "-l" option to bash and source the ".bash_profile".

 

Example:

#!/bin/bash -l

source /root/.bash_profile

set > /boot/logs/test.log

 

Output:

test.log

 

Edited by JoeUnraidUser
  • Like 1
Link to comment

Hi,

 

Would this script work to monitor the GUI's nginx instance & restart if necessary?

 

I'm trying to find out if "504 Gateway Time-out" actually appears in the log or is just displayed.

 

#!/bin/bash

NGINX_LOG="/var/log/nginx/error.log"

# Monitor NGINX error log for specific error messages
tail -n0 -F ${NGINX_LOG} | while read LINE; do
    if echo $LINE | grep -q "504 Gateway Time-out"; then
        echo "NGINX error detected: $LINE"
        echo "Restarting NGINX..."
        systemctl restart nginx
    fi
done

 

Its very occasionally but I want to avoid the GUI locking up like this:

366454024_Screenshot2024-04-19at18_43_55.thumb.png.9779f9c097467bd337aa9d433d7ae5c3.png

Link to comment
11 hours ago, dopeytree said:

GUI's nginx instance

I hope you don't mean unraid?
If so: (/etc/rc.d/rc.nginx {check|start|stop|term|restart|reload|renew|update|port|status|upgrade|rotate})

 

I think it should work and "504" should be enough, here are a few additional parameters:
(just for starters - so that the script does not always restart nginx)

#!/bin/bash

NGINX_LOG="/var/log/nginx/error.log"
MAX_RESTARTS=3
RESTART_COUNT=0
SLEEP_TIME=30 # secounds

# Monitor NGINX error log for specific error messages
tail -n0 -F ${NGINX_LOG} | while read LINE; do
    if echo $LINE | grep -q "504"; then
        if [ $RESTART_COUNT -lt $MAX_RESTARTS ]; then
            echo "NGINX error detected: $LINE"
            echo "Restarting NGINX..."
            /etc/rc.d/rc.nginx restart
            ((RESTART_COUNT++))
            sleep $SLEEP_TIME
        else
            echo "Maximum restart limit reached, not restarting NGINX."
            break
        fi
    fi
done

 

Greetings

Edited by Amane
edit for unraid nginx
  • Thanks 1
Link to comment

I have been using unraid for 5+ years. I am just getting in to CRON scheduling now. I am trying to set the schedule for an rsync job to run on the 1st-7th of the month ONLY if it is Saturday (so only the first Saturday of the month). I have set the following which I checked  "crontab guru". It resolves to what I want there, but does not even execute in unraid.

 

25 12 */100,1-7  * SAT 

So it "should run at 12:25pm only on the 1st thru 7th of the month IF it is a Saturday

Is there anyone that can help me out?

Link to comment
21 minutes ago, dopeytree said:

Yeah just the unraid GUI nginx instance

ok, then you must change "systemctl restart nginx" to "rc.nginx restart". Unraid do not use systemd.

/etc/rc.d/rc.nginx restart

 

And I think you should backup your settings and reinstall unraid... or have you already tried that?

Edited by Amane
Link to comment
5 hours ago, camldr69 said:

I have been using unraid for 5+ years. I am just getting in to CRON scheduling now. I am trying to set the schedule for an rsync job to run on the 1st-7th of the month ONLY if it is Saturday (so only the first Saturday of the month). I have set the following which I checked  "crontab guru". It resolves to what I want there, but does not even execute in unraid.

 

25 12 */100,1-7  * SAT 

So it "should run at 12:25pm only on the 1st thru 7th of the month IF it is a Saturday

Is there anyone that can help me out?

ok, that's tricky..

 

Try this:

25 12 1-7 * * [ "$(date +\%u)" -eq 6 ] &&

You can use the costom schedule in the user scripts an input this "workaround".

At 12:25 on every day-of-month from 1 through 7, run and check if it Saturday ([ "$(date +\%u)" -eq 6 ]) and if so (&&), execute the command - But does it really have to be that complicated 😅

 

Edited by Amane
Link to comment

Thank you for the response, I will try this later today.

 

The reason it seems so complicated, is I have rotating backups. I have a backup to a HDD on the 1st, 3rd and 5th saturday of the month and to a different HDD on the 2nd and 4th Saturday of the month. so I wouild have a 2 week window to recover a file if needed. Doing it this way should allow me to only need 2 scheduled backup jobs.

Link to comment

Thank you so much, I have this running now. I have added to the above details to do what I am aiming at. below is what I have now got setup

 

00 8 1-7,15-21,29-31 * * [ "$(date +\%u)" -eq 6 ] &&      this runs at 8:00am ONLY on 1st, 3rd and 5th Saturday of the month

 

00 8 8-14,22-28 * * [ "$(date +\%u)" -eq 6 ] &&           this runs at 8:00am ONLY on 2nd, 4th Saturday of the month

 

  • Like 1
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.