Skip to content

Ps command may return exit 1 when no processes are shown #15

Description

@sirn

On some systems such as FreeBSD, running ps without an assigned ttys, without login shell and without displaying header may cause ps command to have no output and exit 1 (e.g. via ssh user@host ps -o pid= or as part of Ansible script). When this happens, shellingham may fail with subprocess.CalledProcessError:

In particular, I got this error when I tried to run pipenv over Ansible on FreeBSD:

Traceback (most recent call last):
  File "/usr/local/bin/pipenv", line 7, in <module>
    from pipenv import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/__init__.py", line 23, in <module>
    from .cli import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/cli/__init__.py", line 3, in <module>
    from .command import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/cli/command.py", line 7, in <module>
    import crayons
  File "/usr/local/lib/python3.6/site-packages/pipenv/patched/crayons.py", line 49, in <module>
    is_powershell = "powershell" in shellingham.detect_shell()[0]
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/__init__.py", line 22, in detect_shell
    shell = get_shell(pid, max_depth=max_depth)
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/__init__.py", line 54, in get_shell
    mapping = _get_process_mapping()
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/__init__.py", line 15, in _get_process_mapping
    mapping = impl.get_process_mapping()
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/ps.py", line 18, in get_process_mapping
    'ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=',
  File "/usr/local/lib/python3.6/subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "/usr/local/lib/python3.6/subprocess.py", line 418, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=']' returned non-zero exit status 1.
*** Error code 1

Stop.

$ ssh user@freebsdhost "ps -o pid= || echo $?"
1

The above command is alternating 1 and 0 for some reason, I think because sometimes ps see a ps process, but I have been able to reproduce this quite reliably with Vagrant (run vagrant up then vagrant provision):

Vagrant.configure("2") do |config|
  config.vm.box = "generic/freebsd11"

  config.vm.provision :shell, privileged: false, inline: <<-EOF
    ps -ww -o pid=
    echo $?
  EOF
end

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions