Run As Root On Reboot

Home » CentOS » Run As Root On Reboot
CentOS 10 Comments

Folks

Is there a way to reboot and have a script run without intervention. During initial setup, I’d like to avoid the manual actions of logging on as root and executing a command, but instead have that command run without intervention. The output of the command would still show up on the terminal that initiated the reboot.

Security is not a concern here. And I don’t want to invoke high-powered functions like “jumpstart”.

David

10 thoughts on - Run As Root On Reboot

  • At 04:47 PM 10/28/2020, Frank Cox wrote:

    Alas, I think rc.local has become irrelevant with systemd, which is most Linux distros is the way forward.

  • I suggest running:

    systemctl cat rc-local.service

    The trick is to make the /etc/rc.d/rc.local file executable.

    However, honestly, what you are asking for should be to create your own systemd service to launch on boot. Systemd service units are trivial to write.

  • vi /etc/rc.local -> add what you need to run on boot chmod 700 /etc/rc.d/rc.local systemctl enable rc-local.service

    It’s still supported, but disabled by default by both the unit being disabled and the file being non-executable. This works on 7, no idea about 8. They probably broke it there.

    John

  • –“During initial setup” is vague. Lots of stuff happens during startup. With systemd, you can control what triggers your script to run. Does it need the filesystems up? Does it need networking? This will be a part of your systemd unit file.

    This one might be hard. Is there a way to know where a reboot command came from? Does the kernel or systemd save this somewhere?

  • At 07:10 PM 10/28/2020, Kenneth Porter wrote:

    By “initial setup”, I meant during the initial install of the operating system, starting from “net-install”. Maybe one user is defined. The reboot command is issued from a script that was initiated by hand.

  • so you want this script run /before/ the manually issued reboot ? why not just run the script and have IT initiate the reboot when its done ?

  • Having read this thread, I still can’t tell *when* you want a script to run.  If you want it to run during the initial install, then I can’t make any sense of how that relates to a reboot.

    If you want to run a command during the setup, then provide a kickstart file with a %post section:
    https://pykickstart.readthedocs.io/en/latest/kickstart-docs.html#chapter-6-post-installation-script

    If you want to run a command after rebooting and display the command output on the console, then use the openvt command, possibly with the -w flag:
    https://linux.die.net/man/1/openvt

    If you mean kickstart: I’m pretty sure that’s your only option for running commands during the installation.

  • So you want it to run as the final part of the install process??

    If that’s the case, then you should create a kickstart file with the post installation script in it:

    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/installation_guide/sect-kickstart-syntax#sect-kickstart-postinstall

    If you want/need it to run the script after the install has completed and the first reboot, then you need to look at the FirstBoot scripts

    https://access.redhat.com/solutions/2028143

    P.

  • […]

    Can you explain the use case a little more here? It may actually _really_ be worth your time to learn about kickstart — it’s highly powerful, but not really “high-powered” in a difficultly sense.