Run bash script after login
All interactive sessions of bash
will read the initialization file ~/.bashrc
.
So you can just add the script at the end of the root
's .bashrc
i.e. /root/.bashrc
, assuming the script is executable:
echo '/path/to/whiptail.sh' >>/root/.bashrc
Now the script will be always run when root
opens a new interactive shell. If you only want to run while login only, not all all interactive sessions you should rather use ~/.bash_profile
/~/.bash_login
/~/.profile
(the first one available following the order).
Run a Bash Script automatically upon login on Unix
If you already had a ~/.profile file it should be fine to just add it there. Otherwise look for a ~/.bash_profile and add the line there.
Did you make sure that your file is executable? Otherwise it will not work.
Here is an example code (make sure to adapt to your needs):
echo "echo 'foo'" > /tmp/execute_me
chmod u+x /tmp/execute_me
echo "/tmp/execute_me" >> ~/.profile
login from another console (for safety), and you should see "foo" printed in your console somewhere.
If you want your script to be executed whenever a shell is used (even not interactive, you should add the code to the ~/.bashrc, read this for details: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work)
how to run script after user login but not for root
You could check the EUID
in the script:
if [ ${EUID} -eq 0 ] ; then
echo "Not running this script since you are root"
exit 1
fi
Execute command if I logged in from a specific user
The who am i
command will show the name of the user that originally logged in on that terminal. You can use that to detect if you've switched from one user account to another with su
. So in ~user1/.profile
you can put:
orig_user=$(who am i | awk '{print $1}')
if [[ $orig_user = user2 ]]
then
sh script.sh
fi
This is possible to using bash script for User Authentication Banner in SSH?
You can put your customized bash script in motd
. Actually, the contents of /etc/motd are displayed after a successful login but just before it executes the login shell.
You can read about motd
on Debian wiki and also you can read CUSTOMIZE YOUR MOTD tutorial.
How to run script as another user without password?
Call visudo
and add this:
user1 ALL=(user2) NOPASSWD: /home/user2/bin/test.sh
The command paths must be absolute! Then call sudo -u user2 /home/user2/bin/test.sh
from a user1
shell. Done.
Related Topics
Linux: Disable Using Loopback and Send Data via Wire Between 2 Eth Cards of One Comp
How to Sleep 10 Seconds Before Running a Linux Command
In Linux, How to Create a File Descriptor for a Memory Region
Killing Process in Shell Script
Powershell's Equivalent to Linux's: Ls -Al
Cx_Oracle: Distutils.Errors.Distutilssetuperror: Cannot Locate Oracle Include Files
How to Tell Binary from Text Files in Linux
How to Get Screen Dpi (Linux,Mac) Programatically
How to Find Sample Source Files for Linux Device Drivers, Third Edition
Qemu on Raspberry Pi Arch Linux Latest Sd Image
How to Find The List of Processes Using a Particular Kernel Module
Tools Required to Learn Arm on Linux X86 Platform
Too Many Open Files Error on Lucene
Amazon Ec2 Lost Private Key, How to Get Access to The Server
How to Copy The Top 10 Most Recent Files from One Directory to Another