Shell Scripting

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.

Environment: PATH and setmqenv

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.

shell
1
2
3
4
5
#!/bin/bash # Adjust path to your MQ installation . /opt/mqm/bin/setmqenv -s which runmqsc runmqsc -v

Deploy Pattern with runmqsc

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.

shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
deploy_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

Monitoring Channel RETRY

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.

Common shell commands with MQ
CommandUse
dspmqList queue managers and status
runmqscRun MQSC script
setmqautGrant authority from shell deploy
amqsputc / amqsgetcSmoke test after deploy
strmqm / endmqmStart/stop QM (controlled ops)

Explainer: Sticky Notes for the Computer

Shell scripts are sticky notes that tell Linux exactly which MQ commands to run in order without you retyping them.

Locking and Concurrent Runs

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.

Cron Best Practices

  • Full path or source setmqenv at script top.
  • Redirect stdout/stderr to rotated logs.
  • MAILTO or webhook on non-zero exit.
  • Run as dedicated mqmon user, not root.
  • Document schedule in operations runbook.

dspmq and Process Checks

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.

Security

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.

Limitations

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.

Explain Like I'm Five: Shell Scripting

Shell scripting is a list of MQ chores the Linux computer does when you press go or when the clock hits midnight.

Practice Exercises

Exercise 1

Write bash that runs one MQSC file against QM argument and greps AMQ errors.

Exercise 2

Add flock so two deploys cannot run together.

Exercise 3

Write cron-safe wrapper that sources setmqenv.

Frequently Asked Questions

Frequently Asked Questions

Test Your Knowledge

Test Your Knowledge

1. runmqsc in bash often uses:

  • Input redirection from .mqsc file
  • Only GUI
  • JCL
  • FTP

2. set -e means:

  • Exit on first command failure
  • Always continue
  • Disable TLS
  • Start QM

3. Cron MQ scripts need:

  • setmqenv in profile or script
  • Nothing
  • CICS
  • JES

4. Loop over QMs should:

  • Stop on error with set -e
  • Ignore all errors
  • DELETE QMGR
  • Skip logging
Published
Read time21 min
AuthorMainframeMaster
Verified: IBM MQ 9.3 documentation