Run command at boot as root
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.
lorenzo-s
I'm an italian web and Android developer. I'm a Javascript lover enthusiast about Node.js and React development, and a PHP/MySQL expert. I started working at D-One Software House (Carpi, Italy) after my degree in computer engineering. Look at my portfolio on my website, fork me on GitHub or follow me on Twitter.
Updated on September 18, 2022Comments
-
lorenzo-s almost 2 years
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? -
lorenzo-s over 12 yearsSo, I've just to put that code here? For now, that file contains only
exit 0
. And if I can ask, on boot whenrc.local
is executed, is the filesystem ready so I can also write a log somewhere for that command I want to run? -
Panther over 12 yearsIt sounds as if you are trying to run a boot (init) script. Ubuntu uses upstart. rc.local will run a command, or series of commands on boot and may or may not be your best option. What are you trying to do exactly ?
-
Scott Severance over 12 years@lorenzo-s: See my edit.
-
Takkat over 12 yearsYou may also find this answer helpful: askubuntu.com/a/20347/3940
-
lorenzo-s over 12 yearsIt worked as you suggested, Scott. But the problem is that during boot
/etc/rc.local
cannot found commandmy_script
, that is in/usr/local/bin
and works on my user session. -
lorenzo-s over 12 yearsOk, I solved moving
my_script
from/usr/local/bin
to/usr/bin
. I'm also able to save log in/var/log
usingmy_script &>> /var/log/my_script
. Thank you very much.