9

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?

Bruno Pereira
  • 73,643
lorenzo-s
  • 485
  • 3
  • 8
  • 17

1 Answers1

10

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.

  • So, I've just to put that code here? For now, that file contains only exit 0. And if I can ask, on boot when rc.local is executed, is the filesystem ready so I can also write a log somewhere for that command I want to run? – lorenzo-s Dec 18 '11 at 02:27
  • It 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 ? – Panther Dec 18 '11 at 04:42
  • @lorenzo-s: See my edit. – Scott Severance Dec 18 '11 at 04:52
  • You may also find this answer helpful: http://askubuntu.com/a/20347/3940 – Takkat Dec 18 '11 at 08:31
  • It worked as you suggested, Scott. But the problem is that during boot /etc/rc.local cannot found command my_script, that is in /usr/local/bin and works on my user session. – lorenzo-s Dec 18 '11 at 12:32
  • 1
    Ok, I solved moving my_script from /usr/local/bin to /usr/bin. I'm also able to save log in /var/log using my_script &>> /var/log/my_script. Thank you very much. – lorenzo-s Dec 18 '11 at 12:45
  • If you create /etc/rc.local remember to add #!/bin/bash at the beginning. In my case it is not running without it. – Tommimon Jan 06 '23 at 21:37