Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I login to linux using my Id eg "A". then i'll sudo to the user 'B' without entering the password, after that i'll copy some files from X folder to Y Folder using the SUdo user 'B'.

I am trying to do this activity with a shell script to put it in the cronjob... so far no luck.

Any help on this will be really appreicate...

this is what i am trying...

#!/bin/sh
sudo /usr/local/sbin/deploy
cp -r /tmp/test /tmp/deploy
share|improve this question
    
So what problems are you getting? Does user B have write access to the directory /tmp/deploy? –  intgr Nov 16 '09 at 13:53
2  
Wrong site. This should be on superuser.com . –  Seth Johnson Nov 16 '09 at 13:54
    
Copy works fine... it copies as the User "A" not "B". [pota713@lnx33 /tmp]$ ./cp1.sh --- Switching users --- User Name : deploy UID : 498 Home Dir : /opt/mover Erase set to backspace. <lnx33:~>$ exit [pota713@lnx33 /tmp]$ cd deploy [pota713@lnx33 deploy]$ ls test [pota713@lnx33 deploy]$ ls -ltr total 4 drwxr-xr-x 3 pota713 acn 4096 Nov 16 07:59 test Regards, Ramesh.T –  Ramesh.T Nov 16 '09 at 14:01
add comment

migrated from stackoverflow.com Nov 16 '09 at 16:14

This question came from our site for professional and enthusiast programmers.

3 Answers

sudo only runs the command on the commandline, then returns you back to the invoker's account. if you want to run multiple commands as another user, it would be better to create a script and run that script via sudo.

share|improve this answer
add comment

You must run the cp with sudo as well. Otherwise, deploy creates the files with owner B and then, the cp can't read them (since the shell returns you to the original user when the sudo in your script completes).

Alternatively, run chmod with sudo to make the files readable for A and then make a copy.

share|improve this answer
add comment

sudo is an interactive command. Using it in cron jobs does not make sense. Either you create a super user script to copy all the files and then change the ownership, or run the script by the required user itself.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.