Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-02-2009, 02:07 PM
|
#1
|
|
LQ Newbie
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17
Rep:
|
Creating a script that will log into root and enter password and then run commands.
I have been looking around for a way to make a simple bash script log into root at one point and enter the password then continue with more commands.
So far my script looks like this:
#! /bin/bash
mount /dev/sdd1 /mnt
rsync -av --stats /mnt/* /home/chunky/Desktop
umount /dev/sdd1
su chunky
Any help to get it to log into root so it will be able to mount just running one script would be great!
Thank you!
|
|
|
|
11-02-2009, 02:25 PM
|
#2
|
|
Member
Registered: Feb 2009
Distribution: FreeBSD, OpenBSD, NetBSD, Debian, Fedora
Posts: 770
Rep:
|
You can login as root by disabling the display manager.
You can also configure the display manager to allow root logins.
Out of curiosity, why don't you just use a root terminal within the user environment or use the virtual terminals?
You can also try your commands under single user mode.
Last edited by Mr-Bisquit; 11-02-2009 at 02:26 PM.
|
|
|
|
11-02-2009, 02:31 PM
|
#3
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,971
|
Look into "sudo".
|
|
|
|
11-02-2009, 02:34 PM
|
#4
|
|
LQ Newbie
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17
Original Poster
Rep:
|
This needs to be a back up of a pc with no monitor, keyboard or mouse. The script will sit on the pc and a remote pc will tell it to run when someone plugs a usb hdd into it.
I guess it would be simpler to have the remote ssh as root and run it, but it is plaguing me now that i cant make the script switch between root and user.
Since a couple of your comments are new to me, can you show me a simple script example how to tell it to log into root and enter a password and press the enter key, that's where i'm stuck.
Thanks again!
|
|
|
|
11-02-2009, 02:36 PM
|
#5
|
|
LQ Newbie
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17
Original Poster
Rep:
|
I'll look at the man page for sudo for script examples also Mr.B.
|
|
|
|
11-02-2009, 02:49 PM
|
#6
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,971
|
You need to configure sudo correctly, to allow at least the commands you need. You can as well configure it so everyone can run anything as root just by doing "sudo <command>", then you can use "sudo <command>" in your script when it needs to run something as root.
You could as well run the whole script as root by using "sudo <script.sh>", in any case, the first step is learning how sudo works.
But, looking at your last post:
Quote:
|
The script will sit on the pc and a remote pc will tell it to run when someone plugs a usb hdd into it.
|
What exactly do you need it to do? You might be able to do this with udev rules and no further complications.
|
|
|
|
11-02-2009, 03:05 PM
|
#7
|
|
LQ Newbie
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17
Original Poster
Rep:
|
Just mount the drive and rsync then umount and email success.
|
|
|
|
11-02-2009, 03:56 PM
|
#8
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,971
|
Well, the basics of your script could be something like:
Code:
#!/bin/bash
die() {
echo "$1"
exit 1 # error
}
mount <whatever> || die "error while mounting"
rsync or cp or tar or <whatever else> || die "error while running rsync"
umount <whatever>
echo success
exit 0
You can change the echo commands or pipe them to any mail sender, so the output is sent to wherever you want instead of being echoed locally which would do you no good.
This is one piece. The other piece is the trigger. If your system uses udev, you should check how to write udev rules, this can trigger events when a device is added or removed.
http://www.reactivated.net/writing_udev_rules.html
A simple set of rules to launch an script when you attach a disk (or pendrive or whatever, it really doesn't matter) would look like this:
Code:
KERNEL=="sd[b-z]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", NAME="%k", SYMLINK+="kingston", GROUP="usb", MODE="0775" OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", SYMLINK+="kingston%n", GROUP="usb", MODE="0775" NAME="%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/bin/mkdir -p /mnt/kingston%n"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/bin/mount -t auto -o umask=000,users,rw,noauto,nodev,noatime /dev/%k /mnt/kingston%n", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/path/to/yourscript"
The firsts lines will name and set the right permissions for the device nodes when a device with the given properties is detected. The third one will create the mount points, the 4th will mount it, the last one will run your script. You could as well simplify the script and write everything as udev rules directly as well.
This is just an example, you will need to read the link above to write correct rules for your device, since the defining characteristics for each device will be different. This is just a general idea.
|
|
|
|
11-02-2009, 05:37 PM
|
#9
|
|
LQ Newbie
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17
Original Poster
Rep:
|
Thank you very much for the detailed samples. Visual stimulation far exceeds all the chatter in the world.
I will test this out.
Thank you.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:10 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|