Running Bash script at login

8,201

The standard location for a script that must run at login is /etc/profile. It will then run for every user (once) when they log in. The user never gets to see the output of the script, it is logged

If it is only for a specific user, it should be added to .profile in their home directory.

With login I mean when you enter your username and password.

Any errors normally show up in ~/.xsession-errors

If it has to run every time you open a terminal window, it should be added to /etc/bash.bashrc or to .bashrc in the user's home directory.

At work, I mount a number of network shares when I log in. This is done in .profile in my home directory (it needs only to be done once).

Every time I open a terminal window I get a fortune cookie. This happens because the last line in .bashrc in my home directory contains fortune.

Share:
8,201

Related videos on Youtube

krato
Author by

krato

I'm a computer science student and I love programming.

Updated on September 18, 2022

Comments

  • krato
    krato over 1 year

    This question has been asked numerous times but for some reason, none of the solutions proposed worked for me. I just want to run a simple script after the user has logged in.

    Here are my attempts:

    • I have tried putting the script in /etc/init.d/ and making a symlink in /etc/rc0.d.
    • I tried scheduling it using the @reboot in crontab (crontab -e). Annoyingly, @reboot does not seem to work in Ubuntu(?). I tried this simple line@reboot echo "hi there" to no avail.
    • I tried putting it into the root's crontab(sudo crontab -e) but still nothing happened. Also a simple echo in this crontab does not work too.
    • I also tried to use the @reboot syntax suggested here (@reboot root /home/me/Desktop/script.sh)
    • Followed this and placed the path of the script in /etc/rc.local

    Notes:

    • I'm using Ubuntu 14.04
    • home is mounted, but I also tried my attempts in a VM where home is not mounted
    • I only want to run the script after the user has logged in
    • Nothing fancy about the script it just echoes "hello world"
    • kos
      kos almost 9 years
      Do you need to run it at a specific runlevel? Do you need to run it in a terminal?
    • krato
      krato almost 9 years
      @kos I guess not, it's just a simple attempt of running a script at startup, it only contains an echo
    • kos
      kos almost 9 years
      I guess you want a terminal to see the output of the script, that's the only problem; the only hack I've ever managed to come up with (at least using gnome-terminal or mate-terminal) is this; change mate-terminal to gnome-terminal and remove sudo -H, and obviously change the chain of commands to just echo hello world
    • kos
      kos almost 9 years
      If you don't need to output to a TTY there are way nicer solution; probably at least some of the methods you've tried worked already, you just couldn't catch the output since the script is not run in a terminal.
    • krato
      krato almost 9 years
      if I use notify-send instead will it send a notification? This way terminal output is not needed
    • kos
      kos almost 9 years
      Yes, given that you use it at the correct runlevel (in this case you want X to be running already, so you could use the Startup Applications method to run a script containing the notify-send command, that way you could also add in other commands).
    • krato
      krato almost 9 years
      can you point out what method I can use to run a script that does a notify-send? I'm using Unity as my DE
    • kos
      kos almost 9 years
      Not on Ubuntu right now, however notify-send 'hello world' should be enough; create a text file named, say, script.sh, say, in ~/; then add a shebang to the start of the file (#!/bin/bash) and the command on the next line; mark the script as executable by running chmod +x ~/script.sh and add an entry to Startup Applications to call it; the command would be simply the path to the script, i.e. ~/script.sh
    • krato
      krato almost 9 years
      @kos that works, thanks. Add that as answer so I can accept. Anyway, I'm still curious why all other methods does not work (using crontab, using init.d, etc.)
    • kos
      kos almost 9 years
      I would, but as it stands it wouldn't really answer the question; maybe you can rephrase the question asking for how to run a notify-send command at startup? Beside that, the methods you already tried probably worked, although none of those methods are meant to output to a terminal; let's pick the first method (/etc/init.d/); that's usually used to start daemons, and those scripts are usually run way before X is actually running; an easy way to check an /etc/init.d/ script's output would be, for example, to redirect the output to a file.
    • boardrider
      boardrider over 8 years
      Have you tried putting what you need inside the ~/.login file?