2

It arose from the situation when I wanted to run a script on every time the system starts.

I noticed that there are a number of places such as init.d, rc.local, bash_profile, ~/config/autostart, ~/local/share/applications and a ton of other places that run at everytime the system starts. Which is quite understandable. But it does show the obscure picture of a system start-up.

May I know if someone can point me to a standard resource on how to briefly and quickly understand

  1. Various stages involved in booting an Ubuntu system and
  2. (Leaving out the less interesting junk) What all could be of a user's actual interest for his customizations in general?

Kindly consider that I did already google and found awful resources with incomprehensible details. Would be grateful, if you can actually explain by yourself in layman terms or, at least point me to a place which is not a big-fat reference manual that is not readable for a layman.

Let me be upfront for those nitpickers, these answers are not useful:

While they might share some similarity, all of them have clearly failed to obtain anything useful for beginners. For e.g. the last could obtain only one chart-producing solution.

1 Answers1

3

GRUB loads the kernel, the kernel starts the init process (typically /sbin/init). It's what happens after that is typically of interest. In various stages of Ubuntu's history, there have been three init systems:

  1. SysV init, the Dark Ages
  2. Upstart, Ubuntu 9.10 - 14.10
  3. Systemd, from Ubuntu 15.04

Init systems, in general, start the various services that are supposed to be run, in addition to other duties. What does an init system do?

/etc/init.d is where the shell scripts of interest of the old sysv init system reside. If you can, just avoid it. It exists only for backward compatibility reasons.

/etc/rc.local is a script from the sysv init era, which still works because of backwards compatibility. It's good for short-lived commands to be run at system startup as root. It's executed once during startup. For long-lived tasks, use Upstart (/etc/init) or systemd (/etc/systemd/system) services instead. Both Upstart and systemd allow starting programs conditionally, but the way they do it is different.

Once you login to a GUI, most desktop environments will start applications listed in ~/.config/autostart. It's a good place for commands that should be run after you login to a GUI, like starting your mail client. They're executed each time you login, so each time you logout and re-login, they will be executed (unlike /etc/rc.local).

~/.config/upstart is like ~/.config/autostart, but it is handled by a per-session Upstart process that is started when you login to a GUI. It's more flexible than ~/.config/autostart, which simply executes the commands at login. This is useful up till Ubuntu 16.10, where the per-session process is now systemd (from 15.04 to 16.04, the main init was systemd, but per-session inits were Upstart). See, for example, my answer here: How do I run a script on Unity login/logout?


Now you can probably make more sense of the options in How to run scripts on start up?


~/.bash_profile, ~/.profile, ~/.bashrc, etc. are not good for starting commands at startup. These are usually read by command-line shells, so when you start a terminal, or login to the TTYs. So, .bashrc is good for running a command each time you open a terminal, and .profile for when you login from the TTY. (See this answer.) These files are also mostly used in setting environment variables, but the canonical place for environment variables is /etc/environment for system-wide variables and ~/.pam_environment for user-specific variables. See this answer.

~/.local/share/applications is irrelevant to this discussion.

muru
  • 197,895
  • 55
  • 485
  • 740
  • Thats a great description. Thanks! I will wait for a competently better answer :) BTW, Is it possible to know the standard order and a listing of all possible locations and scripts? – Loves Probability Nov 02 '16 at 07:24
  • To run a single script (which is not a full-fledged service intended to be managed by upstart or systemd) at startup, there is also the option of adding it to the crontab with @reboot time specifier. See man 5 crontab. – zwets Nov 02 '16 at 07:49
  • 1
    Note that /etc/rc.local is not from the van Smoorenburg Linux System 5 clone era, or even from the System 5 era. It is a triple backwards compatibility measure, because it was System 5's backwards compatibility mechanism for the Unix rc mechanism that preceded System 5 rc. – JdeBP Mar 06 '18 at 07:33
  • @JdeBP Noted, but I meant the sysv era of Ubuntu. :) – muru Mar 06 '18 at 07:37