iTerm2 Tip: Repeating Commands Using a Coprocess

iTerm2 is a great terminal for MacOS; far better than Apple’s built-in Terminal app, and it’s my #1 recommendation for Mac-based network engineers. One of the many reasons I like it is that it has a feature that solves a really annoying problem.

Iterm Repeat Title

It’s tedious having to issue a command repeatedly so that you can see when and if the output changes. I’ve had to do this in the past, repeating commands like show ip arp so that I can spot when an entry times out and when it it refreshes. The repeated sequence of up arrow, Enter, up arrow, Enter, up arrow, Enter drives me mad.

Some vendors offer assistance; A10 Networks for example has a repeat command in the CLI specifically to help with show commands:

I have used this feature quite a lot, and I particularly like that there’s a built-in elapsed time marker between each command. But how to do the same in NXOS (for example) which does not have the repeat command?

iTerm2 Run Coprocess

Like many feature-rich applications, I suspect many users don’t get to try out all of iTerm2’s available features, because the base functionality is so good there’s often no need to dig much further into it than how to select a font and color. The Run Coprocess command is one that I simply don’t hear people talk about, so let’s take a look!

Iterm Run Coprocess Menu Option

The Run Coprocess┬ácommand allows a shell script or other executable to generate output which is sent as input to the terminal window. It’s easy to test by creating a shell script which uses echo command to write text to STDOUT:

Make the file executable (chmod +x <filename>), then in a window which is at a shell prompt, run the file using Run Coprocess:

Iterm Run Coprocess

It’s necessary to include a full path to the script, in this case my home directory, or ~/. The script runs and ls -al is echoed to my active terminal window as if I had typed it the command prompt. Voila, a file listing!

Repeating Commands

If I extend the logic above, it should be easy to create a script which will issue the ‘show ip arp’ command at a known interval and, to get a timestamp, how about I add a ‘show clock’ command first too?:

Since this script will loop forever, it is probably a good time to mention how to stop a Coprocess:

Iterm - Stop Coprocess

Does it work? You bet:

Bearing in mind that Cisco IOS/NXOS/XR all ignore commands beginning with a !, the timestamp generation could even be moved into the script:

And the result is as you’d hope:

This is helpful because now I can create a repeating command tool for Cisco devices which will issue a timestamp, issue any command I want, and repeat with whatever interval I choose as well:

As long as the command argument ($1) is issued as a single string (use quotes!) I can now trigger any command:

Iterm - Generic Coprocess Command

And the result:

Note that the timestamps are 2 seconds apart. The unix sleep command is pretty vague about how precise it will be (thus we assume that’s going to sleep for around two seconds in this case), but with a timestamp to confirm, at least I’ll know what the delay actually was.

Handy, right? I think so. What other uses do you have for a terminal Coprocess?

1 Comment on iTerm2 Tip: Repeating Commands Using a Coprocess

  1. if you are in MacOSX you have the luxury of nearly unlimited ‘co-processes’ — you are running a bash (or other) unix shell!

    You are already writing the looping bash scripts- why not use bash’s ability to run sub-shells (cmd-Z) to do everything you are doing without your hands leaving the keyboard (to click on menus)?

    Unless I missing something the bash shell itself is way more powerful than iTerm2.

    Many people don’t explore the other parts of iTerm2 because they are mostly redundant- Apple’s terminal is a very nice gateway into the power and control of the bash shell.

Leave a Reply

Your email address will not be published.



This site uses Akismet to reduce spam. Learn how your comment data is processed.