如何在Linux中用户登录身份验证后运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10797150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:33:29  来源:igfitidea点击:

How to run a script after user login authentication in linux

linuxlogin

提问by user976754

I have a script which I want to run just after user log-in authentication. To accomplish this, I added the script name in /etc/rc5.d/S##rc.localfile. But later i got to know that, anything that is added in rc.local file gets executed in boot time of the system not after the login authentication. Can anyone tell me how to run the script after user login authentication?

我有一个脚本,我想在用户登录身份验证后立即运行。为了实现这一点,我在/etc/rc5.d/S##rc.local文件中添加了脚本名称。但后来我知道,在 rc.local 文件中添加的任何内容都会在系统启动时执行,而不是在登录身份验证之后执行。谁能告诉我如何在用户登录身份验证后运行脚本?

回答by lanzz

Try adding this to your /etc/pam.d/login:

尝试将此添加到您的/etc/pam.d/login

session optional pam_exec.so /bin/bash /path/to/your/script.sh

You will need to check in your script if the current user is actually an administrator (according to whatever your criteria for being administrator are).

如果当前用户实际上是管理员(根据您作为管理员的标准),您将需要检查您的脚本。

回答by woohoo

Alternatively, you can add your script to /etc/profile.dfolder.

或者,您可以将脚本添加到/etc/profile.d文件夹。

More reading about this hereand here.

更多关于这个的阅读在这里这里

Basically, you should give your script the extension .shas all these files are executed in a loop after user logs on.

基本上,您应该为您的脚本提供扩展名.sh,因为所有这些文件在用户登录后都会循环执行。