Author Topic: run a script as root on command  (Read 756 times)

Offline alphaace

  • Sr. Member
  • ****
  • Posts: 310
run a script as root on command
« on: February 20, 2012, 05:28:04 PM »
Hi,

I have a script that I want to run on command (to eject the kindle via eject /dev/sdb1). However, to run that command I need to be root. I've made a script to run the appropriate command but how do I get that script to ask me for my root password so that it can run correctly?

The reason I'm doing this, is that if you simple eject the kindle, it still shows that it's connected via usb and you can't read it. However after issuing that command, all is well. Normally, I would just do it via the terminal, but I'm setting it up for my fiance so I want it to be just point and click.

Thanks!

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: run a script as root on command
« Reply #1 on: February 20, 2012, 05:52:35 PM »
you can su a command only, using a line like the following inside your script:
Code: [Select]
su -c "eject /dev/sdb1"

or you can execute the whole script as root:
Code: [Select]
chmod 700 script.sh
su -c "./script.sh"

or you can use the GUI based kdesu -c or gksu instead of "su -c", check man page and/or --help option about details .

AS