Bash and POSIX shell are the default glue for IBM MQ on Linux, AIX, and many cloud images: cron jobs that check channels, deploy pipelines that pipe MQSC into runmqsc, and on-call scripts that restart a listener after a network blip. Shell is not elegant, but it is everywhere—operators SSH to a broker host and run the same script that Jenkins uses. Beginners write five-line scripts; platforms standardize libraries of functions mq_deploy(), mq_channel_retry_check(), and mq_export_config(). This tutorial covers environment setup with setmqenv, running runmqsc safely, parsing output with grep and awk, looping queue managers, integrating dspmq for process checks, cron scheduling, logging, locking to prevent concurrent deploys, and pitfalls like cron without PATH or scripts that ignore runmqsc errors.
Interactive shells source setmqenv -s from the MQ installation to set PATH, LD_LIBRARY_PATH, and MQ_DATA_PATH. Cron and CI omit login profiles—scripts must source setmqenv explicitly or use full paths to runmqsc in /opt/mqm/bin. Verify with which runmqsc inside the script and log the version. Wrong environment produces “command not found” in cron while SSH works—classic confusion.
12345#!/bin/bash # Adjust path to your MQ installation . /opt/mqm/bin/setmqenv -s which runmqsc runmqsc -v
Pass queue manager as argument $1. Validate non-empty. Redirect output to log. Grep for errors. Use heredoc for inline MQSC when tiny; files when large. Exit non-zero on failure so CI marks build red.
1234567891011121314deploy_qm() { local qm="$1" local script="$2" local log log="$(mktemp)" runmqsc "$qm" < "$script" | tee "$log" if grep -qE 'AMQ[0-9]{4}E' "$log"; then rm -f "$log" return 1 fi rm -f "$log" return 0 } deploy_qm QM1 /opt/mq/scripts/01-queues.mqsc
echo "DISPLAY CHSTATUS(*) STATUS" | runmqsc QM1 | grep RETRY triggers alert if count > 0. Refine with awk for channel names. Pair with ping or START CHANNEL recovery only if runbook allows automated restart—many sites alert humans first. Log timestamp and hostname for correlation.
| Command | Use |
|---|---|
| dspmq | List queue managers and status |
| runmqsc | Run MQSC script |
| setmqaut | Grant authority from shell deploy |
| amqsputc / amqsgetc | Smoke test after deploy |
| strmqm / endmqm | Start/stop QM (controlled ops) |
Shell scripts are sticky notes that tell Linux exactly which MQ commands to run in order without you retyping them.
Two pipelines deploying same QM simultaneously corrupt state. Use flock on lock file /var/lock/mq-QM1.deploy. Second script exits with message “deploy in progress.” Release lock in trap on EXIT. Long deploys set timeout alerts.
Before runmqsc, dspmq -m QM1 | grep -q "Running" or exit early with clear message. Saves cryptic connection errors. After endmqm maintenance, script waits loop with sleep until Running before deploy step continues.
chmod 750 scripts; chown mqdeploy:mqm. Do not embed passwords—use sudo to mqdeploy role. Restrict cron to mqdeploy. Audit sudoers. Scripts on shared admin server are attack targets—integrity monitoring recommended.
Complex PCF parsing in bash becomes unmaintainable—move to Python or Java. Heavy binary message handling does not belong in shell. Windows servers use PowerShell instead of bash for native MQ admin.
Shell scripting is a list of MQ chores the Linux computer does when you press go or when the clock hits midnight.
Write bash that runs one MQSC file against QM argument and greps AMQ errors.
Add flock so two deploys cannot run together.
Write cron-safe wrapper that sources setmqenv.
1. runmqsc in bash often uses:
2. set -e means:
3. Cron MQ scripts need:
4. Loop over QMs should: