Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Does anybody know (of a resource listing) what scripts are run automatically, and when, from the moment I startup the computer until it's shutdown again.

I know several places where I can add scripts to run automatically, but I’ve never been able to figure out a more-or-less complete list.

For example, I know these:

  • ~/.profile is executed on login
  • I can use crontab -e to add scripts that will trigger on certain times and/or dates

But I can't figure out where to put scripts so that they'll be executed

  • after boot, before X is initialized
  • after the X (or other graphical shell) is initialized, but before anyone's logged in
  • after logging in on an X session
  • before/after fast user switching
  • before logging out of an X session
  • before shutdown on the X shell
  • after shutdown of X, before system halt
  • after login/before logout via tty-1..6
  • after login/before logout via SSH

Is there such a list? If not, can we assemble one here?

share|improve this question
1  
As it is, this is unanswerable. It depends on what software is used and there is no standard composition. You'd not only need an answer for every OS/distro, but also for all their releases, and then again for all different ways you can assemble DIY Linuxes, and then again branch on if certain environment vars are set, with all possible permutations of installed packages or software built from source etc. ad nauseam. You'd have to confine the question very narrowly for it to make sense. –  user112553 Mar 25 '12 at 13:39
    
@user112553: So this is not standardized across distros? In that case, I’ll try to narrow down the scope of my question. –  Martijn Mar 26 '12 at 7:45
add comment

2 Answers 2

up vote 4 down vote accepted

For Ubuntu Linux we have the solution as:

  1. For after boot, before X is initialized: Ubuntu starts X-Windows from GDM (those using the ubuntu-desktop) or from KDM (those using kubuntu-desktop)

    To run a script prior to GDM/KDM, you have to write your own upstart script. For this, you can read Upstart Cookbook.

  2. After user switching: You have to add the script in the ~/.bashrc file.

  3. After Login in via tty: You have to add the script in the ~/.bash_profile file.

  4. After Login using ssh but not while local login using tty: In this case also you have to add the script in the same ~/.bash_profile file but in this case, test for the presence of the SSH_CONNECTION environment variable, and only run the commands if you find it exists.

  5. Before Logging out via tty: You have to add the script in the ~/.bash_logout file.

  6. Before Logging out via ssh: You have to add the script in the ~/.bash_logout file, but in this case also , test for the presence of the SSH_CONNECTION environment variable, and only run the commands if you find it exists. In the file you have to add the line

    if [[ -n $SSH_CONNECTION ]]

share|improve this answer
2  
Most of the answers only apply to bash users, and Ubuntu doesn't use gdm anymore, and the user don't ask specifically about Ubuntu. –  enzotib Mar 25 '12 at 11:33
1  
@enzotib: You have to take some flavour of Linux as standard for answering this question. Since I am Ubuntu user, I replied this question like this. –  pradeepchhetri Mar 25 '12 at 11:39
add comment

If you're interested in Linux built in tasks, you might also refer to Linux runlevels which is mode of operation that implement Unix System V-style initialization.

  • 0 Halt Shuts down the system.
  • 1 Single-User Mode Mode for administrative tasks.
  • 2 Multi-User Mode Does not configure network interfaces and does not export networks services.
  • 3 Multi-User Mode with Networking Starts the system normally.
  • 4 Not used/User-definable For special purposes.
  • 5 Start the system normally with appropriate display manager. ( with GUI ) As runlevel 3 + display manager.
  • 6 Reboot Reboots the system.

For example, below you can see a partial list of Linux processes and their runlevel settings:

chkconfig --list
acpid           0:off   1:off   2:on    3:on    4:on    5:on    6:off
anacron         0:off   1:off   2:on    3:on    4:on    5:on    6:off
atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off
auditd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
autofs          0:off   1:off   2:on    3:on    4:on    5:on    6:off
avahi-daemon    0:off   1:off   2:off   3:off   4:off   5:off   6:off
avahi-dnsconfd  0:off   1:off   2:off   3:off   4:off   5:off   6:off
.
.
.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.