简体   繁体   English

如何知道进程是用户还是root在bash,Linux中

[英]How to know if a process is user or root in bash, linux

I need to know if a process is from the user or not. 我需要知道一个过程是否来自用户。 I'm using this code: 我正在使用此代码:

#c is the PID
Aid=$(cat /proc/$c/status | grep -e ^Uid) 
Uid="Uid:   0   0   0   0"
if [ "$Aid" != "$Uid" ]; then
    echo "is from user"
fi

But I'm not very comfortable with the "tabs" in the string, i think that maybe can cause some unexpected behavior. 但是我对字符串中的“选项卡”不太满意,我认为这可能会导致某些意外行为。

Is there any other way to do it? 还有其他方法吗?

You can get the UID of a certain pid without parsing using ps -o uid= -p $pidhere : 您可以在不使用ps -o uid= -p $pidhere进行解析的情况下获取某个pid的UID:

mypid=1
if uid=$(ps -o uid= -p "$mypid")
then
  if [[ $uid -eq 0 ]]
  then
    echo "The process runs as root"
  else
    echo "The process runs as something else"
  fi
else
  echo "The process doesn't exist"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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