-   Linux - Newbie (
-   -   How to run a shell script as root (when user is not root) (

taylorkh 09-08-2008 10:34 AM

How to run a shell script as root (when user is not root)
Ubuntu 8.04 - Gnome - here is the the issue:

I wrote a shell script to copy the data under /home to a USB drive for backup. I intended to schedule the job to run every day at 1 AM. However, while the PC will go into standby as I want it to, it will not wake up to run the script. I have another PC of the same model and I can wake it up by using the auto power on in BIOS - but not this one(?)

The script will copy data for 2 user accounts, mine and the wife's thus it must be run with root permissions. Her account is not a sudoer and it should not be. Her account is generally logged onto the PC and she would be the one to execute the script manually until I figure out how to resolve the power on issue. What I need to do is create a launcher which will run the script with root permissions without her having to logon with my credentials in order to sudo the script.

I believe I have come across a way to make a program or script run as root without having to be root or know the root password. How to do it escapes me at the moment.

Thanks for any advice.


b0uncer 09-08-2008 10:37 AM

Why not add your wife's user account to sudoers and configure it so that only your script can be run (and then prevent her from modifying the script)? For the time being.

taylorkh 09-08-2008 11:36 AM

That is certainly a possibility. However, she is not happy about having to push the power button and type in her password to get back in after the PC goes to standby (as opposed to just bumping the mouse in XP - until service pack 3 destroyed my finely crafted install). She uses the PC for Web and email and that is about as far as her interest goes.

Still, this is more of an intellectual challenge for me. I recall that early versions of Red Hat required root permission to connect to the Internet with a modem or to burn a CD with XCDRoast. There was a way to fix those although I do not remember how. I guess I need to see if I can find any of my old notes.


darthaxul 09-09-2008 01:32 AM

i never heard of that
I dont know anything like what u tring to do but in order to write data to disk just set permission on usb drive to allow her to write to the disk.
you dont need to run script as root if its just copy /home paste /usb.

taylorkh 09-09-2008 09:52 AM

Thanks darthaxul. I just reviewed permissions on the various directories and files under /home and see that they are 755 and 644. Umask is 022. This is my first experience with setting up a Linux box with multiple accounts where another user will actually logon and use the PC (other than for testing or to share a printer or folder with Samba). I guess I was expecting a default Umask of 066 where each user's files would be private.

Regardless, my script checks to see if the USB drive is mounted, if not it mounts it, does the cp then umounts the USB drive. At a minimum the account running the script must be able to execute mount and umount. I have tried granting these permissions in /etc/sudoers with NOPASSWD: however, I must have a syntax error as I do not get the result I expect. I will keep working on it.


CJS 09-09-2008 01:26 PM

One thing to keep in mind with the sudoers file is that the order of the rules matters. So to make sure the sudoers line that you add isn't overridden by some other rule, be sure to add your rule to the end of the file. Here is an example of how to use the NOPASSWD syntax in case you haven't gotten it working all ready:

john ALL = NOPASSWD: /path/to/cmd1, /path/to/cmd2, path/to/cmd3
Let me know if you get it working OK.

taylorkh 09-10-2008 10:55 AM

As they say in the movies "belay my last" - I have explored the permissions under /home a little more and am back to my original position that my script should be run as root. For example:

ken@taylor9:~$ sudo ls -l /home/ken/.config
total 16
drwx------ 3 ken ken 4096 2008-08-29 12:52 compiz
drwx------ 2 ken ken 4096 2008-08-29 13:23 tracker
-rw------- 1 ken ken 630 2008-08-29 12:24 user-dirs.dirs
-rw-r--r-- 1 ken ken 5 2008-08-29 12:24 user-dirs.locale

ken@taylor9:~$ sudo ls -l /home/patsy/.config
total 20
drwx------ 3 patsy patsy 4096 2008-08-29 13:12 compiz
drwx------ 2 patsy patsy 4096 2008-08-29 18:21 gtk-2.0
drwx------ 2 patsy patsy 4096 2008-08-29 13:12 tracker
-rw------- 1 patsy patsy 630 2008-08-29 13:12 user-dirs.dirs
-rw-r--r-- 1 patsy patsy 5 2008-08-29 13:12 user-dirs.locale
It would appear that only the account owner or root can copy all files under their personal /home/user directory. Only root can copy all files for ALL users so I guess I am back to my original question of how to run a script as root when the user is not root does not have the root password.


darthaxul 09-10-2008 04:35 PM

another option
maybe you can run it as a cron job. just make sure the job runs as root. that way the user doesnt even have to run anything, its automatic.

taylorkh 09-10-2008 05:20 PM

Back at the beginning of the thread I indicated that I had planned to run the job as with the scheduler - and it will work if I schedule it with root credentials. The problem is that this particular PC will not wake from standby to run the job.

Windoze will let me wake the PC from within the scheduler. I do not know if/how that can be done with cron.


darthaxul 09-12-2008 06:40 PM

so u want it to go into standby, but have the ability to wakeup and run that script/job?

taylorkh 09-12-2008 07:05 PM

That is it exactly.

The PCs are Dell Dimension 4600s. The "newer" one with a 2.8 GHz processor seems to wake up from standby when set in BIOS to "automatic power on" at appointed time (about a minute before the cron job) just fine. The older PC with a 2.66 GHz processor will not wake from standby using the same method. I have upgraded the bios on both to the latest A12 from Dell.

I think I will try putting the offending PC in hibernate and see if BIOS can awaken it.

All times are GMT -5. The time now is 12:36 AM.