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.
3 Answers
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.– VickyApr 22, 2013 at 11:25
-
You can use
crontab -u <user>
(andcrontab -l/-e -u <user>
) to set up crontab for other users.– fedorquiApr 22, 2013 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!!!– DavuzFeb 23, 2017 at 18: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.
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
.