ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Rep:
bash problem
Hi ya!
I am writing a bash script in which I have to use commands that are only available for the superuser. The problem is that I need the su command in bash, so I have to enter the password, but after that it does not run the rest of the script. How can I come around that problem? Or can I "integrate" the password in the script somehow?!
The programme should synchronise my clock (and hardware clock) to some atomic clock. I will make a button of it on my desktop, so I can - whenever I want - synchronise, since my clock should not be synchronised necessarily all the time. I just want to click it and done, not entering my password if that is possible...
When your script executes the su command it starts a new shell (and patiently waits). After it is closed (by you, manually) it will return control to the script and execute the commands below it (as your regular user). That is the reason why this approach does not work.
To add entries to crontab you type "crontab -e" after becoming root. Once its in cron it will run periodically as root to set your time.
Having said that - the answer to your question as phrased is you need to do the
"su -c <command>" syntax.
You could either have it do the su -c for each of the commands (ntpdate and hwclock) OR just put both commands in a separate script and have your first one do su -c calling that separate script name.
Originally posted by schneidz hmm. i dont use it, but isnt that what sudo is for?
Yes but sudo requries you to input your own password on invocation so wouldn't be a work around. Sudo will cache the password for your session for some finite period of time but if you wait to long between sudo commands it will ask for your password again. This is so someone doesn't leave their terminal open and walk away allowing some other user to get access to things they shouldn't have.
As I mentioned in my prior post the best way is to just run this via cron. Presumably he doesn't want password because he wants to automate it and that's exactly what cron is designed for.
Why not just run the ntp daemon? It will maintain the clock accuracy better with fewer large jumps in the time, as periodically running ntpdate might tend to do.
I was initially going to say run ntp daemon myself but found it doesn't come with Debian even though ntpdate does. Presumably there's an ntpd package one could download but for me it was just as fast to set it up in cron.
On my RedHat systems I do run ntpd. On my HP-UX I run xntpd.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.