I am trying to write bash shell script in Ubuntu 11.10 Linux distro, that will get executed automatically on logging into the system. But I am not able to figure out that what to write in script that by it will get automatically executed on logging in.
I would highly recommend against using
/etc/profile.d/yourscript.sh if it produces output. When you use a non-interactive session, you will receive a
$TERM is not set message. This is noticeable when using the ssh protocol, like scp. Usually not a big deal, however, Veeam doesn't like it and will throw a warning. I know Veeam is not the topic here, but it's worth mentioning that not all applications will gracefully ignore the
$TERM is not set warning.
In short, if the script generates output, place it in the locations specified on the first line. However, if you're modifying the environment and your script doesn't generate output, then use the latter.
Just a data point since the question is tagged Ubuntu.
Under Ubuntu 20.04 and 22.04, the only option to autoload of a script/command at each login is (that worked for me) to add it in
As stated in the comments, adding it to
~/.profile doesn't seem to invoke the script. Adding it to
/etc/profile.d/ is not per login.
To execute a shell script on login in Ubuntu 11.10, you can add your script to the ~/.bashrc file. This file is executed each time a login shell starts up, so any commands or scripts added here will be run whenever you log in to your system. To add your script to ~/.bashrc, Open a terminal and navigate to your home directory by typing:
cd ~/ Next, open the .bashrc file in a text editor using the following command:
nano .bashrc Once you have the file open, navigate to the end of the file and add the following line:
/path/to/your/script.sh Make sure to replace "/path/to/your/script.sh" with the actual path to your script. Also, make sure that your script is executable by running:
chmod +x /path/to/your/script.sh Save and close the file, and the next time you log in, your script should be executed automatically. Let me know if it works for you!