ps 仅打印任何进程命令行的最多 4096 个字符

ps 仅打印任何进程命令行的最多 4096 个字符

当我 grep 查找 java 进程时,我得到以下输出,但它限制为 4096 个字符,这导致实际进程名称(即 kafka.Kafka)未显示在grep输出中。

这是 grep 的限制吗?有没有办法打印超过 4096 个字符的限制?

ps -ef | grep java

java -Xmx6G -Xms6G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+DisableExplicitGC -Djava.awt.headless=true -Xloggc:/x/kafka/data01/kafka-app -logs/kafkaServer-gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom. sun.management.jmxremote.ssl=false -Dkafka.logs.dir=/x/kafka/data01/kafka-app-logs -Dlog4j.configuration=文件:./../config/log4j.properties -cp :/x /home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/aopalliance-repackaged-2.4.0-b34.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10 -0.10.1.1/bin/../libs/argparse4j-0.5.0.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/connect-api- 0.10.1.1.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/connect-file-0.10.1.1.jar:/x/home/bmcuser/kafka -paypal/kafka_2.10-0.10.1.1/bin/../libs/connect-json-0.10.1.1.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/。 ./libs/connect-runtime-0.10.1.1.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/guava-18.0.jar:/x/home /bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/hk2-api-2.4.0-b34.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10 .1.1/bin/../libs/hk2-locator-2.4.0-b34.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/hk2- utils-2.4.0-b34.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jackson-annotations-2.6.0.jar:/x/home /bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jackson-core-2.6.3.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1 /bin/../libs/jackson-databind-2.6.3.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jackson-jaxrs-base- 2.6.3.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jackson-jaxrs-json-provider-2.6.3.jar:/x/home /bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jackson-module-jaxb-annotations-2.6.3.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10 -0.10.1.1/bin/../libs/javassist-3.18.2-GA.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/javax.注释-api-1.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/javax.inject-1.jar:/x/home/bmcuser/kafka -paypal/kafka_2.10-0.10.1.1/bin/../libs/javax.inject-2.4.0-b34.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin /../libs/javax.servlet-api-3.1.0.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/javax.ws.rs- api-2.0.1.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-client-2.22.2.jar:/x/home/bmcuser /kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-common-2.22.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin /../libs/jersey-container-servlet-2.22.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-container-servlet- core-2.22.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-guava-2.22.2.jar:/x/home/bmcuser /kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-media-jaxb-2.22.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jersey-server-2.22.2.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10 -0.10.1.1/bin/../libs/jetty-continuation-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/ jetty-http-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jetty-io-9.2.15.v20160210.jar: /x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jetty-security-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal/kafka_2 .10-0.10.1.1/bin/../libs/jetty-server-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../ libs/jetty-servlet-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jetty-servlets-9.2.15.v20160210。 jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/jetty-util-9.2.15.v20160210.jar:/x/home/bmcuser/kafka-paypal /kafka_2.10-0.10.1.1/bin/../libs/jopt-simple-4.9.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/ kafka_2.10-0.10.1.1.jar:/x/home/bmcuser/kafka-paypal/kafka_2.10-0.10.1.1/bin/../libs/kafka_2.10-0.10.1.1-so

答案1

这不是限制grep,而是/proc/PID/cmdline(从技术上讲,是设计决策,而不是限制)。/proc/PID/cmdline包含进程的完整命令行,主命令和参数以 ASCII NUL 分隔,文件也以 NUL 结尾。因此,grep如果存在匹配,将打印整个文件内容。 (ps -ef获取该文件的内容为CMD)。

最大长度在 (Linux) 内核中硬编码为PAGE_SIZE

static int proc_pid_cmdline(struct task_struct *task, char * buffer)
{
        int res = 0;
        unsigned int len;
        struct mm_struct *mm = get_task_mm(task);
        if (!mm)
                goto out;
        if (!mm->arg_end)
                goto out_mm;    /* Shh! No looking before we're done */

        len = mm->arg_end - mm->arg_start;

        if (len > PAGE_SIZE)
                len = PAGE_SIZE;

因此这样的系统需要 4096 字节:

% getconf PAGE_SIZE
4096

此外,如果您有多字节字符,则字符数将少于 4096,正如您可以想象的那样。

相关内容