[SOLVED] Issue with a script running as a cron job

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bananabob
Level 3
Level 3
Posts: 142
Joined: Tue Feb 07, 2012 11:05 pm

[SOLVED] Issue with a script running as a cron job

Post by bananabob »

I am having a problem with the following script.
It runs as a cron job once an hour between 10.00 and 22.00
When the laptop is sleeping/hibernating (never got the hang of which is which) the script obviously will not run. When I reopen the laptop the script starts running after a minute. The problem is that it reports that the lock file has been found, but it doesn't exist, and then does not exit after the aborted message but continues and completes the rsync. I have tested with the lock file being present and it runs as expected exiting after the aborted message.

Why is it "finding" the lock file when it isn't there? And why does it ignore the exit?
How can I fix it?
Thanks.

Code: Select all

#!/bin/bash
STIME=`date +%d-%m-%Y-%T`
echo "*********** Audio to S3TB $STIME ***********"
file="/home/xxxxxxxx/rsync.lock"
if [ -f "$file" ]
then
	echo "Lock file found."
    SUBJECT="Audio to S3TB"
    TOEMAIL="xxxxxxxx@cccccccc"
    /usr/bin/mail -s "$SUBJECT" "$TOEMAIL" <<END
    Lock file found for Audio S3TB
END
    STIME=`date +%T`
    echo "*********** Aborted at $STIME ***********"
	exit
else
	echo "Creating lock file"
	touch $file
	echo "Created lock file"
fi
rsync -lpogtr --stats --rsh=ssh --recursive --times --delete --exclude '.*' /home/xxxxxx/ /mnt/xxxxxxx

rm $file
if [ -f "$file" ]
then
	echo "Could not remove lock file."
else
	echo "Lock file removed"
fi
STIME=`date +%T`
echo "*********** Finished at $STIME ***********"
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Issue with a script running as a cron job

Post by 1000 »

TIP:
1. From man rm
-v, --verbose explain what is being done
I mean that you can use rm -v $file

2. You can create log, for example in side cron
* * * * * script > /home/xxxxxxxx/script.log

3. You can check if cron can remove file from /home/xxxxxxxx/
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Issue with a script running as a cron job

Post by Termy »

There's actually a program called flock(1) which you could use for file locking in shell scripts. If all else fails, maybe you could try that? You may already have it available, but it's part of the 'util-linux' package.

When you run the script normally, without waiting for Cron to use it, does it work as intended?
I'm also Terminalforlife on GitHub.
bananabob
Level 3
Level 3
Posts: 142
Joined: Tue Feb 07, 2012 11:05 pm

Re: Issue with a script running as a cron job

Post by bananabob »

Thanks for the replies.

I have restructured the script to use flock. I am not sure if flock fixed the issue or the restructure of the script did, but I am no longer troubled by the problem.

@termy thanks for the heads up on flock, I had never heard of it.

Here is my new script.

Code: Select all

#!/bin/bash
STIME=`date +%d-%m-%Y-%T`
echo "*********** Audio to S3TB Started $STIME ***********"
file="/home/xxxxxx/rsync.lock"
exec {FD}<>$file
if ! flock -x -n $FD
then
	echo "Aborting Another Instance Running"
    SUBJECT="Audio to S3TB"
    TOEMAIL="xxxxxxs@xxxxxx"
    /usr/bin/mail -s "$SUBJECT" "$TOEMAIL" <<END
    Lock file found for Audio S3TB
END
else
    echo "Creating lock file"
    touch $file
    echo "Created lock file"
	echo "Running Rsync"
    rsync -lpogtr --stats --rsh=ssh --recursive --times --delete --exclude '.*' /home/xxxxxx/ /mnt/xxxxxx
    rm $file
    if [ -f "$file" ]
    then
        echo "Could not remove lock file."
    else
	    echo "Lock file removed"
    fi
fi

echo " "
STIME=`date +%T`
echo "*********** Audio to S3TB Finished at $STIME ***********"
Locked

Return to “Scripts & Bash”