Run an Automation Script from a Cron Task in Maximo
Cron tasks are jobs that run automatically on a fixed schedule within Maximo. They include important jobs such as the PM Work Order Generation Cron Task, the Inventory Replenishment Cron Task, and others. There may be times when you come across some requirements where creating your own Cron Task provides the best solution. In previous releases of Maximo, it was required to write a custom Java class to perform the action of the Cron Task when the schedule came due. Therefore, many times we turned to Escalations to provide solutions where scheduled tasks were required. In Maximo 7.6, you can now run an Automation Script directly from a Cron Task. This article will walk through how to configure this.
Step 1: Create a new Automation Script
- Log into Maximo as an administrative user.
- Navigate to the System Configuration > Platform Configuration > Automation Scripts application.
- From the Select Action menu, choose the Create > Script option.
- Populate the appropriate fields on your new script:
- Script: CRONSCRIPT
- Description: Automation Script called from a Cron Task
- Script Language: python
- Log Level: ERROR
- Source: print ‘Automation Script called from a Cron Task’
- Click the Create button to create the new script
- Press the Close button on the ensuing dialog
- Locate the new CRONSCRIPT script in the list, and open the script
- Update the Script Source as follows, and press the Save button when finished:
####################### # Script to generate an SR for a monthly equipment audit ####################### from psdi.server import MXServer # Get the variables from the cron task arguments srDesc = 'Monthly Equipment Audit' srOwner = 'WILSON' if arg: srDesc,srOwner = arg.split(',') # Create the SR srSet = None try: srSet = MXServer.getMXServer().getMboSet('SR', runAsUserInfo) srMbo = srSet.add() srMbo.setValue('DESCRIPTION', srDesc) srMbo.setValue('OWNER', srOwner) srSet.save() finally: if srSet: srSet.close() ####################### # End of Script #######################A few things to note with this source code:
- This script will create a new Service Request and populate the Description and Owner fields
- Notice that there are a few implicit variables that Maximo creates for use in the script:
- arg: This is a delimited list of arguments that you wish to pass into the script. We will show below how to populate the arguments when we create our Cron Task.
- runAsUserInfo: This variable gives the script access to the user profile that the cron task is running as. This will allow you to fetch Mbo Sets or perform other tasks with a set of credentials.
- instanceName: While this variable is not utilized in the above script, it represents the Cron Task Instance Name that the script is being called from. This could be useful in the cases where you have multiple cron task instances and need to distinguish which one is actively running.
- You can utilize the
MXServer.getMXServer().getMboSet()method to fetch MBO sets of any record set that the runAsUserInfo profile has access to. Please note the strategy for closing MBO sets when you are finished with them. This alerts the JVM that the object is ready to be garbage collected. It’s important to follow this strategy, or a similar strategy, to avoid potential memory leaks with the JVM.
Step 2: Create a new Cron Task
- Log into Maximo as an administrative user.
- Navigate to the System Configuration > Platform Configuration > Cron Task Setup application.
- Click the New Cron Task Definition button on the toolbar or under the Common Actions menu.
- Cron Task: MYCRON (give your cron task an appropriate identifier)
- Description: My Cron Task (give your cron task an appropriate description)
- Class: com.ibm.tivoli.maximo.script.ScriptCrontask (this is the key to the setup, the value is case sensitive and must be exact)
- Click the Save button
- Click the New Row button under the Cron Task Instances table
- Cron Task Instance Name: MYCRON01 (give your cron task instance an appropriate identifier)
- Description: My Cron Task Instance 01 (give your cron task instance an appropriate description)
- Schedule: 1M,0,0,0,1,*,*,*,*,* (this will schedule the task to run on the first of every month; use the schedule builder to select the schedule that you want your cron task to run on)
- Run as User: MAXADMIN (select the user that you want the cron task to run as)
- Active: Yes
- Click the Save button
- There should be two new parameters in the Cron Task Parameters table at the bottom of the page.
- SCRIPTARG: This parameter is used to pass in variables to the script. You can separate variables by a comma or other delimiter in order to pass in more than one variable to the script.
- SCRIPTNAME: This parameter tells Maximo which Automation Script to trigger when the cron task runs.
- Fill out the above parameters and click the Save button. The SCRIPTNAME parameter is required, but the SCRIPTARG parameter is optional.
As an IBM Maximo mobile solution provider, A3J Group consistently improves the functionality and features...
Pretend you are going to take a real vacation, two weeks in Spain with no work laptop. You have planned...
A3J Group is happy to announce the latest MxMobile releases below. The releases will be available on...
Question: The argument values defined at Crontask SCRIPTARG and also in the script itself. So since the argument values are defined (hardcoded) in the script, what is the value at SCRIPTARG for?
The SCRIPTARG Cron Task parameter is converted into an implicit variable in the Automation Script called arg. This variable can then be utilized in the script. For example, you might have a script that has slightly different behavior based upon which Site you’re operating on. You can use the SCRIPTARG parameter to pass in the Site ID for the current Cron Task Instance. A different instance on a different schedule might be for a different Site. Rather than having two scripts that do mostly the same thing, you can pass the Site ID into the one script. Hopefully this helps.
Very good script and feature, thanks a lot
Thanks your help in advance.
in my case the crone task run with similar start Time and End time. And also the associated script not working. Below is the content of the script. i can’t figure out the cause. can you help me please?
from psdi.server import MXServer;
from java.sql import Statement;
from java.sql import PreparedStatement;
from java.sql import Connection;
from java.sql import ResultSet;
from psdi.mbo import Mbo;
mxServer = MXServer.getMXServer()
userInfo = mxServer.getSystemUserInfo()
conn_key = userInfo.getConnectionKey()
conn = mxServer.getDBManager().getConnection(conn_key)
updatesql = “update SR set COMMENTS = ‘Test 1’ where ticketid = ‘SR575229’;”
stmt = conn.createStatement()
results = stmt.executeUpdate(updatesql)