0

I have a script called "upcall" which calls 4 different scripts. In upcall I call them in the way show. The first part of the script works when I run the script directly (bash upload_cloud1), but does not when its called from the script below. Im sure there is a way to fix this, but just not sure what it is. I have it currently setup in crontab to run every 15 mins to check for used space.

#!/bin/bash

if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then
    echo "This script is already running with PID `pidof -x $(basename $0) -o %PPID`"
    exit; fi

count=$(</opt/rclone/scripts/upcount)
size=$(df -k /dev/sda2 | tail -1 | awk '{print $3}')
if [ "$size" -gt "234003200" ]; then
bash /opt/rclone/scripts/upload_cloud${count}
else
echo "Not full yet"
fi
  • you could rewrite your scripts as [services](https://unix.stackexchange.com/questions/15348/writing-basic-systemd-service-files) – jeremysprofile May 19 '20 at 21:16
  • 2
    Does this answer your question? [What is the best way to ensure only one instance of a Bash script is running?](https://stackoverflow.com/questions/1715137/what-is-the-best-way-to-ensure-only-one-instance-of-a-bash-script-is-running) or [this one](https://stackoverflow.com/a/61773885/6770384). There are a lot of duplicates for this type of question. I'm sure you find the answer you need in one of those. – Socowi May 19 '20 at 21:51
  • @Socowi Locking the file didnt work either. – Dimrain13 May 19 '20 at 23:40
  • 1
    *"`Didn't work`"* does not help. You have to show exactly what you did and describe the *exact* problem you encountered. – Socowi May 20 '20 at 07:16
  • I attempted to lock the file. The file was confirmed to be locked. I then called the script twice and it ran both instances at the same time. Im going to try the service method later today. – Dimrain13 May 20 '20 at 21:22
  • What do you mean "locking the file"? Show us **exactly** what you did. How did you confirm the file was locked? Etc. – Charles Duffy Jul 28 '23 at 13:34
  • ...if file locking _truly_ doesn't work it may be that you're on a filesystem that lacks the requisite support, but a usage bug is more likely (like locking a different file for each instance, or trying to lock a file that's read-only to the running process and having the attempt fail, etc). – Charles Duffy Jul 28 '23 at 13:35

1 Answers1

0

IS RUNNING Function

#!/bin/bash

IS_RUNNING(){
pidof -o %PPID -x -- "$0" >& /dev/null
}

IS_RUNNING && exit