Auto-start with LinuxCNC

Use one method only: two would start two servers on the same port. Use Method 3 (systemd) if the server must come back by itself; a server started by loadusr that exits with status 1 is not started again until LinuxCNC is.

Method 1: HAL file

Add a loadusr line to your machine’s HAL file:

loadusr /home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051

Give the absolute path to the venv’s linuxcnc-grpc (see Installation). loadusr looks for a bare linuxcnc-grpc only on LinuxCNC’s PATH, which does not include the venv. A bare name works if you made the /usr/local/bin link. When LinuxCNC cannot find the program, its log shows execv(linuxcnc-grpc): No such file or directory and LinuxCNC runs without the server. The server stops when LinuxCNC shuts down.

Don’t use loadusr -W linuxcnc-grpc. -W waits for a HAL component named after the program, but the server’s component is grpc-hal-service, so LinuxCNC never finishes starting. To make LinuxCNC wait for the component, name it with -Wn:

loadusr -Wn grpc-hal-service /home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051

A ready component does not mean the server answers RPCs yet: it binds its port after that. Use the gRPC health Check to know it serves.

A server started from a HAL file can connect before LinuxCNC’s task reports its state, which counts as in cycle. With --error-channel auto it then logs a warning that it reads the error channel as always for now, and decides the mode once a status read, made every 2 seconds, shows the machine out of cycle. Until then the GUI can miss some error messages (see Error messages and the GUI).

Method 2: dedicated HAL file

Put the line in its own grpc-server.hal:

loadusr /home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051

Reference it in your INI file:

[HAL]
POSTGUI_HALFILE = grpc-server.hal

The GUI loads POSTGUI_HALFILE after it has started, so the server starts after the GUI. Only these GUIs read it: AXIS, touchy, gscreen, gmoccapy, QtVCP screens such as qtdragon, and mdro. LinuxCNC itself, halui, tklinuxcnc and headless or custom displays ignore it without a message, and the server never starts; use Method 1 with those.

If your INI file already has a POSTGUI_HALFILE line, add this one below it rather than replacing it. AXIS loads every POSTGUI_HALFILE line.

Method 3: systemd service

Create /etc/systemd/system/linuxcnc-grpc.service:

[Unit]
Description=LinuxCNC gRPC Server

[Service]
Type=simple
ExecStart=/home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051
Restart=always
RestartSec=2
User=your-username

[Install]
WantedBy=multi-user.target

Then enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now linuxcnc-grpc

ExecStart needs the absolute path to the venv’s linuxcnc-grpc or to the /usr/local/bin link. Set User= to the user that runs LinuxCNC.

The service can start at boot, before LinuxCNC: the server waits and connects when LinuxCNC comes up. Use Restart=always, not on-failure. When LinuxCNC shuts down it stops the server with a normal exit, and systemd has to start it again for the next LinuxCNC session.

Keep RestartSec=2, and never set it below 0.5 seconds. A server restarted sooner can come up while the old LinuxCNC is still shutting down and attach to it instead of the next one. It then exits with status 1 (see When the server finds its LinuxCNC gone), and those extra restarts count against systemd’s start limit. Without RestartSec, systemd waits only 100 ms.

The server’s command helper is a child process, and the unit needs nothing for it (see When the server stops).