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.
Asked
Active
Viewed 5,287 times
11
-
1@bruce take a look of this -> http://askubuntu.com/questions/9382/how-can-i-configure-a-service-to-run-at-startup/9384#9384 – hhlp Oct 27 '10 at 18:57
-
1You 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 think I wasn't clear. This script will be run on an ubuntu-server, it relies on networking, and on the mounting of a particular partition (which auto-mounts in fstab). Sometimes I turn on the server computer but don't actually login (I just leave it on for when I need to login remotely), so I want this script to be run on every boot without relying on login. That's why I said I want it to be the very last thing tu run but before login. – Malabarba Oct 28 '10 at 01:04
2 Answers
8
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
Kees Cook
- 17,473
-
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 01: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 03: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
4
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.
Gilles 'SO- stop being evil'
- 60,593
-
-
@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 'SO- stop being evil' Oct 29 '10 at 18:41