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.

How can I get a specific script to run (preferably not as superuser) whenever the machine boots, but before login. It can be the last thing to run on boot. I mostly just want the script to work even if no user logs in.

share|improve this question
1  
@bruce take a look of this -> askubuntu.com/questions/9382/… –  hhlp Oct 27 '10 at 18:57
1  
You can use upstart or cron or probably a whole bunch of other methods. One important thing to know is also how early it can run. E.g. does it need a particular filesystem to be mounted, does it need X to be running, does it need DBus, does it need networking—or more in general: does it need any resources to be present? –  JanC Oct 27 '10 at 19:15
    
@JanC: Yes actually, it needs just about everything you asked (except for X), which is why I'd like it to be the very last thing to be run. There's really no reason to run it earlier either. –  Bruce Connor Oct 27 '10 at 19:33
    
@hhlp: Cool, thanks. But how early will the script run if I use that method? As I mention above, it's best if it's one of the very last things (before login). –  Bruce Connor Oct 27 '10 at 19:34
    
Well, login doesn't require networking, unless you want to login into a terminal server and/or you have your $HOME on a network share... ;-) –  JanC Oct 27 '10 at 21:30
show 1 more comment

2 Answers

up vote 7 down vote accepted

I would recommend using cron. The special time value of @reboot will spawn your job at each reboot as your user. For example, run crontab -e and use:

@reboot /home/yourself/bin/some_script_to_run

For more details on the special time formats, see man 5 crontab

share|improve this answer
    
Thanks, it seems easy enough. This does not rely on which user creates the cronjob right? It runs at the end of boot regardless of logins? –  Bruce Connor Oct 28 '10 at 1:00
    
It absolutely depends on who created the cronjob -- it will run as that user. But it does not depend on them to be logged in. –  Kees Cook Oct 28 '10 at 3:06
    
Works... Thanks. –  Bruce Connor Oct 28 '10 at 12:08
add comment

One possibility is to use Upstart. This lets you specify when you want to run your script in terms of dependencies, e.g. “when the filesystems are mounted and the network interface eth0 is up and running”. Create a file /etc/init/bruce_script.conf (you need to create the file as root) containing something like this:

description "Bruce's boot script"
start on filesystem and net-device-up IFACE=eth0
task
exec su -c '/home/bruce/script' bruce

Consult the upstart documentation for more information, in particular the init(5) manual page for a list of what you can put in that configuration file.

share|improve this answer
    
Is there a list of upstart events that can be used? –  Markus Oct 29 '10 at 18:20
    
@Markus: not that I know of. Standard events have a manual page in section 7 (so dpkg -L upstart |grep /man7/), and you can look at events emitted by and used as triggers in existing scripts in /etc/init. –  Gilles Oct 29 '10 at 18:41
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.