This post is an excerpt from the Maximo 76 Scripting Features guide.
Starting 76 we can write scripts for cron tasks too. It follows the same principle as the endpoint handler. We have a cron class – com.ibm.tivoli.maximo.script.ScriptCrontask that helps achieve
this. We need to register that class as a new crontask in the crontask application. We then will create an instance of it and set the “scriptname” property to the name of the script that we want this task to run. Next we need to set the schedule and last but not the least – need to define the
script. We are then good to go running this newly defined script with the crontasks. It is no different than any other crontasks and you can activate/deactivate the task or change the schedule as you would do in any other cron task.
If you are wondering why we need this when we already have an escalation which is a crontask that works of a given Maximo object. The answer is simple – we often need to schedule jobs that are not just based off Maximo objects. So in those cases, this crontask based scripts would come in handy.
Below we develop a cron script to log an error when the count of maxsessions exceeds a configured limit.
We will first need to register the script crontask definition. For that we will use the Crontask app and the class name would be com.ibm.tivoli.maximo.script.ScriptCrontask. We will also need to create a cron task instance.
In that instance we can set the frequency to 30 sec.
We can set the SCRPTNAME param value to SESSIONCOUNT. This implies that we will create a script names SESSIONCOUNT to do the job.
Next we create the script SESSIONCOUNT. A sample script in py is shown below. Note that the “runasUserInfo” implicit var will hold the userinfo for the cron job.
from psdi.server import MXServer cnt = MXServer.getMXServer().getMboSet("maxsession", runAsUserInfo).count() if cnt>1: service.logError("cnt exceeds 1::" + str(cnt))
Next we activate the crontask instance. You need to wait a minute or so for the task to start.
You can use another browser instance to login to maximo – just to create another maxsession record. That is when you can see that log that says “cnt exceeds 1::”.
Note that we hard coded the count limit. In this step we can softcode this by leveraging the SCRIPTARG parameter in the Crontask. We can set that to 1. We need to now save and reload the task.
Next we modify the script to leverage that SCRIPTARG parameter using the implicit var called “arg” as below.
from psdi.server import MXServer argval = int(arg) cnt = MXServer.getMXServer().getMboSet("maxsession", runAsUserInfo).count() if cnt>argval: service.logError("cnt exceeds 1::" + str(cnt))
You can now repeat step 3 to see if you get the error log.