Getting a script to run on boot, not on login

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.


Solution 1:

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

Solution 2:

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.