Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

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

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

3 Answers 3

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.
share|improve this answer
    
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

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.

share|improve this answer

First: It is not a programming question - better place is http://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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.