8

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.local file. 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?

3
  • 1
    Are you doing this for convenience or for security? I.e. do you just want to set up your login environment (add aliases, set variables, etc), or do you want your script to run after each login without allowing users to remove it? Commented May 29, 2012 at 10:20
  • For the security purpose. Actually i want to run the script whenever admin log-in the system. Commented May 29, 2012 at 10:23
  • 2
    if any of the answers below helped you, please mark them as accepted. thanks. Commented Oct 16, 2012 at 20:29

3 Answers 3

22

For bash:

You can add your script to /etc/profile.d folder.

More reading about this here and here.

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

Sign up to request clarification or add additional context in comments.

6 Comments

@user976754: I am glad it helped you. Please mark my answer as your accepted answer for this question :)
It looks like you need to have root permissions to create files under /etc/profile.d How then does the file get executed when the user logs in? As the file will be owned by root.
@ams Requiring root permissions for creating script file does not imply it has to be run as root. Files in /etc are always considered affecting a whole system which is implying affection on all users of that system. You definitely don't want to have anyone writing scripts to be run as you log on with "anyone" including user accounts used to run network services like webserver, only.
Scripts in this file seem to be run very inconsistently. This is too unreliable to be a solution.
@Cerin: the solution IS tested, so I totally disagree with you, but you're entitled to your opinion...
|
8

Try adding this to your /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).

5 Comments

Hi Lanzz, I added this in /etc/pam.d/login script. But unable to get exceuted my script...
pam_exec apparently does not display its output on the terminal where the user is logged on. Do you need to display anything in your script, or just to perform something in the background (e.g. send a notification)? You can add a log file where your script's output will be logged: session optional pam_exec.so log=/path/to/logfile /bin/bash /path/to/your/script.sh
I found this line un my /etc/pam.d/login. I think it's the reason why uname gets printed when I ssh to a machiine : session optional pam_exec.so type=open_session stdout /bin/uname -snrvm
My script is supposed to launch some services. When I run script manually the services start. But adding this as your answer didn't get it to work. I had to run the script manually.
It worked for me if I add the script in /etc/pam.d/common-auth instead of login
2

For Linux Mint and Ubuntu with Desktop environment there is a option in main menu called "Startup Applications", just go there and add the path to the script file. This script will run after login in window manager.

1 Comment

this runs the script only when the system had been booted. It does not run on every user log-in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.