0

I Have a Ubuntu Headless Server and I want to run a basic script after I login how can I configure my server to run the script automatically after login? Script:

clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

2 Answers2

5

You can add those lines at the end of your ~/.bashrc file which will get executed when you login.

I'm talking about the ~/.bashrc serverside. When you have added your lines and logout and ssh back in these lines will get executed. You can leave out the last line of your script.

If the ~/.bashrc does not exist you can simply create it or even better copy it:

cp /etc/skel/.bashrc ~/.bashrc

and make sure your ~/.profile file contains the following lines:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi
Videonauth
  • 33,815
0

Just worked for me the simplest way:

sudo nano ~/.bash_profile

Append any command or script in the end:

echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal 

I intentionally omitted exit 0 command because it exists from ssh automatically, so you will be unable to stay logged in. (Tested on Ubuntu 20.4)