LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2005, 10:45 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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?!

Code:
#!/bin/bash
# ATOMIC CLOCK SYNC
echo "Atomic Clock Synchroniser"
su
ntpdate ntp.nasa.gov
hwclock --systohc
echo "Clocks synchronised"
exit
exit
#EOF
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...

Thanks!
 
Old 11-09-2005, 10:57 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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.

Try using something like:
Code:
#!/bin/bash
# ATOMIC CLOCK SYNC
echo "Atomic Clock Synchroniser"
su - -c "ntpdate ntp.nasa.gov ; hwclock --systohc"
echo 'Clocks synchronised'"
# EOF
man su for details.
 
Old 11-09-2005, 11:01 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Rather than having a non-root user become root you might want to do it via root's cron. See the thread at: http://www.linuxquestions.org/questi...hreadid=381120

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.
 
Old 11-09-2005, 11:54 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Might be a good idea to put some checking in there...
Code:
#!/bin/bash
# ATOMIC CLOCK SYNC needs ntp package
echo Atomic Clock Synchroniser
   su - -c "which ntpdate > /dev/null 2>&1"
if [ $? = 1 ]; then
   echo "The ntp package isn't installed"
   exit 1
else
   su - -c "ntpdate ntp.nasa.gov ; hwclock --systohc"
   echo "Clocks synchronised"
fi
 
Old 11-09-2005, 02:51 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
hmm. i dont use it, but isnt that what sudo is for?
 
Old 11-09-2005, 02:59 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
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.
 
Old 11-09-2005, 03:21 PM   #7
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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.
 
Old 11-10-2005, 06:40 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with bash adityavpratap Slackware 9 06-13-2006 09:44 AM
a problem about bash naihe2010 Linux - Enterprise 1 10-15-2005 11:06 AM
Problem with BASH Pheidon Linux From Scratch 5 06-24-2005 02:34 AM
Bash Problem krock923 Programming 3 01-02-2005 01:40 PM
bash problem cL4YmAN Linux - Newbie 1 06-07-2004 01:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration