How to Run a Bash Script on Login

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: June 7th, 2021
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.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Better understand your data with visualizations

    With over 330+ pages, you'll learn the ins and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh, and more.

    © 2013-2021 Stack Abuse. All rights reserved.