Trouble running script in Cron Jobs

If this is the entire script, you are missing something… you need the first line.

#!/bin/bash

pseudopid="`pgrep -f $0 -l`"
actualpid="$(echo "$pseudopid" | grep -v 'sudo' | awk -F ' ' '{print $1}')"

if [[ `echo $actualpid` != "$$" ]]; then
    echo "Another instance of this script is already running!" >&2
    exit 1
fi

I too had issues with multiple instances running and I resolved it by creating a lock directory in /tmp. See the code below. You will need it all and change the pieces you want. it will toss you an error message and exit if you have an instance already running.

	if ! mkdir /tmp/multi_report.lock; then
		printf "Script is already Running... Exiting\n" >&2
		printf "If this message is in error, remove '/tmp/multi_report.lock' directory or just reboot TrueNAS to recover.\n" >&2
		exit 1
	fi
fi

trap 'rm -rf /tmp/multi_report.lock' EXIT 	# Remove the lock directory on exit

Hope this helps.

EDIT: I did not run your script or proof it. Trying to use PIDs sucks.

1 Like