代码之家  ›  专栏  ›  技术社区  ›  Alessandro Valentino

从linux终端的PID中获取进程(服务)名称或描述

  •  0
  • Alessandro Valentino  · 技术社区  · 1 年前

    我有多个java应用程序在我的ubuntu服务器上运行,它们都有*.service文件,并在启动时启动。 当我尝试使用 top 或与 ls -l /proc/191358/exe 进程名称总是 /bin/java 或者一些超长的东西,但我想要.service文件“我的应用程序”中的流程描述 有办法做到这一点吗?

    1 回复  |  直到 1 年前
        1
  •  1
  •   tink    1 年前

    我没有 java 服务正在运行,所以不能真正为您尝试样本数据,但下面将为您提供所有正在运行的服务及其PID。

    systemctl -t service --state=active --no-pager --no-legend | awk '{print $1}' | while read -r service; do echo -e "${service}\t $(systemctl show --property MainPID --value ${service})" ; done
    

    随意钉在拖尾上 grep 只获取您感兴趣的:

    systemctl -t service --state=active --no-pager --no-legend | awk '{print $1}' | while read -r service; do echo -e "${service}\t $(systemctl show --property MainPID --value ${service})" ; done | grep -E 'filter1|filter2|...'
    

    编辑(添加说明):

    systemctl -t service --state=active --no-pager --no-legend | awk '{print $1}' | while read -r service; do echo -e "$(systemctl show --property MainPID,Description  --value ${service}|tr '\n' '\t')\t${service}" ; done