How to exit minicom via scripting

11.8k Views Asked by At

I have a minicom script which sends some commands via serial and expects something back(which works) but im having trouble exiting the minicom screen.

Below is the minicom script:

success1:    
   print \nSuccessfully received running!
   send "exit" 
   exit 0

success2:
   print \nSuccessfully received degrading!
   ! killall -9 minicom
   exit

I was using ! killall -9 minicom which is recommended on their documentation but unfortunately, when running the script on Jenkins, it fails due to exit code 137 (Another process sent a signal 9). However this does exit minicom, just not successfully.

On the other hand, the 'send "exit"' just logs out of the device, and doesnt exit minicom.

How can i exit minicom and receive a 0 exit code?

3

There are 3 best solutions below

1
Diego Scaravaggi On

you need to feed <stdin> with three characters: <ctrl-A>x<enter>

  • prepare the file escape.txt using vi in order to write ^Ax^M
  • launch minicom script

/bin/rm -f capture.txt; ( minicom -D /dev/ttyUSB0 -S test_minicom.macro -C capture.txt < escape.txt ) ; cat capture.txt

0
Devyn Curley On

To build on what Diego shared, if you just need to exit minicom without error and don't care about capturing the exit code, build escape.txt as Diego described then you only need to run:

( minicom -D /dev/ttyUSB0 -S test_minicom.macro -C capture.txt < escape.txt )

This proves to be very helpful for automatic provisioning like with Ansible!

1
jjcf89 On

As an alternative to creating the escape.txt file, you can use echo to send the exit sequence.

Building on the above answers:

$ /bin/rm -f capture.txt; ( echo -ne "\x01x\r" ) | minicom -D /dev/ttyUSB0 -S test_minicom.macro -C capture.txt; cat capture.txt

To break down the echo command a little,

  • -n removes the default line-feed \n
  • -e tells echo to interpret escape sequences
  • \x01 is the escape sequence for ^A (start-of-heading)
  • x tells minicom to exit
  • \r sends a ^M carriage-return

Hex output from echo:

$ echo -ne "\x01x\r" | od -A x -t x1a -v
000000  01  78  0d
       soh   x  cr
000003

Note: If you just want to send some text and don't need the full minicom scripting you can add an extra echo. Sleep may not be needed depending on command run and if you care about output in capture.txt...

$ /bin/rm -f capture.txt; ( echo "poweroff"; sleep 1; echo -ne "\x01x\r" ) | minicom -D /dev/ttyUSB0 -C capture.txt; cat capture.txt