Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

sudo /usr/local/sbin/deploy
cp -r /tmp/test /tmp/deploy
share|improve this question

migrated from Nov 16 '09 at 16:14

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

So what problems are you getting? Does user B have write access to the directory /tmp/deploy? – intgr Nov 16 '09 at 13:53
Wrong site. This should be on . – Seth Johnson Nov 16 '09 at 13:54
Copy works fine... it copies as the User "A" not "B". [pota713@lnx33 /tmp]$ ./ --- 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

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

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

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

You must log in to answer this question.