Ravish Mody

One of our subscriber middleware_guy was having issues to run a WLST script using Cronjob and wanted us to help him out with it to solve his problem by providing him a detailed steps or the script which would do the same for him, hence we thought that it would be better if we create an article on this issue so that everyone can take the advantage and can run any WLST script using cronjob.

The requirement was to run the WLST script every hour so that they can get some information from the running Weblogic server, we had suggested to use the cronjob to this part of the job, as running WLST script continually in the background takes lot of usage and we have been reported that this causes high CPU issue as well, thus using cronjob would be a best choice in this situation. For the new guys the question would come….

What is a Cronjob ?

Cronjob is a a job scheduler which is used for time based scheduler which comes with all Unix based operating systems and one of the best tool for the all administrators. You can use cronjob to run any commands or scripts periodically in a certain time based or dates wise manner which would automate the job of an administrators like taking backup of the logs every week, clean up the logs which are older then 30 days etc.

To use cronjob there are predefined scheduling definitions which has to be used as show in the diagram, which would give you a better picture what all things are needed to activate a cronjob.

* * * * * [command/script to get executed]

┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week [0 to 7] (where Sunday=0 or 7)
│    │    │    └────────── month [1 to 12]
│    │    └─────────────── day of month [1 to 31]
│    └──────────────────── hour [0 to 23]
└───────────────────────── min [0 to 59]

Let’s take an example, as per the requirement middleware_guy wanted that the WLST script should be called every hour, so to get that job done following would be the command which has to go in the “crontab” file.

0 * * * * [script]

This would make sure that the given script would run every hour, however to run a WLST script we would have to make sure that the weblogic environment is set properly so that it can call the WLST script or else it would get an exception. So the below shell script would do that job to sent the weblogic environment and then call the WLST script as well. Create a file called “WLST.sh” and put the below code in it

#!/bin/bash

# ************* Setting the Environment ***********************
DOMAIN_HOME="/home/testusr/WLS103/user_projects/domains/Domain_7001"
. ${DOMAIN_HOME}/bin/setDomainEnv.sh $*

echo "Environment has been set....."

# ************* Changing the directory where all the related files are needed ***********************
cd home/testusr/WLS/App_State

echo "Calling the PYTHON script....."

# ************* Calling the WLST script  *****************
java weblogic.WLST application_status.py

NOTE:

Make sure that all the files to run the python script and the WLST.sh file should be in the same directory folder so that you would not get filenotfound error.

Hence below line would be given in the crontab file to run the WLST script given in the WLST.sh file

0 * * * * /home/testusr/WLS/App_State/WLST.sh >> home/testusr/WLS/App_State/out.log

Same way if you want to run your script in every 15 minutes (like 1:15, 1:30,1:45,2:00,2:15…) then do the following:

00,15,30,45  *   *  *  * /home/testusr/WLS/App_State/WLST.sh >> home/testusr/WLS/App_State/out.log

Regards,
Ravish Mody