openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting » Scp Script With Users Or Permissions Issues!

Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 16-Apr-2008, 15:59
Marco Sanchez
Guest
 
Posts: n/a
Arrow

Hi my name is Marco Sanchez and I am facing a problem with this script that I don't know the solution for, probably is because of a lack of experience scripting or probably a misunderstanding of how something is supposed to work, but anyway here is the explanation of the problem and below you'll find the scripts related to this problem.

Problem
It's a backing up script, it actually works if I run it manually but if it runs through a cronjob I get an error, the script attempts to copy some files from one server to another through SCP in a password-less way.

Ive authenticated both server's keys between each other for the admin user, so if I am admin I can SCP in that password-less way no problem, my script actually switches users (from root to admin) and does it correctly when I run the script manually as root since I need to perform some things as root before doing the SCP as admin but the cronjob won't do it hehe

I'll show you the scripts (Some of my lines are very long so the system wrapped them, sorry about that)


Script 1
================================================== ============================
# Set the variable "DATE" & format the date cmd output to look pretty
#
DATE=$(date +%b%d)

# create log file with today's date.
# after each task is performed the result will be written there
touch /home2/tempdir/www4towww7_log_$DATE
chown admin:admin /home2/tempdir/www4towww7_log_$DATE

#TASKS PERFORMED IN WWW4 AS ROOT

#Moving of files to the admin temp dir echo "Atempting to move files from orig dir at www4 to temp dir at www4"
mv /home2/backups/tars/vhosts/1/*.* /home2/tempdir/ && echo "++Moving of files from original dir at www4 to temp dir at www4 successful" >>/home2/tempdir/www4towww7_log_$DATE || echo "**Moving of files from original dir at www4 to temp dir at www4 not successful" >>/home2/tempdir/www4towww7_log_$DATE


#Change of ownership of files at the tempdir echo "atempting to change ownership of files at tempdir on www4"
chown admin:admin/home2/tempdir/*.* && echo "++Change of ownership of files at tempdir on www4 successful" >>/home2/tempdir/www4towww7_log_$DATE || echo "**Change of ownership of files at tempdir on www4 not successful" >>/home2/tempdir/www4towww7_log_$DATE


#TASKS PERFORMED IN WWW4 AS ADMIN
echo "Atempting to change user to admin"
su admin -c ./scp_www4_to_www7.sh


if grep "++SCP of files from tempdir at www4 to tempdir at www7 was successful" /home2/tempdir/www4towww7_log_$DATE
then
echo "The Files were copied succesfully to www7 therefore we proceed to delete them from www4 tempdir"
echo "Atempting to remove the .gz files from tempdir"
rm -f /home2/tempdir/*.gz && echo "++Removal of .gz files was succesful" >>/home2/tempdir/www4towww7_log_$DATE || echo "**Removal of .gz files from tempdir was not successful" >>/home2/tempdir/www4towww7_log_$DATE

else
echo "The Files were not copied succesfully to www7 therefore we won't delete them from www4 tempdir"
fi

echo "Process finished, verify the /home2/tempdir/www4towww7_log_$DATE file for messages"

================================================== ============================




Script 2 (scp_www4_to_www7.sh)
================================================== ============================
# Set the variable "DATE" & format the date cmd output to look pretty
#
DATE=$(date +%b%d)

echo "Atempting to SCP files from tempdir at www4 to tempdir at www7"
scp /home2/tempdir/*.gz www7.eonconnect.com:/home2/tempdir/ && echo "++SCP of files from tempdir at www4 to tempdir at www7 was successful" >>/home2/tempdir/www4towww7_log_$DATE || echo "**SCP of files from tempdir at www4 to tempdir at www7 not successful" >>/home2/tempdir/www4towww7_log_$DATE
echo "Returning to the original script"

================================================== ============================

By the way, The cronjob has no problems at all but I get the following error on my root's e-mail:

The first 3 lines are ok, but the when atempting to change user the problem starts and basically stops the process
================================================== ============================
Atempting to move files from orig dir at www4 to temp dir at www4
atempting to change ownership of files at tempdir on www4
++Change of ownership of files at tempdir on www4 successful
Atempting to change user to admin
bash: line 1: ./scp_www4_to_www7.sh: Permission denied
The Files were not copied succesfully to www7 therefore we won't delete them from www4 tempdir

Process finished, verify the /home2/tempdir/www4towww7_log_Apr14 file for messages
================================================== ============================


Of course I understand is a problem related to the permissions or the user but cant figure out what it is, any help is really appreciated.

Marco Sanchez
  #2 (permalink)  
Old 23-Apr-2008, 18:38
rev_1318
Guest
 
Posts: n/a
Default

When running scripts/commands from crontab you have to take in account that you'll get a different (and very limited set-up) environment then when running the same command interactive. Most problem stem from:
  • your working directory under cron defauults to / (root-dir)
  • your PATH is limited to the basic direcories (/usr/bin, /bin)
  • most environment variables you've set up aren't available
So always change directory explicitly, set your PATH and set your variables as needed.

An extra remark to your script: 'su <user>' behaves differently as 'su - <user>'!

HTH,
Paul
  #3 (permalink)  
Old 24-Apr-2008, 12:09
Marco Sanchez
Guest
 
Posts: n/a
Default

Quote:
When running scripts/commands from crontab you have to take in account that you'll get a different (and very limited set-up) environment then when running the same command interactive. Most problem stem from:
  • your working directory under cron defauults to / (root-dir)
  • your PATH is limited to the basic direcories (/usr/bin, /bin)
  • most environment variables you've set up aren't available
So always change directory explicitly, set your PATH and set your variables as needed.

An extra remark to your script: 'su <user>' behaves differently as 'su - <user>'!

HTH,
Paul
[/b]

HI and thanks for replying, I'll check the script based on your suggestions and lets see if it works !!

Thanks!!
 

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2