Category Archives: PASE / AIX / SSH / Shell

Aix is IBM’s Unix

Automating deployment of IBM i SQL Database changes to a remote system

If you ever wanted to automate your SQL changes to a remote systems here’s one way of accomplishing that.

First you’ll have to  manually create two local sql files that have the database SQL that you would like to run.  The 1st file up{version#}.sql  will be ran when you want to promote your code, and the 2nd file down{version#}.sql will be ran when you want to rollback your changes.  The version# allows you to keep a history of what changes were made each time you this or you could tie it to source control commit#/version#/tag etc.. use whatever helps you.

Now that we have these two files we have to securely send them over to our IBM i.  Simply run this command on your local computer in a shell.  If your on windows install this so you can run it (http://cmder.net/)

scp -r "/sqlsource/migrations" myuser@mysystem.example.com:/sqlsource/migrations

This will recursively copy the local directory /sqlsource/migrations to the remote system mysystem.example.com in the directory /sqlsource/migrations .  This is the directory that must contain our up and down sql scripts.

Now that we have the source on the remote system we need to create two local shell scripts that we’ll send to the remote system and call to promote or rollback the sql.  Create the two files below on your local machine:

PromoteSQL.sh

system -i "RUNSQLSTM SRCSTMF('/sqlsource/migrations/up1.sql') COMMIT(*NONE) NAMING(*SQL)"

RollbackSQL.sh

system -i "RUNSQLSTM SRCSTMF('/sqlsource/migrations/down1.sql') COMMIT(*NONE) NAMING(*SQL)"

We now have the source on the remote server, and 2 local shells scripts that we can send to the remote system.  To actually send the remote command we’ll use this command below on our local machine to send the promotion shell script to the remote system and it’ll execute immediately the shell script that was sent over

ssh myuse@mysystem.example.com < PromoteSQL.sh

If we need to rollback the changes we’ll just use this command:

ssh myuse@mysystem.example.com < RollbackSQL.sh

I’ve kept this example pretty basic, but you can get more extensive by sending parameters to your shell scripts to dynamically tell it what to do.  For example you might want to pass it the environment, the user, the server and this is how you can do that

./promoteAndExecuteSQL.sh dev myuse mysystem.example.com

PromoteAndExecuteSQL.sh

#!/bin/bash
#ZZZPromoteCodeAndDB.sh dev 1205 1206
ENVIRONMENT=${1}
DEPLOYMENTUSER=${2}
DEPLOYMENTSERVER=${3}
echo ${ENVIRONMENT}
echo ${DEPLOYMENTUSER}
echo ${DEPLOYMENTSERVER}
scp -r "/sqlsource/migrations" ${DEPLOYMENTUSER}@${ENVIRONMENT}-${DEPLOYMENTSERVER}:/sqlsource/migrations
ssh ${DEPLOYMENTUSER}@${DEPLOYMENTSERVER} < PromoteSQL.sh

Run Linux commands on Windows via cmder

I know a lot about Linux commands and run them on Linux and IBM i, but never found a tool that could do it on windows until today.  If you have to run on windows and would like access to :

  • ls, grep, cat, tail, vim, ssh, scp, phpcli (via setting your windows path), etc..
  • history of commands
  • color coded screen
  • full screen

You can download cmder @ http://cmder.net/

I guess I won’t need Putty or Google’s ssh client app anymore and goodbye cmd.exe.

Shout out to @tweetjbh for recommending it

Compiling PHP extensions on IBM i / PASE/ AIX

This is post for Andy Youens and anyone looking to compile PHP Extensions on IBM i.  The first thing i would do is get GCC on the IBM i by following Tony’s guide here: http://yips.idevcloud.com/wiki/index.php/PASE/GCC .  GCC is the compiler system that can take a PHP extension  source code, which are written in C, and compile it into an .SO file that can be added via PHP.ini.  In Andy’s case he want’s xdebug and you can get the latest source code here: http://xdebug.org/files/xdebug-2.3.3.tgz . Or find other versions here: http://xdebug.org/download.php .

Compile Script

Why can’t we just use any old binary? 

The source code has to be compiled to the processor and the operating system you’re running it on.  In the case of IBM i , the PASE environment is basically AIX (IBM’s flavor of UNIX) and it runs on IBM’s Power processor.

Binary compiled for PHP 7.1

https://github.com/phpdave/DataDump/raw/master/xdebug.so

 Further reading if you need more information to get strarted:

http://files.zend.com/help/Zend-Server-5.6-Cluster-Manager/content/compiling_php_extensions.htm

 

Make your own command line functions in #SSH #Bash on #IBMi. Migrate off the 5250 already!

Just an example of how you can start migrating from the 5250 to SSH

Create aliases to various commands that you use.  Even create commands greater than 10 characters! lol

Other files to put in your /home/USER/ directory to unleash the power of the shell

Shout out to @tweetjbh https://twitter.com/tweetjbh for the ideas!

Run DB2 SQL from the command line in SSH in a Bash shell on IBMi

Aliases in unix are used to make it easier to run commands that are long or hard to remember.  Below is an example of how to create an alias to run DB2 SQL

WARNING! – This is just an experiment and you might want to add some security to this to make sure that sql injection and other things don’t occur.

Shout out to @tweetjbh https://twitter.com/tweetjbh for the ideas!

PHP CLI on IBMi PASE Memory Limit Problem (AIX OS via QP2TERM)

I ran into one of the hardest things to figure out the dreaded “Segmentation fault” and “Illegal instruction” while running php-cli in a QP2TERM session (a PASE/AIX/”IBMs unix” shell)The exact errors were:

php-cli[9]: 12345 Illegal instruction (coredump)  –when ran non-interactive

php-cli[9]: 12345 Segmentation fault(coredump) –when ran interactive

Note: php-cli is the shell script that calls the PHP interpreter on line 9 from the command line and 12345 is the AIX process id that had the issue.  Segmentation fault means you are addressing memory outside of your data segment which has a predefined size (256MB). coredump should be a data dump in the system log

In /usr/local/zendsvr/etc/php.ini I tried to increase the
memory_limit = 512M ; Maximum amount of memory a script may consume (512M). 

I tried to set the value in my script with
ini_set(‘memory_limit’, ‘512M’); 

I even tried to set it on the command line with the -d option
/usr/local/zendsvr/bin/php-cli -d memory_limit=512M myscript.php

I figured out the amount of memory my script was using by reducing the number of records it was processing and running the follow echo command to get the peak memory usage:

echo “Memory Peak Usage: “.(memory_get_peak_usage()/1024/1024).” MB”;

The actual memory bottle neck was happening further up the chain at the AIX process/job level.  The default memory limit of AIX process is 256MB with additional Data Segments of 256MB (Hex 0x10000000 2^8) with a max size of 8 additional data segments

Solution

Set the LDR_CNTRL environment variable in the parent process (PHP-CLI) to multiple data segments (in the example below 8 additional data segments of 256MB for 2.25GB of memory [this is the max…]) and then run your php script and then unset the memory limit so you don’t affect other processes.   Modify the shell script /usr/local/zendsvr/bin/php-cli and wrap the call to the php interpreter ($ZCE_PREFIX/bin/php “$@”) with the export and unsetting of LDR_CNTRL as shown below

export LDR_CNTRL=MAXDATA=0xB0000000@DSA
$ZCE_PREFIX/bin/php “$@”
unset LDR_CNTRL

putenv should probably not work because the parent process has to set LDR_CNTRL not the PHP script

putenv(“LDR_CNTRL=MAXDATA=0xB0000000@DSA”);

Use echo getenv(“LDR_CNTRL”); to see what its set to in your PHP script.

If you are using the FASTCGI w/ apache you can modify the config file (/www/zendsvr/conf/fastcgi.conf) and add to the end of  the line starting with Server type=”application/x-httpd-php” …

SetEnv=”LDR_CNTRL=MAXDATA=0xB0000000@DSA”

Caution

If you are hitting this limit you should probably look at the program you created because there may be something that is inefficiently using memory and that should be fixed instead of changing the memory limit.

What is this DSA

“The @DSA which can be appended to this value allows the boundary been private data and shared memory to be changed, allowing more segments to be used and the heap to start in segment 3. It also allows shared objects to be moved into segment 2 to give more contiguous space (See Figure 4).” – http://ibmsystemsmag.com/CMSTemplates/IBMSystemsMag/Print.aspx?path=/aix/administrator/systemsmanagement/Avoiding-Those–Segmentation-Fault–Failure-Messag

More info here: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.genprogc/lrg_prg_support.htm

These issues might also go away when we go from 32 bit to 64 bit.

Useful Shell/SSH/Bash commands for IBMi

What is my current path to binaries and libs:

echo $PATH

add a directory to my path so I don’t have to fully qualify the path to run. (we’re adding /QOpenSys/usr/bin)

export PATH=$PATH:/QOpenSys/usr/bin

Permanently set the default PATH used when you connect.  Create /home/MYPROFILE/.profile and add the export command above

In bsh just use

PATH=$PATH:/QOpenSys/usr/bin

set libpath for *.so files

LIBPATH=$LIBPATH:/QOpenSys/opt/freeware/lib