Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script after user login authentication in linux

Tags:

linux

login

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?

like image 339
user976754 Avatar asked May 29 '12 10:05

user976754


People also ask

How do I run a script from a different user in Linux?

Running Script as Another User. By default, the su command takes an input a target username to switch into. However, we can specify a script to be run with the flag -c. When specified, su command will just execute the script without dropping into a new shell as the target user.


2 Answers

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.

like image 160
scrat.squirrel Avatar answered Oct 13 '22 12:10

scrat.squirrel


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).

like image 26
lanzz Avatar answered Oct 13 '22 10:10

lanzz