Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any config file that is evaluated once, everytime operating system starts up. bashrc does not qualifies since it gets evaluated everytime a gnome terminal starts..

share|improve this question
add comment

6 Answers 6

up vote 2 down vote accepted

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.

share|improve this answer
    
This does not answer the question, because the user can have a server without gdm. Also, ~/.profile is sourced also when you login to a virtual terminal, so I would not advice to put graphical applications startup there. –  enzotib May 28 '11 at 12:29
    
the ~/.profile method works... –  Richard May 28 '11 at 19:31
    
@enzotib: it does work: ~/.profile is executed no matter if you have GDM or not. Even SSH/telnet/text mode logins with no X11 at all. So for text mode commands, Id say use profile. For graphical ones (synergy for example), use the 2nd one. Both cases, text and graphical, are covered by my answer. And no need to config cron –  MestreLion May 29 '11 at 4:36
    
@MestreLion: ~/.profile is executed for running GDM or lightdm even if you have a different shell from bash, because bashis used to start the graphical environment. Now, if you (like me) are using zsh and you login via a virtual console, ~/.profile is not executed. System-wide one-time commands to be executed at boot should go to /etc/rc.local... –  Rmano Jan 3 at 18:09
    
@Rmano: good point about zsh as user default shell not executing ~/.profile in console (text) logins. I'll try to add that to my answer, thanks! –  MestreLion Jan 8 at 14:48
show 1 more comment

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
share|improve this answer
    
not working... after crontab -e, this line is added "@reboot /usr/bin/xmodmap /home/XXX/scripts.sh". when I run the same command on terminal, it works.... dont know why...(should be no printout of this scripts) –  Richard May 28 '11 at 19:16
    
@Richard crontab absolutely works, it is one of the cornerstones of Linux ;) Check your script and add some output to a file inside the script. Most likely your script does crash due to showing output on the screen. Also take note that cron does NOT use a PATH unless you tell it to. Have a look here: phwinfo.com/forum/comp-unix-shell/… for some pointers. –  Rinzwind May 28 '11 at 19:19
    
@Richard have a look at the link I posted in the answer (help.ubuntu.com/community/CronHowto) and look at the paragraph: Further Considerations. –  Rinzwind May 28 '11 at 19:32
1  
Did you check execute and user permitions of your scripts? I never used cron, so not sure. Which user runs the cron jobs? If its not root, then may be a user permition issue –  MestreLion May 29 '11 at 4:40
1  
Also, your scripts are at /home. Is /home in a separate partition? If the cron job is started before /home is mounted, then it will have no access to your scripts. –  MestreLion May 29 '11 at 4:41
show 2 more comments

Beside other answers, you can put your startup (superuser) commands in /etc/rc.local.

share|improve this answer
    
not work... too. see comments to @Rinzwind –  Richard May 28 '11 at 19:18
    
Should work. Well, it works for me since ever. Have you tested your scripts without a graphical environment and as root? If you post it we can help... –  Rmano Jan 3 at 18:10
add comment

Have you looked into cron jobs? You can set one at reboot @reboot in your crontab

share|improve this answer
add comment

Just add commands to /etc/rc.local. It is executed once at the end of the boot process. Make sure you exit with error code 0.

(0)asus-romano:~/PDF% cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

I use this on other computer since... since... well, probably Ultrix in 1989. You have to make sure your script works invoked as root and without graphical interfaces. A way to test it is running it form a virtual console (Ctrl-Alt-F1, Ctrl-Alt-F7 to go back to your graphical environment) in a root shell (sudo -i).

Be warned that a wrong command in that shell can completely destroy your system.

share|improve this answer
add comment

In addition to the crontab solution: You can do this (as root) noninteractively via

echo "@reboot /path/to/runonce-script.bash" | crontab

and put into this script a

crontab -r

for removing it again. Else the script will be executed after every reboot.

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.