Four codes control when a program pauses and how it ends:
M0— stop, unconditionally. Machine holds; cycle-start resumes.M1— optional stop: identical to M0, but only honored when the operator's optional stop switch is on. Otherwise skipped.M2— program end.M30— program end + rewind to the first line, armed for the next run. What modern posts emit.
...
M9 ; coolant off
M5 ; spindle stop
G53 G0 Z0 ; retract
G53 G0 X-10 Y200 ; present the part toward the operator
M30 ; end, rewind, ready for the next part
#Stops: M0 and M1
M0 is a scheduled interruption: flip the part over, blow out a pocket,
insert a bushing. The program is the checklist — the stop happens at
exactly the right line every time.
M1 is the prove-out tool. Posts put one before each tool change; with
the optional-stop switch on, a new job runs one tool at a time with an
inspection hold between. Once the job is trusted, switch off and M1 lines
vanish.
The critical caveat for both: what the machine does to spindle and
coolant during a stop is control-dependent. Some stop everything, some
keep the spindle turning behind the closed door. If hands go anywhere
near the work during the stop, the program must command M5/M9 first —
never trust the stop code to do it.
| Operation | LinuxCNC | GRBL | Centroid | FANUC | Mach3 | Mach4 | Notes |
|---|---|---|---|---|---|---|---|
| Compulsory program stop | M0 |
M0 |
M0 |
M0 |
M0 |
M0 |
Cycle-start to resume. |
| Optional stop | M1 |
M1 |
M1 |
M1 |
M1 |
M1 |
Skipped unless the OSTOP switch is on. |
| Program end (no rewind) | M2 |
M2 |
M2 |
M2 |
M2 |
M2 |
|
| Program end + rewind | M30 |
M30 |
M30 |
M30 |
M30 |
M30 |
What most CAM emits at file end. |
| Re-run program from start | — | — | — | — | M47 |
M47 |
Mach4: partial |
— means the control has no equivalent code. Full cross-control tables: the complete G-code & M-code reference.
#Ends: M2 and M30
The practical difference on modern controls is small: M30 rewinds to the
top and generally resets more modal state (offsets stay; things like
G91 or an active canned cycle don't survive on most controls — the exact
reset list varies). Because "ready for the next part" is what production
wants, M30 is the standard ending and M2 mostly appears in legacy
files. Mach adds M47 — jump back to the first line without stopping —
for continuous demo/warmup loops; treat it as the special case it is.
One habit worth stealing from good posts: end programs with the machine in a boring state — coolant off, spindle stopped, Z high, table presented forward — so the ending is identical whether the file ran clean or an operator hit reset halfway.
#Common mistakes
- Trusting M0 to make the machine safe. It stops the program, not
necessarily the spindle. Explicit
M5 M9before any stop a human interacts with. - No end code at all. A file that just runs out of lines behaves differently per control — some stop cleanly, some alarm. End with M30.
- Restarting after M2 on an old control and inheriting modal leftovers — a G91 or active cycle from the previous run. M30's fuller reset (and a proper preamble at the top) covers both directions.
- Using M0 for tool changes on a changer-equipped machine. That's what M6 is for; M0 leaves the change untracked and the offsets unmanaged.