{"record":{"id":"022c274db89c78e3","repo":"fluent/fluentd","slug":"cannot-access-pid-file-pid-path","errorCode":null,"errorMessage":"Cannot access pid file: #{pid_path}","messagePattern":"Cannot access pid file: #(.+?)","errorType":"exception","errorClass":"Fluent::ConfigError","httpStatus":null,"severity":"error","filePath":"lib/fluent/daemonizer.rb","lineNumber":56,"sourceCode":"      rescue NotImplementedError\n        daemonize_with_spawn(pid_fullpath, args)\n      end\n    end\n\n    private\n\n    def daemonize_with_spawn(pid_fullpath, args)\n      pid = Process.spawn(*['fluentd'].concat(args))\n\n      File.write(pid_fullpath, pid.to_s)\n\n      pid\n    end\n\n    def check_pidfile(pid_path)\n      if File.exist?(pid_path)\n        if !File.readable?(pid_path) || !File.writable?(pid_path)\n          raise Fluent::ConfigError, \"Cannot access pid file: #{pid_path}\"\n        end\n\n        pid =\n          begin\n            Integer(File.read(pid_path), 10)\n          rescue TypeError, ArgumentError\n            return # ignore\n          end\n\n        begin\n          Process.kill(0, pid)\n          raise Fluent::ConfigError, \"pid(#{pid}) is running\"\n        rescue Errno::EPERM\n          raise Fluent::ConfigError, \"pid(#{pid}) is running\"\n        rescue Errno::ESRCH\n        end\n      else\n        unless File.writable?(File.dirname(pid_path))","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/fluent/fluentd/blob/dd45c6e18dc7be33b5e5a0f0767bf46307ff5626/lib/fluent/daemonizer.rb#L38-L74","documentation":"When fluentd daemonizes (--daemon PIDFILE), check_pidfile runs before spawning: an existing pidfile must be both readable and writable by the current user, otherwise startup aborts with this ConfigError. Fluentd needs read access to check whether the recorded pid is alive and write access to replace it with the new daemon pid. The check deliberately fails closed so two daemons never race on one pidfile.","triggerScenarios":"Starting fluentd --daemon /var/run/fluentd.pid as the fluent user when a previous root-run fluentd left a 0600 root-owned pidfile; pidfile created by a different user; ACLs denying write on the file.","commonSituations":"Switching the daemon user between runs (root to non-root); leftover pidfile from a crashed/killed root process; container images baked with a root-owned pidfile.","solutions":["Verify no fluentd is running for that pidfile (ps -p $(cat /path/to/fluentd.pid)), then remove the stale pidfile","chown the pidfile to the daemon user or chmod 664 so the current user can read and write it","Run the daemon under the same user that owns the pidfile","Use a per-instance pidfile path under the daemon user's own runtime directory (e.g. systemd RuntimeDirectory=fluentd)"],"exampleFix":"# before\n$ sudo fluentd --daemon /var/run/fluentd.pid ...\n$ fluentd --daemon /var/run/fluentd.pid ...   # fails: not writable\n\n# after\n$ ps -p $(cat /var/run/fluentd.pid) || sudo rm /var/run/fluentd.pid\n$ sudo chown fluent:fluent /var/run/fluentd.pid 2>/dev/null || true\n$ fluentd --daemon /var/run/fluentd.pid ...","handlingStrategy":"validation","validationCode":"pidfile = '/var/run/fluentd.pid'\nif File.exist?(pidfile)\n  abort \"pidfile not readable/writable\" unless File.readable?(pidfile) && File.writable?(pidfile)\nend","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep daemon user consistent across restarts so pidfile ownership never changes","Have init scripts/systemd own the runtime dir and pidfile lifecycle","Clean stale pidfiles as part of the start script after a liveness check"],"tags":["fluentd","daemon","pidfile","permissions","filesystem"],"backgroundTag":"pid-file-permission-denied","analyzedSha":"dd45c6e18dc7be33b5e5a0f0767bf46307ff5626","analyzedAt":"2026-08-21T16:22:07.332Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}