Ubuntu (Linux) – Run program at startup / boot / login

This is meant to be a collection of methods in which you can launch a program automatically on an Ubuntu (Linux) system.

Launch at boot – These will launch regardless if a user logs in or not.

  • Put your script in /etc/rc0.d
    • Runs at boot (before the OS loads).  The scripts in this directory are executed in alphabetical order.  The name of your script must begin with “K99″ to run at the right time.
    • Make it executable (sudo chmod +x YourScriptName)
  • Add a script, starting your file to /etc/init.d/
    • Starts the program as a Service (you can use /etc/init.d/YourScript {start|stop|restart} to interact with the program).
    • Runs after /etc/rc* scripts (scripts essential to the OS).
    • Script should start with “#!/bin/bash”
    • Append an “&” to the end of your script command to let it run in the background as everything else starts.  Ex:  “/home/User/scriptDir/YourScript &”.
    • Scripts will automatically run as root.  To allow it to run as a standard, unprivaliaged user, add “su” to the front of your script path.  Ex:  “su /home/User/scriptDir/YourScript &”.
    • Example:
    • #! /bin/sh
      # /etc/init.d/YourScriptName
      
      case "$1" in
       start)
       #path to the executable file you want to run
       /home/User/scriptDir/YourScriptFile &
       ;;
      stop)
       killall YourScriptName
       ;;
      *)
       echo "Usage: sudo /etc/init.d/YourScriptName {start|stop}"
       exit 1
       ;;
      esac
      
      exit 0
    • When complete, run “sudo update-rc.d YourScript defaults”.
  • Add the path of the file you want to run to /etc/rc.local
    • Runs after /etc/init.d
    • Scripts must always end with “exit 0”.
  • Run as a crontab

Launch at login – These will only launch when someone logs into their account.

  • From the Dash (press the “Windows” key),  “Startup Applications“.
    • These are run AFTER Gnome/Unity starts up
  • Put a .desktop file in ~/.config/autostart.
    • This is the manual version of using “Startup Applications”.
  • Add the script to ~/.gnomerc
  • Add the script to ~/.profile
  • Add the script to ~/.bashrc
    • This will only run when you log into a bash shell (i.e. the command line).
  • Add the script to ~/.bash_profile
  • Add the script to ~/.bash_login
    • If this file exists, Bash will NOT read .bash_login or .profile.

Run a script at shutdown (Not exactly the focus of this post, but worth mentioning here).

  • Put your script in /etc/rc6.d
    • Runs at shutdown.  The scripts in this directory are executed in alphabetical order.  The name of your script must begin with “K99″ to run at the right time.
    • Make it executable (sudo chmod +x YourScriptName)

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *