How Do You Get a Script to Execute Every Time You Log in as the User in Linux?

Problem scenario
You want a script to run whenever a user logs in (e.g., when a sudoer uses "su jdoe", you want a script to run before you assume the jdoe user). You are using Linux SUSE or Ubuntu or a Debian distribution. You found that having a script in /etc/profile.d/ does not work. What should you do?

Solution
To have a script automatically run for a given user, create a .bashrc file in the /home/jdoe/ directory (where jdoe is the username). For CentOS/RedHat/Fedora, place a script in /etc/profile.d/.

To have a script automatically run for a given user, create a .bashrc file in the /home/jdoe/ directory (where jdoe is the username). If one exists modify it. Make sure the .bashrc file has this line appended to it (where /home/ubuntu/foobar.sh is the complete path to the shell script):

/bin/bash /home/ubuntu/foobar.sh

For CentOS/RedHat/Fedora, place a script in /etc/profile.d/ (and do not be concerned about a .bashrc file).

Leave a comment

Your email address will not be published. Required fields are marked *