How to Run a Bash Script on Login

Eventually, you may find yourself wanting to run a particular script every time you log in to a Unix machine (SSH hop to another machine, see a detailed system status, etc.) or, you maybe you'd like to improve the experience for all the users on your machine (i.e., show the weather forecast).

To do this, you'd want to write a bash script to perform the desired task and place it in a certain directory for execution.

To get the weather forecast for your area, create weather.sh and place the following contents in it:

curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-87111}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';

Don't forget to replace the 87111 zip code with your own.

Run chmod +x weather.sh to make it executable, and place it in the /etc/profile.d/ directory.

Now whenever a user logs in, this script will run and automatically display the weather forecast. Of course, this applies to any other task you'd want to run as well.

Last Updated: July 3rd, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms