[Plugin] CA User Scripts


Recommended Posts

This is probably a simple question, but after an hour of searching, I have yet to find a definitive answer.

 

I have a working script that weekly will rsync several folders from my array to an external USB drive mounted with Unassigned Devices.  I have two drives (External_USB_1 and External_USB_2) which I swap on the beginning of the month, and I keep the unconnected drive off site.  My current solution has me manually editing my script each time I swap drives, to specify the correct destination drive.  I would much rather set and forget the script, and have it check for the drive to be present before executing rsync.

 

What would be the best way to check if these drives are present and mounted? 

 

UPDATE:  I have a solution that seems to work, but if someone has a better one, I'm open to your ideas.

 

I ended up using mountpoint. 

 

#!/bin/bash
#arrayStarted=true

if mountpoint -q "/mnt/disks/External_3TB_1"
then
   rsync -avh "/mnt/disks/Samsung_USB/UNRAID_Backups" "/mnt/user/Media/Music" "/mnt/user/Photos/ORIGINALS" "/mnt/user/Backup/appdata backup" "/mnt/disks/External_3TB_1"

else
   echo "External_3TB_1 not mounted"
fi

if mountpoint -q "/mnt/disks/External_3TB_2"
then
   rsync -avh "/mnt/disks/Samsung_USB/UNRAID_Backups" "/mnt/user/Media/Music" "/mnt/user/Photos/ORIGINALS" "/mnt/user/Backup/appdata backup" "/mnt/disks/External_3TB_2"

else
   echo "External_3TB_2 not mounted"
fi

 

Edited by ConnerVT
Solution?
Link to comment

I'm trying to run a script with the following custom cron schedule:

 

50 2 * * 2 [[ $(date +%e) -le 7 ]]

 

I wanted the script to run the 1st week of every month on Tuesday at 2:50am but it didn't run this past Tuesday.  I just copied the cron schedule for my parity check and made some modifications.  Do you see anything wrong with this cron schedule?

 

Link to comment
28 minutes ago, niwmik said:

I'm trying to run a script with the following custom cron schedule:

 

50 2 * * 2 [[ $(date +%e) -le 7 ]]

 

I wanted the script to run the 1st week of every month on Tuesday at 2:50am but it didn't run this past Tuesday.  I just copied the cron schedule for my parity check and made some modifications.  Do you see anything wrong with this cron schedule?

 

I have a cron I run in Ubuntu that is for the first Saturday of every month. My syntax is:

 

0 0 * * 6 [ $(date +\%d) -le 07 ] && /script/path/follows/here.sh

Link to comment
21 hours ago, cybrnook said:

I have a cron I run in Ubuntu that is for the first Saturday of every month. My syntax is:

 

0 0 * * 6 [ $(date +\%d) -le 07 ] && /script/path/follows/here.sh

 

Thanks for the tip.  I changed the cron schedule to "50 2 * * 4 [ $(date +%d) -le 07 ]" and it appears to have run this morning based on what it is in the logs.

 

Nov  4 02:50:01 Atlas crond[2264]: exit status 2 from user root [ $(date +\%d) -le 07 ] /usr/local/emhttp/plugins/user.scripts/startCustom.php /boot/config/plugins/user.scripts/scripts/AutoBackupVM/script > /dev/null 2>&1
 

However, AutoBackupVM script doesn't appeared to have ran.  I just ran it manually to test and it ran successfully.  Is there something else I'm missing?

 

Link to comment
2 minutes ago, wgstarks said:

i'm running the clear_array_drive script. The status window shows 2.2 MB/s. At this rate it's going to take about 21 days to complete.

Will it speed up? It's a part of the shrink array process but is it really necessary?

Speed sounds about right, the script is not optimized at all.

 

You can either write all zeroes to the data drive, or rebuild parity, either way works, but rebuilding parity is much quicker.

Link to comment
4 minutes ago, JonathanM said:

Speed sounds about right, the script is not optimized at all.

 

You can either write all zeroes to the data drive, or rebuild parity, either way works, but rebuilding parity is much quicker.

That's what I thought. Drive has already been emptied, so now I can just stop the array, remove the drive, create new config and rebuild parity. Did I miss anything?  

Edited by wgstarks
typo
Link to comment
20 hours ago, cybrnook said:

What is AutoBackupVM? I use my own script to backup VM's. Seems the script is exiting with status 2, so something is not happy.

 

When you run it by hand, after it completes what does "echo $?" return

 

AutoBackupVM is a script written by an Unraid user that I use to backup my Windows 10 VM.  I've never had an issue with it before.

 

I clicked "RUN SCRIPT" in User Scripts to manually run the script so I didn't have access to a terminal screen.

 

As a test, I changed the cron schedule from "Custom" to "Scheduled Monthly" and changed Monthly schedule in Scheduler to run on the 5th.

 

