Ubuntu – Differences how to run scripts at startup

bashscriptsstartup

I've read various questions about how to run script at startup but I'm not quite sure what the differences are and what's the actually the right way to do it without GUI.

  • /etc/rc.local
  • cron job using @reboot
  • ~/.profile
  • /.bash_profile
  • /.bash_login

Examples I need to run at startup

  • apt-get update
  • set some permissions
  • set display brightness
  • turn numlock on
  • setup touchpad
  • run some apps
  • run some scripts as root (should I call them with sudo even if main script is already executed as a root?)
  • run some scripts without root

It would be nice to has a single one script that handles all these things in the one file. How to do it properly?

Best Answer

~/.profile, /.bash_profile, and /.bash_login are better for defining the environment variables and setting up the environment, than for running scripts. I would not recommend these.

/etc/rc.local can be used for that, but remember that it will start before GUI starts . Unless it is for a script that only sends notification to GUI. Note that you will need to call your script from /etc/rc.local in format /full/path/to/script & (& to avoid blocking other scripts you may have there from execution). You will also need export DISPLAY=:0 variable in either the script itself or within /etc/rc.local . Apps that need GUI will need this variable to exist. Same idea with cron job using @reboot.

If you want to run scripts at GUI login, the proper way is to create .desktop file for each script in your ~/.config/autostart/ folder. You can do it by hand, or use Startup Applications app that does exactly the same thing for you. Just open the Unity Dash and type in "Startup Applications"

Overview of your examples

  • apt-get update doesn't need GUI , just network. /etc/rc.local is ok for this one.
  • set some permissions , this doesn't need GUI too, just use /etc/rc.local
  • set display brightness doesn't need GUI too , but may need root privillege. If you want to change it before login , you will need to write to a special file. Consult my other post on this subject
  • turn numlock on may not need GUI,too
  • setup touchpad depending on your method, may or may not need GUI
  • run some apps if they are GUI apps, they will need to be started from Startup Applications I described above.
  • run some scripts as root you can use /etc/rc.local or cron for these. Running GUI apps as root is bad idea for many reasons, so please avoid that.
  • run some scripts without root use the Startup Applications method.
Related Question