简体   繁体   中英

Bash how to get full script name from PID

I have this php function that checks the script's name from the given PID, and compares it to itself.

function isRunning($pid) {
    $filename = exec('ps -p '.$pid.' -o "%c"');
    $self = basename($_SERVER['SCRIPT_NAME']);
    return ($filename == $self) ? TRUE : FALSE;
}

From what I know, I usually use this command to get the script name from the PID:

ps -o PID -o "%c"

It returns me the filename, but only the first 15 characters. Since my script's name is

daily_system_check.php

the function always returns FALSE, because it's comparing itself with

daily_system_ch

Is there another bash command for Centos 6 that will return me script's full name?

You didn't specify what is your OS, but in Ubuntu Linux I can see full name of the script with adding --context to the ps call:

# ps -p 17165 --context
  PID CONTEXT                  COMMAND
17165 unconfined               /bin/bash ./testing_long_script_name.sh
# 

阅读proc cmdline文件:

cat /proc/$pid/cmdline | awk 'BEGIN {FS="\0"} {print $2}'

There seems to be no flag or collumn in "ps" command to show the whole filename without the filepath or it being cutoff. PHP's basename() gets the job done.

function isRunning($pid) {
    $filename = basename(exec('ps -o cmd= '.$pid));
    $self = basename($_SERVER['SCRIPT_NAME']);
    return ($filename == $self) ? TRUE : FALSE;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM