Skip to content

feat(access): add --pidfile support for access command (fixes #723) - #1764

Closed
MauroDruwel wants to merge 1 commit into
cloudflare:masterfrom
MauroDruwel:feat/access-pidfile
Closed

MauroDruwel wants to merge 1 commit into
cloudflare:masterfrom
MauroDruwel:feat/access-pidfile

Conversation

@MauroDruwel

Copy link
Copy Markdown

Summary

Fixes #723

Adds --pidfile command-line flag (and TUNNEL_PIDFILE environment variable) support to cloudflared access and cloudflared access tcp (including aliases ssh, rdp, and smb).

Problem & Motivation

When running cloudflared access tcp as a background process or forwarder in automation scripts, Taskfiles, or CI pipelines (e.g. for forwarding SOCKS5, Kubernetes API, or Vault access), users currently have to resort to fragile pgrep / grep pipelines to find and terminate the process during cleanup traps.

cloudflared tunnel already provides a --pidfile flag. Bringing parity to cloudflared access enables clean process management and reliable shutdown (e.g., pkill -F .cloudflared.pid or kill -15 $(cat .cloudflared.pid)).

Changes

  1. Shared PID Helper: Extracted writePidFile logic into cmd/cloudflared/cliutil/pidfile.go (cliutil.WritePidFile) supporting ~ homedir expansion, path cleaning, and structured error logging. Refactored cmd/cloudflared/tunnel/cmd.go to use this shared utility to eliminate duplicate code.
  2. Flag Registration:
    • Added cfdflags.Pidfile constant in cmd/cloudflared/flags/flags.go.
    • Registered --pidfile with TUNNEL_PIDFILE env var on cloudflared access and the tcp subcommand in cmd/cloudflared/access/cmd.go.
  3. Execution Hook: In cmd/cloudflared/access/carrier.go, cliutil.WritePidFile is called for both background forwarding listener mode (--url) and client stream proxy mode.

Testing Plan

  • Unit Tests:
    • Added cmd/cloudflared/cliutil/pidfile_test.go verifying normal file creation with os.Getpid(), homedir ~ expansion, and graceful error handling on invalid paths.
    • Added cmd/cloudflared/access/pidfile_test.go verifying --pidfile and TUNNEL_PIDFILE registration across access and access tcp.
    • Ran go test -count=1 on cliutil, access, tunnel, and carrier packages — all passed.
  • End-to-End Verification:
    • Verified live process execution: cloudflared access tcp --hostname ... --url 127.0.0.1:23456 --pidfile /tmp/test.pid created the file with the exact running PID.
    • Verified parent flag invocation: cloudflared access --pidfile /tmp/test.pid tcp ....
  • Linters:
    • gofmt -s -l: clean (0 issues).
    • go vet ./cmd/cloudflared/...: clean (0 issues).

@MauroDruwel

Copy link
Copy Markdown
Author

Closing as duplicate of #1599.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

💡--pidfile support for "cloudflared access"

1 participant