Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification regarding shell script scheduling

Tags:

bash

shell

I have 2 shell scripts say a.sh and b.sh scheduled in cron where the first one a.sh is scheduled to run at 5am and the second script b.sh will run at 7am.

There are pre-conditions which state that :

1) Only one can run at a time.
2) The second script b.sh should run only after the first script a.sh completes.

Now the problem is the first script a.sh may complete its execution before 7am or may exceed 7am in some cases and in the case where it exceeds 7am the second script is also started which will break one of the pre-conditions.

Here I cannot start the second script later that day as its very crucial script.

Here how can I make both the scripts run one after the other without the first script stepping on the second script.

like image 680
Dark Matter Avatar asked Jan 27 '26 21:01

Dark Matter


1 Answers

Have the a.sh script create a lockfile when it's complete (last line).

touch ~/a.lck

At the beginning of b.sh:

if [ ! -f ~/a.lck ]; then
  exit 1 #or whatever you want it to do if the lock file is not there.
fi

Don't forget to rm the lockfile either at the beginning of a.sh or the end of b.sh!

like image 198
MerrillFraz Avatar answered Jan 29 '26 14:01

MerrillFraz