I want to set up the ever popular fortune program to display a random fortune each time a user logs in. The program is installed, but which config file do I need to edit to get the program to automatically run after each successful login?

Dani AI

Generated

Good call — ’s idea of invoking fortune from a login startup script is the right direction. A couple of small tweaks make it safer and cleaner for everyday use: only print a fortune for interactive terminals (so automated/scp/git-over-ssh sessions aren’t corrupted), test that the fortune program actually exists, and format output to the current terminal width so long lines don’t look terrible.

Try adding a short, guarded snippet like this to your shell startup (adapt it to your shell’s login file):

if [ -t 1 ] && [ -n "$PS1" ]; then
  if command -v fortune >/dev/null 2>&1; then
    WIDTH=$(tput cols 2>/dev/null || echo 80)
    fortune | fmt -w "$WIDTH"
  fi
fi

For a system-wide setup for all accounts, drop a small script into /etc/profile.d/ (or the equivalent on your distro) rather than editing every user file. If you use zsh or csh, put an equivalent check in the appropriate startup file for that shell. Note that many GUI login managers don’t source the same files as a text login; if you want a message shown at graphical login you’ll need a display-manager or distro-specific mechanism.

If the built-in fortunes are too corny or contain material you don’t like, create a custom fortune file: plain text entries separated by % and compiled with strfile, then install it into the local fortunes directory. If a fortune doesn’t appear, check the fortune path (command -v fortune), file permissions, and that the snippet is being sourced for interactive shells only.

ok ... got it! In that bash config thingie file (more specifically, the last line of .bash_profile) in my home directory ... I just typed /usr/games/fortune and it worked :)

My problem now is trying to figure out these really stupid, corny jokes. I *would* say they just went over my head, but everyone I ask doesn't have a clue what they mean either. Oh well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.