No internet connection
  1. Home
  2. How to

How do you prevent multiple instances of the same run forever scripts from running?

I have a background/loop script. If I trigger it once and issue the "Stop All Running Commands" command from the SF menu or keyboard command one error message is displayed via notifications.

I noticed that if I trigger the background script multiple times - let's say three times for example - and I stop all running commands then I get three error notifications which leads me to believe that there are multiple instances of the background script running.

Is this right and if so is there a way to prevent this? I've tried using a globalState at the top of the script to prevent multiple instances running like this:

// Prevent multiple instances of this background process from running
// If this the first time the script has been launched then globalState.backgroundScriptRunning is undefined
 
if (globalState.backgroundScriptRunning == true) { throw 0 };
// Now set globalState.backgroundScriptRunning to true
globalState.backgroundScriptRunning = true

This does work but if I stop all running commands then globalState.backgroundScriptRunning will be set to true and will prevent the script from running again.

Is there another way to do this?

//CS//

Solved in post #2, click to view
  • 3 replies
  1. Hi Chris,

    It's likely that you're using a modified version of the original runForever script I wrote. The original takes care of this exact issue so that you'll never see multiple scripts running at the same time.

    ReplySolution
    1. Try to share the full script (minus the main function, you can leave that out)

      1. Oh, I see what I need now in this thread:
        Select Track as trigger #post-7

        Thanks

        //CS//