how to ...

custom scripts

The infx script service is used to execute your own scripts. The service takes care of setting up the environment, then executes your script, and records its output.

You can use this service to create new scripts, or to re-use your existing scripts.

The difference between a script and a utility is that a script can perform actions on the instance, not just get status information.

For example, a utility might list user connections, where a script might be used to terminate user connections.

For the purposes of this example, I will create a script that locates databases that are not logged, and turns their logging on.

First, I will use the infx status service to output the required database information. Next, I will write a script that takes this output, and calls the infx backup service to run a fake back up and turn logging on.

In real life, you would probably need to do a real backup as well.

You could also use the dbs utility from the custom utilities example to list the database information.

get database info

First, we need to output the list of databases, and whether they have logging or not.. See the db list for more column information.

bob001@bobii:/infx/local/script>infx status list=db_list vars=name+is_logging
monitor_jgh1  1
sqltest       0
sysadmin      1

write the shell script

The script must conform to the following rules, otherwise infx will not execute it.
  1.     Located under /infx/local/script
  2.     Owned by informix
  3.     Not writable by others
  4.     Executable
Next, we will use the status output in our script to find the unlogged database, then call the infx fake backup service to change the logging mode.

#!/bin/bash

infx status list=db_list vars=name+is_logging | while read dbname logging ; do
 if [ ! -z "$dbname" ] ; then
        if [ "$logging" -eq 0 ] ; then
                printf "infx backup type=fake unbuff=$dbname\n"
                infx backup type=fake unbuff=$dbname
        fi
 fi
done


That is all you need to put in this script. The infx script service will take care of setting the PATH and other variables. It will also record its output and status in the service log file. 

Now we call the script service, and pass it this scripts name. 

bob001@bobii:/infx/local/script>infx script script=dblog
infx backup type=fake unbuff=sqltest
ontape -s -F -L 0 -U sqltest -t /dev/null
Archive to tape device '/dev/null' is complete.

Program over.


Run the template again, and we see the logging mode changed.

bob001@bobii:/infx/local/script>infx status list=db_list vars=name+is_logging
monitor_jgh1  1
sqltest       1
sysadmin      1

pass parameters

Lets add an option to this script to choose between buffered and un-buffered logging. To do this we will pass a parameter to the service, logmode=, the type of logging mode we want to set.

To set unbuffered logging

infx script script=dblog logmode=unbuff

To set buffered logging

infx script script=dblog logmode=buff

The parameter from the command line will be automatically set in the environment of the script. The script should provide a default value, or produce an error message if something is missing. Changes highlighted.

#!/bin/bash

INFX_LOGMODE=${INFX_LOGMODE:-unbuff};   # default to unbuffered logging

infx status list=db_list vars=name+is_logging | while read dbname logging ; do
 if [ ! -z "$dbname" ] ; then
        if [ "$logging" -eq 0 ] ; then
                printf "infx backup type=fake unbuff=$dbname\n"
                infx backup type=fake $INFX_LOGMODE=$dbname
        fi
 fi
done


The command line parameters come from the infx service, direct into the environment of the script, with the variable name being prefaced with INFX_ to prevent name clashes.

Use the config.ini file to set or modify any other environment variables you need for your script.