Search code examples
linuxubuntuserviceprocesspid

Get process (service) name or description from PID in linux terminal


I have multiple java applications that run on my ubuntu server, all have their *.service file and start at boot. When I try to get the process names with top or with ls -l /proc/191358/exe the process name is always /bin/java or something extra long but I want the process description as in the .service file "My application" Is there a way to achieve this?


Solution

  • I don't have any java services running, so can't really experiment with sample data for you, but the following will give you all running services with their 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
    

    Feel free to tack on a trailing grep to get only the ones you're interested in:

    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|...'
    

    Edit (adding description):

    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