I need some help. I’m having a script ‘Script 1’, which will call ‘Script 2’ to run in background which checks something periodically. But I want the Script 2 to get started only once, even Script 1 is called multiple times. Is there a way to do it?
It would be even more helpful, if someone suggests some commands to achieve this.
Thanks in advance
Sure, you can put something like this at the top of
Script2:That will stop
Script2from ever running again by using a sentinel file, unless the file is deleted of course, and it probably will be at some point since it’s in/tmp.So you probably want to put it somewhere else where it can be better protected.
If you don’t want to stop it from ever running again, you need some mechanism to delete the sentinel file.
For example, if your intent is to only have one copy running at a time:
And keep in mind there’s a race condition in there that could result in two copies running. There are ways to mitigate that as well by using the content as well as the existence, something like:
There are more levels of protection beyond that but they become complex, and I usually find that suffices for most things.