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,355
  • 17
  • 105
  • 120
  • Where is the .bashrc file located? – Thomas Adema Nov 26 '17 at 21:02
  • In ~ of your user, if its not existing you can create it and make sure that the ~/.profile includes it. – Videonauth Nov 26 '17 at 21:04
  • Found it! Should i add a reference to where the file is located or the code of the script directly? – Thomas Adema Nov 26 '17 at 21:04
  • You can add your lines directly at the end of the .bashrc file just leave out the exit line (e.g. the last line you posted as script above). And if you ever need a fresh .bashrc file you can copy it from /etc/skel – Videonauth Nov 26 '17 at 21:08
  • doesn't works on my 18.04.3, works by add in ~/.profile like explain in https://askubuntu.com/questions/270049/how-to-run-a-command-at-login – bcag2 Dec 18 '19 at 09:43
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)