LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2008, 05:34 AM   #1
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Running functions in bash script as another user with su


I'm writing a backup script which copies a series of directories over to an NFS server using a cron job run from root. The NFS server has the root_squash option defined in its /etc/exports file, so the script is essentially run as nobody, and thus doesn't have the necessary permissions to write where I need it to on the server.

The script comprises mainly of a series of functions which get called at the end to perform the backup (each function is for a different directory). I thought to run the functions as the user who owns the directories on the nfs server would essentially bypass the root_squash option, allowing the functions to run. So, a simple example is:
Code:
#!/bin/bash

backup_dir1(){
   rsync -av /home/user1 /media/nfsmount/backup
}

su - user1 -c backup_dir1

exit 0
Since the script's running from a root cron job, it doesn't ask for the user's password, but it gives this error:
Code:
zsh:1: command not found: backup_dir1
(user1's shell as defined in /etc/passwd in this example is /bin/zsh). So it seems when the su - command is initiated, it isn't aware of the functions defined in the script, which makes sense.

Is there a way I can pass the functions from the script to the su command such that it'll know what backup_dir1 is?
 
Old 12-13-2008, 06:46 AM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 57
Make backup_dir1 an executable script in /usr/local/bin (or any such directory normally used for general executables) like this:

echo "rsync -av /home/user1 /media/nfsmount/backup" > /usr/local/bin/backup_dirl; chmod +x /usr/local/bin/backup_dirl

and run /usr/local/bin/backup_dirl from cron

Dave
 
Old 12-13-2008, 07:04 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
If the login shell is not strictly necessary for user1, omit the - from the su command, so that you can export the function and make it available to the user.
Code:
#!/bin/bash

backup_dir1(){
   rsync -av /home/user1 /media/nfsmount/backup
}

export -f backup_dir1

su user1 -c backup_dir1

exit 0
 
Old 12-13-2008, 08:28 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Original Poster
Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by colucix View Post
If the login shell is not strictly necessary for user1, omit the - from the su command, so that you can export the function and make it available to the user.
Code:
#!/bin/bash

backup_dir1(){
   rsync -av /home/user1 /media/nfsmount/backup
}

export -f backup_dir1

su user1 -c backup_dir1

exit 0
Hi colucix

I tried what you suggested, but I still get the same error. The su command is trying to launch it as a zsh shell rather than a bash shell for user1. Weird.

Thus, in order to force it to use bash I did:
Code:
#!/bin/bash

backup_dir1(){
   rsync -av /home/user1 /media/nfsmount/backup
}

export -f backup_dir1

su user1 -c "bash -c backup_dir1"

exit 0
This seems to work fine, although it seems a little heavy handed!

Thanks for the pointer
 
Old 12-13-2008, 08:39 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Hi pwc, you're welcome! Indeed it is heavy handed also the usage of a function for a single command without dynamic arguments, but I guess this is a simplification of a more complex scenario.
 
Old 12-13-2008, 09:19 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Original Poster
Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by colucix View Post
Hi pwc, you're welcome! Indeed it is heavy handed also the usage of a function for a single command without dynamic arguments, but I guess this is a simplification of a more complex scenario.
Yes, this is a simplification, although I also have a dozen or so variables I have to export to get it to work in the real script. However, it's working now, so that's the main thing.

Now to fix why the transfers to the NFS are so slow! I think it's the wireless on the server that's the problem there, so back to a wired connection it is
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
super user privileges check for a normal user in bash script freeindy Programming 2 08-01-2008 06:08 AM
Functions in bash script som_kurian Programming 3 01-01-2008 09:12 AM
running functions on the same time (bash programming) sharapchi Programming 1 11-12-2006 11:55 AM
Order of functions in a bash script essdeeay Programming 1 05-02-2006 10:52 PM
running su from a bash script caminoix Programming 7 12-28-2005 03:41 PM


All times are GMT -5. The time now is 11:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration