LinuxCNC M100-M199 User Defined Commands

In LinuxCNC, the M-codes M100 through M199 run an external program instead of a built-in function. The optional P and Q words are passed to that program as its first and second arguments, and execution of the G-code file pauses until the external program exits. If the external program exits with a code other than 0, G-code program execution is stopped.

Source checked7/7 values · 2026-10-26

Specification

1 source
Languagegcode
CodeM100-M199
GroupUser Defined M-Codes
SyntaxM1-- <P- Q->
DialectLinuxCNC

Parameters

NameDescription
PA number passed to the file as the first parameter.
QA number passed to the file as the second parameter.

Study notes

  • The external program file is named Mnnn (capital M, no extension) with nnn from 100 to 199, and must be located in the directory pointed to by the [DISPLAY] PROGRAM_PREFIX parameter of the INI file.
  • Any valid executable file can be used; it must be in the search path specified in the INI file configuration.
  • After creating a new M1nn file the GUI must be restarted so it is aware of the new file, otherwise an 'Unknown m code' error occurs.
  • The 'Unknown M-code' error means the user defined command does not exist, the file is not executable, the file name has an extension, the file name does not follow the Mnnn format with nnn = 100 through 199, or the file name used a lower case M.
  • Example: files M101 and M102 written as bash scripts use 'halcmd setp parport.0.pin-14-out True/False' to open and close a collet closer, and must be set as executable files before running LinuxCNC.
  • Example: 'M100 P123.456 Q321.654' passes the two values to the M100 script as arguments $1 and $2.
  • Example: an M110 script can display a graphic with a viewer program and halt until the window is closed, or continue processing by suffixing the command with an ampersand.

Safety

  • G-code execution pauses until the external program exits; if the external program exits with an exit code other than 0, G-code program execution is stopped.
  • Do not use a word processor to create or edit M1nn files; a word processor leaves unseen codes that cause problems and may prevent a bash or python file from working, so a text editor such as Geany or Notepad++ should be used.
↑↓ navigate↵ openesc close