Sample Header Ad - 728x90

How to add file markers to check if script is already running

1 vote
2 answers
107 views
I wonder if I can get some help with a project I'm working on. I have a Synology NAS. I found a Community Package that autoruns a script of my creation anytime a USB drive is plugged in to one of the drives. My script copies images and movie files to a given folder from all USB drives/Sandisk cards listed in the script into a specific folder on the Synology. The autorun package runs the script **every** time each drive is plugged in. The problem is if I plug in four USB drives one after the other within 15 seconds, it copies all four drives four times. Instead I want it wait 15 seconds to allow me to plug in all USBs, and then copy all drives once. My script is:
#!/bin/bash
#
var=$(date +"%FORMAT_STRING")
now=$(date +ā€%m_%d_%Y_%sā€)
printf "%s\n" $now
today=$(date +%m-%d-%Y-%s)
rsync -avz --prune-empty-dirs --include "*/" --include="*."{cr2,CR2,mov,MOV,mpg,MPG,dng,DNG,jpg,JPG,jpeg,JPEG} --exclude="*" /volumeUSB1/usbshare/ /volume1/KingstonSSD/Camera_Loads/Sandisk-${today}
rsync -avz --prune-empty-dirs --include "*/" --include="*."{cr2,CR2,mov,MOV,mpg,MPG,dng,DNG,jpg,JPG,jpeg,JPEG} --exclude="*" /volumeUSB2/usbshare/ /volume1/KingstonSSD/Camera_Loads/Sandisk-${today}
rsync -avz --prune-empty-dirs --include "*/" --include="*."{cr2,CR2,mov,MOV,mpg,MPG,dng,DNG,jpg,JPG,jpeg,JPEG} --exclude="*" /volumeUSB3/usbshare/ /volume1/KingstonSSD/Camera_Loads/Sandisk-${today}
rsync -avz --prune-empty-dirs --include "*/" --include="*."{cr2,CR2,mov,MOV,mpg,MPG,dng,DNG,jpg,JPG,jpeg,JPEG} --exclude="*" /volumeUSB4/usbshare4-2/ /volume1/KingstonSSD/Camera_Loads/Sandisk-${today}
My goal is to have the script wait a given time (say 15 seconds), to allow me to plug in all four USBs. Then after 15 seconds, run the code one time. I guess I need to check if the code is already running for any of the USBs plugged in. Terminate the current script if so, copy files if not. I found this, I'm wondering if I can tweak it and add it to mine to check if any other instances of my script are running and terminate if so... or copy files if not:
if [ ps -ef | grep "script.sh" | grep -v grep | wc -l -gt 1 ] ; then
echo "RUNNING...."
else
echo "NOT RUNNING..."
fi
Any chance anyone would help with a solution? Thanks in advance!
Asked by Matt (13 rep)
Feb 11, 2023, 10:30 PM
Last activity: Feb 12, 2023, 11:20 AM