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.

  • 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.
    – Malabarba
    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).
    – Malabarba
    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

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

  • 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?
    – Malabarba
    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
  • Have in mind that this run the script when the cron daemon is started so is posible that other system that you could need aren't ready yet.
    – PhoneixS
    Apr 27 '17 at 11:18

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
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.

  • Is there a list of upstart events that can be used?
    – sunside
    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. Oct 29 '10 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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