Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashsmith/55098099d2a5b5dfed9935dd4488abd6 to your computer and use it in GitHub Desktop.
Save ashsmith/55098099d2a5b5dfed9935dd4488abd6 to your computer and use it in GitHub Desktop.
Enable "ps [-p|--pid] PID" for /bin/ps from busybox (like Alpine Linux)
#!/bin/sh
# enable "ps [-p] PID" for /bin/ps from busybox (like Alpine)
# copy this script as /usr/local/bin/ps or /usr/bin/ps, and chmod 755 it.
if [ $# == 1 ]; then
echo $1 | grep -q -E '^[0-9]+$' # number only argument
if [ $? == 0 ]; then
OPT_P=1
ARG_P=$1
fi
else
for OPT in "$@"
do
case "$OPT" in
-p|--pid)
OPT_P=1
ARG_P=$2
shift 2
;;
esac
done
fi
if [ x$OPT_P == x1 ]; then
/bin/ps -o pid | grep -q $ARG_P # grep pid only
RET=$?
/bin/ps | egrep "^(PID| *$ARG_P )" # show output like normal ps
exit $RET
else
/bin/ps $*
fi
@ashsmith
Copy link
Author

ashsmith commented Mar 27, 2019

This script is for when you're using alpine and require the --pid or -p option on ps.

In my case, this error was caused whilst running Magento integration tests.

Fatal error: Uncaught Exception: ps: unrecognized option: pid
BusyBox v1.29.3 (2019-01-24 07:45:07 UTC) multi-call binary.

Usage: ps [-o COL1,COL2=HEADER]

Show list of processes

	-o COL1,COL2=HEADER	Select columns for display in /var/www/html/vendor/magento/framework/Shell.php:61
Stack trace:
#0 /var/www/html/dev/tests/integration/framework/Magento/TestFramework/Helper/Memory.php(74): Magento\Framework\Shell->execute('ps --pid '28' -...', Array)
#1 /var/www/html/dev/tests/integration/framework/Magento/TestFramework/Helper/Memory.php(55): Magento\TestFramework\Helper\Memory->_getUnixProcessMemoryUsage(28)
#2 /var/www/html/dev/tests/integration/framework/Magento/TestFramework/MemoryLimit.php(127): Magento\TestFramework\Helper\Memory->getRealMemoryUsage()
#3 /var/www/html/dev/tests/integration/framework/Magento/TestFramework/MemoryLimit.php(59): Magento\TestFramework\MemoryLimit->_getUsage()
#4 /var/www/html/dev/tests/integration/framework/Magento/TestFramework/Bootstrap/Memory.php(50): Magento\TestFramework\MemoryLimit-> in /var/www/html/vendor/magento/framework/Shell.php on line 62

To use this script simply add the following in your docker file:

RUN curl -O https://gist.github.com/ashsmith/55098099d2a5b5dfed9935dd4488abd6/raw/9113834d220fca40ed69e53c198a8891a4357d8e/ps_opt_p_enabled_for_alpine.sh \ 
  && mv /usr/local/bin/ps \
  && chmod +x /usr/local/bin/ps

@GalSvirin
Copy link

In alpine linux you can run apk --no-cache add procps and this should solve your issue without workaround script

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