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! |
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. |
Look into "sudo".
|
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! |
I'll look at the man page for sudo for script examples also Mr.B.
|
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:
|
Just mount the drive and rsync then umount and email success.
|
Well, the basics of your script could be something like:
Code:
#!/bin/bash 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" 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. |
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. |
All times are GMT -5. The time now is 03:58 PM. |