LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-08-2011, 11:41 AM   #1
jonathansfl
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu 10.04
Posts: 11

Rep: Reputation: 0
.sh script won't tar when run from cron


Greetings
I'm having a script / cron problem. i'm trying to run a script that backs up a postgresql database using a pgdump command.
I've included a simplified version of my script here.
The pgdump works fine, and outputs an SQL file.
The problem is with the tar command. it fails to correctly tar, and then compress, the .sql file when the .sh script is run from cron. but when run manually from command line (with sudo) it works well. Note ls -l printout, where the top lines were generated by cron (and tar & tar.gz are minisule), whereas in the second group the tar and tar.gz are much larger (and correct).
Note that the .sh script is owned by root, who is also the user in the cron entry. I tried using postgres as the user for all those, but i think i then had password problems. should i be using some kind of "credential"?
THANK YOU!!!

---------------------------------------------------------------

-rwxr-xr-x 1 root root 400 2011-05-08 12:24 testbackup.sh
-rw-r--r-- 1 root root 3945771 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.sql
-rw-r--r-- 1 root root 10240 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.tar
-rw-r--r-- 1 root root 45 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.tar.gz

postgres@dev-postgres-1:~/backups$ sudo ./testbackup.sh
-rw-r--r-- 1 root root 3945771 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.sql
-rw-r--r-- 1 root root 3952640 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.tar
-rw-r--r-- 1 root root 747801 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.tar.gz

---------------------------------------------------------------

Here is the script and cron entry (cron in test mode to produce every 2 minutes):
---------------------------------------------------------------
Code:
#!/bin/sh
MYDATABASENAME="beta_cms_main"
MYDATABASENAME_DATETIME="TEST_"_"$MYDATABASENAME"_$(date +"%Y%m%d%H%M%S")
pg_dump -U postgres -F c -f /home/postgres/backups/$MYDATABASENAME_DATETIME.sql $MYDATABASENAME 
tar -cf /home/postgres/backups/$MYDATABASENAME_DATETIME.tar $MYDATABASENAME_DATETIME.sql
tar -czf /home/postgres/backups/$MYDATABASENAME_DATETIME.tar.gz $MYDATABASENAME_DATETIME.tar
---------------------------------------------------------------

*/2 * * * * root /home/postgres/backups/testbackup.sh >> log_BST_postgres_backup_service_cron.log
 
Old 05-08-2011, 11:47 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
If it works correctly with sudo, why not add sudo to the cronjob?

*/2 * * * * root sudo /home/postgres/backups/testbackup.sh >> log_BST_postgres_backup_service_cron.log

You many need to comment the "Defaults requiretty" line in /etc/sudoers.

After thinking about this, it might not work. Let me know if you try it.

Last edited by carltm; 05-08-2011 at 11:48 AM. Reason: update after second thought
 
  


Reply



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
Tar Won't Properly Backup FS From Cron wafflesausage Linux - Software 3 04-09-2011 06:16 AM
Backup script won't run in cron job. Why? Micro420 Linux - General 19 10-31-2007 08:26 PM
Cron won't open console to run script rollo Linux - General 5 11-10-2006 01:56 PM
cron won't run commands? mma8x Linux - Software 4 04-05-2006 12:25 PM
Script creats temporary files... Cron won't run it spiffytech Linux - Software 5 01-11-2006 10:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration