簡體   English   中英

如何知道進程是用戶還是root在bash,Linux中

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

我需要知道一個過程是否來自用戶。 我正在使用此代碼:

#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

但是我對字符串中的“選項卡”不太滿意,我認為這可能會導致某些意外行為。

還有其他方法嗎?

您可以在不使用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