Background script

I’ve been working on a script, so far have just been executing in the script console. Now I need it to run autonomously in the background. The script has 9 functions, they need to execute sequentially one after the other. I tried creating a tag change script with the whole lot in it, the global defined imports at the top seem to need to be declared within each function? What’s the best method to achieve this task?

Is it supposed to be running in the background on the gateway or client? You can use a gateway timer script and call one global script function that will in turn call all the other ones as needed. With this though remember that switching from client context to gateway context can be a pain, since when you developed the script in client/designer context you had a project, and many things may have been assumed by that, where in the gateway context it needs to be specified.

Gateway scoped for all…

Try putting your test script into a global script then the tag event script can simply call the global script.
e.g. TagEventScript: shared.global.yourScript( param1, param2, paramn )
This has the advantage of allowing changes to the global script without interrupting the tag its self but keep the wisdom of @mrogers in mind: You may need to pass project details as parameters so the global script can know project information and/or find project resources.
And yes, each independent function needs to import it’s own resources unless they are declared as members of a parent function:

def parentFunction( param1, param2, paramn ):

import libraries (not used in by memberFunctions but available to them)

from com.inductiveautomation.ignition.common.script.builtin import DatasetUtilities
import urllib2
from urllib2 import URLError, HTTPError

initialize variables available to memberFunctions

logger = system.util.getLogger(‘mainFunction’)
startTime = system.date.now()

def memberFunction1( param1 ):
# logic / processing
logger.info(‘subFunction1 processingTime: %s’ %str(system.date.millisBetween(startTime, system.date.now())))
def memberFunction2( param2, paramn ):
# logic / processing.
logger.info(‘subFunction2 cumulative processingTime: %s’ %str(system.date.millisBetween(startTime, system.date.now())))
def memberFunction3( paramn, param1 ):
# logic / processing
logger.info(‘subFunction3 total processingTime: %s’ %str(system.date.millisBetween(startTime, > system.date.now())))

call memberFunctions in order

memberFunction1( param1 )
memberFunction2( param2, paramn )
memberFunction3( paramn, param1 )

Great, thanks… before the last reply I had 9 separate scripts one calling the next, putting it in one makes life much easier