2

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
3
  • So what problems are you getting? Does user B have write access to the directory /tmp/deploy?
    – intgr
    Nov 16, 2009 at 13:53
  • 2
    Wrong site. This should be on superuser.com . Nov 16, 2009 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, 2009 at 14:01

4 Answers 4

1

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.

0

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.

0

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.

0

Try this: for example if your username is "james", add the following entry to the /etc/sudoers or into a separate /etc/sudoers.d/ file. After this, you can copy files from source to target without entering password.

# visudo 
james  ALL=(ALL) NOPASSWD: /bin/cp

You must log in to answer this question.