Any file executed once when system starts up

cron can be of help here.

Besides starting something on a minute,hour,day of week, month etc it also has some special operations:

@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

Editing is done from command line with the following command:

sudo crontab -e

at the bottom of the file (below the # m h dom mon dow command) you can add a line that executes what you want like so @reboot /directory/to/file will execute /directory/to/file during boot.

Just one warning: you need to make sure that there is no output from that script or that the output is redirected to a file (or /dev/null) since there is no display for cron to send the output (and it will end the operation).

Example sudo crontab -e

# For more information see the manual pages of crontab(5) and cron(8)
# 
PATH=/usr/sbin:/usr/bin:/sbin:/bin
# m h  dom mon dow   command
@reboot /usr/bin/testscript

and...

cd /usr/bin/
sudo vi testscript 
echo "works" >/tmp/testing
chmod 775 testscript

Now for a reboot... And here is a working example:

ls -l /tmp/testing
-rw-r--r-- 1 root root 6 2011-05-29 08:34 /tmp/testing
rinzwind@discworld:/tmp$ more /tmp/testing
works

There are 2 places I use when I need to add "run-once" commands:

Once at every user login (be it Graphical/GDM or text/console login): ~/.profile

Pros:

  • It works even if no GDM/X11/Graphical server is used. Meaning it will work with SSH and text-mode logins
  • It is NOT evaluated when a gnome terminal starts, as required. ONLY at login
  • Executed with user priveleges, its secure while allowing full access to personal scripts.
  • Run after all mounts are done, so the whole filesystem is avaliable.

Cons:

  • If a user logs in, logs out and logs in again, it will be executed again, once per login. So its not a "true" system start up only. But it may suit your need.
  • Since this is executed even in text-mode logins, its advisable NOT to place any command that requires a GDM/X11 server (like synergy daemon)
  • Bash is only used with text-mode logins. So if using GDM, no bashisms are accepted in the script, since Ubuntu will run it with dash

Once when GDM starts (before any user logs in): /etc/gdm/Init/Default

Pros:

  • Executed only once, no matter how many users log in or out
  • Can be used for both text and graphical commands
  • Run after all mounts are done, so the whole filesystem is avaliable.

Cons:

  • Run using gdm user. So personal scripts and path to them must be world-readable and executable
  • Cannot be used if no GDM server is used (duh). So it wont run in text-mode start-ups
  • Again, not run in bash, so bashisms must be avoided.

Use the method that suits your need.