5

I want to run a bash script at boot time in CentOS 6. I added the script in /etc/init.d/ directory but all these script are run by root. I want to run script for any user.

migrated from stackoverflow.com Apr 22 '13 at 13:13

This question came from our site for professional and enthusiast programmers.

8

You can add it to your crontab like this:

@reboot /bin/bash /dir/of/your/script

If you are not familiar with crontab, here you have some hints:

  • crontab -l lists current jobs scheduled.
  • crontab -e allows you to edit it.
  • i know how to edit crontab but when i edit using crontab -e it will create file for current user to /var/spool/cron/ if i have this line added in rpm package then it will create cron file for root because for installation purpose i have to login as root. – Vicky Apr 22 '13 at 11:25
  • You can use crontab -u <user> (and crontab -l/-e -u <user>) to set up crontab for other users. – fedorqui Apr 22 '13 at 11:32
  • awesome sir!!! this is the easiest way to autorun startup scripts. I had researched but all of them told that we need to config /etc/init.d/*** bla bla... to complicated!!! – Davuz Feb 23 '17 at 18:32
0

You can use su to drop root privileges by using the -c to run a command, like:

# su user -c whoami
  user

If you want a specific script to drop privileges, then you can just call the script itself with su <user> -c <script> -magic-parameter where -magic-parameter tells the script to do what ever you want it to do as a non-root user.

0

First: It is not a programming question - better place is https://superuser.com/.

Second: You probably using a Linux - but it is better specify ;).

Third: Running scripts at boot time is very dangerous for the system security. Therefore here is a good reason for root-only scripts. So, if you have root access - use it (you're an sysadmin, right?), if you haven't access - it is probably better for you leave it as it is.. :)

If your question is about how to change the effective UID for some rc.d script, you can use something like:

#!/bin/bash    

export MYHOME=/path/to/somewhere

case $1 in
    start)   /bin/su <username> $MYHOME/bin/startup.sh ;;
    stop)    /bin/su <username> $MYHOME/bin/shutdown.sh ;;
    restart)
             /bin/su <username> $MYHOME/bin/shutdown.sh
             /bin/su <username> $MYHOME/bin/startup.sh
             ;;
esac
exit 0

so, you can put your script anywhere and from the init-script will run it with su.

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.