I have a script that I need to run at startup. I have it in init.d. I need to execute one of the commands within the script with root permissions. How do I go about doing this?

Edit 1:

For reference, this is a ClearCase/ClearQuest RHEL 6.7 server. There looks to be an issue relating to CC starting before the license manager. So, I want to run the following (all requiring root permissions):

 /path/to/atria_start stop  # stop CC
 /path/to/start_lmgrd start # start license manager (flexlm)
 /path/to/atria_start start # restart CC

These don't happend. I have them in a simple script "cc_startup.sh" in /etc/init.d

  • 1
    Scripts run by the init system are already running as root. Do you need to run a portion of the script as a normal user? – jecxjo Jan 27 '16 at 19:50

If it's in init.d then it should be running as root. If you want it to run at start up then place it in for example /etc/rc.d/rc2.d or /etc/rc2.d depending upon your OS distro/version. You'll want to create a link in the following format for example:

S##Name -> ../init.d/scripthere.sh wherein ## is equivalent to when you want it to run at start up. Do you want it coming up before ssh? After apache? Before your database? etc etc. Knowing that will tell you which numbers to put in.

To achieve this you'll want to run ln -s ../init.d/scripthere.sh S##name from the proper rc directory.

| improve this answer | |
  • Edit 1 expounds on what I would like to do. I am not sure what you are getting up wrt script numbering. I suppose it could run at the very end. – basil Jan 28 '16 at 15:28

If you are creating your own init script on RH and derived distros then you should really write it to use the default chkconfig startup configuration utility (as that is what almost all RHL admins know and use).

The following covers the basics quite well. Creating a new chkconfig init script

| improve this answer | |

I have tested it in Bash, but not in other shells. if want to make this work on your machine only. You can use the following:

echo $MY_SUDO_PASSWORD | sudo -S <command>

This will run under sudo without prompting for password, if $MY_SUDO_PASSWORD matches your sudo password of course.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.