Run command at boot as root

I need to run a bash script at boot-time as superuser in Xubuntu. I don't know where to start. I have to do something described in a Arch Linux guide. That guide says exactly:

Add following at the end of the file : /etc/rc.sysinit

# Do my action
/etc/rc.d/do_my_action

/bin/dmesg >| /var/log/dmesg.log

But I do not have /etc/rc.sysinit and /etc/rc.d/ in Xubuntu. I'm sure there are equivalent ones, right?


Solution 1:

The file you want is /etc/rc.local. This script will be run near or at the end of the boot process. Thus, everything should be up by this time.

However, as @bodhi.zazen pointed out, Upstart might be the best option for you. Check it out.

Note 1: in 15.04 and later, Ubuntu uses systemd and not Upstart. Therefore if you want to take a service approach, see this answer here by @muru

Note 2: In 16.10 there is no /etc/rc.local by default, but if you create it and make it executable (sudo chmod u+x /etc/rc.local) it will work because there is a systemd service to pull it in if it exists. Don't forget to put a line with exit 0 at the end of the file to prevent any failed commands causing the entire boot process to hang.