gpt4 book ai didi

regex - 使用 bash 获取 http 进程用户

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:33 25 4
gpt4 key购买 nike

为 bash 脚本使用获取网络进程用户 (apache|nginx|www-data) 的最佳方式是什么?

在我的例子中,设置文件夹权限并更改为 poper 所有者。

目前我正在使用:

ps aux | grep -E "(www-data|apache|nginx).*(httpd|apache2|nginx)" \
| grep -o "^[a-z\-]*" | head -n1

在 bash 脚本中获取 http 进程的所有者。

任何关于更聪明的解决方案或更好的正则表达式的提示都会很棒。

最佳答案

您的解决方案实际上取决于您的操作系统。一种选择可能是检查您的密码文件中是否存在可能的候选人:

user=$(awk -F: '/www|http/{print $1;exit}' /etc/passwd)

如果您真的想查找正在运行 进程的所有者,请记住 Apache 经常启动一个根拥有的“主”进程,然后以 Web 用户身份启动子进程。所以也许是这样的:

user=$(ps aux|awk '$1=="root"{next} /www|http|apache/{print $1;exit}')

但是您还应该能够根据操作系统检测来确定事物,因为事物往往遵循标准:

case "`uname -s`" in
Darwin) user=_www; uid=70 ;;
FreeBSD) user=www; uid=80 ;;
Linux)
if grep Ubuntu /etc/lsb-release; then
user=www-data; uid=$(id -u www-data)
elif [ -f /etc/debian_version ]; then
user=www-data; uid=$(id -u www-data)
elif etc
etc
fi
;;
esac

我不知道检测不同 Linux 发行版的最佳方法,因此您可能需要做一些额外的研究。

关于regex - 使用 bash 获取 http 进程用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com