I didn't see anything in the logs this morning but AutoBackupVM did run successfully.  As far as I can tell, the script only fails when I set it to a custom cron schedule.

 

 

 

Link to comment
  • 2 weeks later...
4 hours ago, HarryMuscle said:

Does anyone know the GitHub repo for this plugin?  I'd like to look though the source code.

 

Thanks,

Harry

If you have the plugin installed then you can always look at the .txz file in the plugins folder on the flash drive without bothering to go to GitHub.

Link to comment
On 11/4/2021 at 9:47 AM, niwmik said:

50 2 * * 4 [ $(date +%d) -le 07 ]

You're going to want to add a cron job via crontab or by placing an application .cron file within /config/dynamix on the flash drive.  User scripts will eventually disallow a cron entry like this  (when I get to it)

Link to comment

I don't know if this belongs here:

Had serious trouble with rsync commands running wild. Started something like "rsync -avh --delete \\A \\b - huge job, almost 10 TB. I canceled it via userscript-plugin but it did not work. In the end i had about 10 rsync scripts copying the same stuff over and over (because the first ones altered the date/time and the later scripts did not notice). Annoying...

Is there a way to include this (i needed "killall rsync" to clean up this mess) or should i provide something with the script to avoid this behaviour.

Link to comment

I just noticed my cron based scripts last ran on 19th Nov.  The dropdown daily, weekly, hourly ones are running fine.

I have some scripts for this however :

 

*/5 * * * *   - very 5 minutes sync video camera data to hidden laptop

30 23 * * * - Backups every night at 23:30

 

Any idea where I can go to rectify this ?  In there a cron process that has stopped or something ?

Link to comment
On 10/27/2021 at 4:51 PM, Noim said:

@Squid 

My issue still persists. Here is a test script:

61713642_Screenshot2021-10-27at16_44_25.thumb.png.4047f199b5c9e7ae2deb2263f4e98974.png

 

After aborting it, the script process is still running:

 

1366726180_Screenshot2021-10-27at16_45_20.png.5644734dc475c1f64e11ea83d4d7d675.png

 

What am I doing wrong?!

 

This is how it gets spawned if I press background process:

550709164_Screenshot2021-10-27at16_48_27.png.0be54c7f5dc6a3931b9911edca04c345.png

The process 10197 survives if I press abort.

 

Plugin is up-to-date and running on unraid 6.9.2. 

 

Because nobody cared, and the dev doesn't even publish the plugin on GitHub, so I could make a pull request I investigated by myself.

The abort how it is implemented is flawed.

 

This is my fix. Not elegant, but it works better than the current version.

Starting at line 115 in exec.php:

  case 'abortScript':
    $script = isset($_POST['name']) ? urldecode(($_POST['name'])) : "";
    
    $pid = file_get_contents("/tmp/user.scripts/running/$script");
    exec("pkill -TERM -P $pid");
    exec("kill -9 $pid");
    $processListOutput = null;
    exec("ps aux | grep -i '/tmp/user.scripts/tmpScripts/$script' | grep -v grep", $processListOutput);
    foreach ($processListOutput as $emergencyKill) {
      $emergencyKill = str_replace("root","",$emergencyKill);
      $emergencyKill = trim($emergencyKill);
      $rawKill = explode(" ",$emergencyKill);
      logger("Kill pid: ".$rawKill[0]);
      exec("kill -9 ".$rawKill[0]);
    }
    @unlink("/tmp/user.scripts/running/$script");
    file_put_contents("/tmp/user.scripts/finished/$script","aborted");
    file_put_contents("/tmp/user.scripts/tmpScripts/$script/log.txt","Execution was aborted by user\n\n",FILE_APPEND);
    break;

 

The original version didn't kill all running script instances. And the grep command not even found the correct processes if I tried it manual. 

Link to comment
  • 4 weeks later...

I created a script to run in the background, but today the log shows Full logs for this script are available at /tmp/user.scripts/tmpScripts/bypy_mmd/log.txt.Then the script failed to run after retrying several times automatically, and I had to restart the server to run normally.How to solve my trouble?

Link to comment
  • 2 weeks later...
2 hours ago, bobokun said:

Is there a way to skip the Userscript from triggering if the previous run is still running? Currently it seems like the userscript will trigger a new run at the scheduled time even if the previous one is not complete.

I believe that the script itself would be responsible for ensuring that only a single instance can run at a time. Adding one of the solutions from this page https://unix.stackexchange.com/questions/48505/how-to-make-sure-only-one-instance-of-a-bash-script-runs would probably do the trick.

Link to comment

I have a problem with the plugin. I updated the user scripts plugin and afterwards it disappeared from my unraid menu and if I try to open it via the plugins > installed plugins tab and click on it, I get a black screen. Reinstall did not fix the problem.
I am on Unraid Version: 6.10.0-rc1
Any ideas?

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.