Skip to content

ps command fails when no parent process (docker environment) #661

@benningm

Description

@benningm

I'm running overcommit within a docker container and the pre-commit always outputs:

error: process ID out of range

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

This is because the pre-commit checks if the parent process has the '--amend' flag on his command line. But when run inside docker the command will have no parent process at all.

In this case:

irb(main):001:0> Process.ppid
=> 0

Then ps system command will fail at:

`ps -ocommand= -p #{Process.ppid}`.chomp

The Overcommit::Utils.parent_command method should be extended to handle the case when there is no parent (ppid=0) or when the ps command fails with a non-zero return code.

Possible solution may be:

      def parent_command
        return if Process.ppid.zero?

        output = if OS.windows?
          `wmic process where ProcessId=#{Process.ppid} get CommandLine /FORMAT:VALUE`.
            strip.
            slice(/(?<=CommandLine=).+/)
        elsif OS.cygwin?
          # Cygwin's `ps` command behaves differently than the traditional
          # Linux version, but a comparable `procps` is provided to compensate.
          `procps -ocommand= -p #{Process.ppid}`.chomp
        else
          `ps -ocommand= -p #{Process.ppid}`.chomp
        end
        return unless $?.success?

        output
      end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions