[Solved] How to run in script, if not already executed?

Hi,

I want to run my “rclone move” command in a script, using CRON. I saw @ajki scripts and tried the upload one.
But it seems it fails because it’s like my script runs many times. And I set only 5 transfers and 10 checkers.

I’m running Debian 8 on a dedicated server.

I use a staging folder for all the renames to happen locally before being uploaded. This is my script for moving the staging folder to Amazon Cloud Drive:

#!/bin/bash
 ps_out=`ps -ef | grep "rclone move" | grep -v 'grep' | grep -v $0`
 result=$(echo $ps_out | grep "$1")
 if [[ "$result" != "" ]];then
     echo "Rclone move is already Running."
 else
     echo "Rclone move is Not Running.  Starting it now..."
 	rclone move /mnt/user/Media/Staging/ ACDEncrypted:/Media/ --transfers=12 --min-age 1m --stats 15s
 fi
exit

You will note that it checks to see if rclone move is running before allowing the move script to run. This makes sure that only one copy of rclone move is running at a time.

I run this once every few minutes via cron jobs on my unraid system.

Also, it’s quick and dirty, but gets the job done. My bash isn’t up to par with some others on here.

1 Like

if pidof -o %PPID -x “$0”; then
echo “$(date “+%d.%m.%Y %T”) Already running, exit”
exit 1
fi

The above code will always exit script if already running, just make sure you have !/bin/bash and not !/bin/sh

I also preform check if there are any new files in my upload folder before running rclone.

uploadcheck=$(find /storage/local/ -mindepth 1 -type f -mmin +15)
if [[ -n $uploadcheck ]]; then
rclone
fi

2 Likes

@Ajki I c/p your script and this “if pidof …” doesn’t work for me. I just can’t find the scripts name PID.
@xyber411 Thanks, i’ll try this one and get back to you ASAP.

It runs perfectly :slight_smile:

put in your script echo “$0” that should always be script name and $1,2,3 are the parameters you are passing.
Not sure what OS you are using but its build in function in Ubuntu http://manpages.ubuntu.com/manpages/precise/man8/pidof.8.html

1 Like