How to exit minicom via scripting
Asked Answered
L

3

5

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?

Legendre answered 7/3, 2019 at 15:45 Comment(0)
P
5

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

Picaresque answered 18/7, 2019 at 14:48 Comment(1)
Cannot get expected output in capture.txt with the solution, minicom will start a runscript process while the pipe here just terminates minicom directly before runscript runs test_minicom.macro. An improvement for this: { sleep 1; while pgrep runscript > /dev/null; do sleep 1; done; cat escape.txt; } | minicom -D /dev/ttyUSB0 -S test_minicom.macro -C capture.txt.Spital
S
3

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!

Schenck answered 29/6, 2021 at 21:12 Comment(0)
H
3

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
Histrionic answered 8/11, 2022 at 16:47 Comment(1)
This should be the top answer. You can further improve the code with a temp output file created with mktemp to avoid conflicts.Spavined

© 2022 - 2025 — McMap. All rights reserved.