I'm using:

# cat /etc/redhat-release
CentOS release 6.4 (Final)

Have an script in user's crontab:

$ crontab -l
0 7 * * * /home/teamcity/scripts/TC_backup.sh

If I run this script under this very user - it's create needed archive:

$ /home/teamcity/scripts/TC_backup.sh

And from local-scripts-log:

$ cat /var/log/teamcity_backup.log | tail -n 20
Exporting TeamCity data directory: Exporting build logs
Exporting TeamCity data directory: Exporting personal changes
Finalizing export...
Export has completed successfully.
Backup file created: /home/teamcity/backups/TeamCity_20140117_172034.zip
Backup finished

   Free disc space: 147G;
   Used disc space: 125M.

   Last archives available:

55M Jan 17 17:21 TeamCity_20140117_172034.zip
55M Jan 17 17:14 TeamCity_20140117_171318.zip
16M Dec 17 18:43 TeamCity_20131217_184333.zip

   Backup finished at 2014-01-17--17-21

And have files:

$ ls -l /home/teamcity/backups/
total 127656
-rw-rw-r-- 1 teamcity teamcity 16777213 Dec 17 18:43 TeamCity_20131217_184333.zip
-rw-rw-r-- 1 teamcity teamcity 56914703 Jan 17 17:14 TeamCity_20140117_171318.zip
-rw-rw-r-- 1 teamcity teamcity 57022673 Jan 17 17:21 TeamCity_20140117_172034.zip

But - I haven't any *.zip from other days.

In cron-log I see that script started without errors:

Jan 12 07:00:01 lms-teamcity CROND[11878]: (teamcity) CMD (/home/teamcity/scripts/TC_backup.sh)

Contents of the script:

backup () {
    /home/teamcity/TeamCity/bin/maintainDB.sh backup --all -M -F $BDIR/$ARHNAME >> $LOGFILE

What can be the cause?

UPD: 2> errorlog.file doesn't give result:

$ crontab -l
0 7 * * * /home/teamcity/scripts/TC_backup.sh 2> /var/log/teamcity_backup_error.log

$ file /var/log/teamcity_backup_error.log
/var/log/teamcity_backup_error.log: empty

In cron-log also no any errors:

Jan 20 09:00:01 lms-teamcity CROND[24816]: (setevoy) CMD (/home/setevoy/scripts    /db_con_test-4.sh)
Jan 20 09:00:01 lms-teamcity CROND[24818]: (teamcity) CMD (/home/teamcity/scripts/TC_backup.sh 2> /var/log/teamcity_backup_error.log)
Jan 20 09:00:01 lms-teamcity CROND[24817]: (root) CMD (/usr/lib64/sa/sa1 1 1)
  • 1
    Capture the error output of your cron to see what's going on: 0 7 * * * /home/teamcity/scripts/TC_backup.sh 2> /tmp/back.log. – terdon Jan 17 '14 at 15:35
  • 90% of the time these issues are caused because cron runs with a very minimal environment. You don't get the same environment as an interactive user session (under which you're manually running the script). So, make sure your script has the right environment setup. – EightBitTony Jan 17 '14 at 15:35
  • @EightBitTony Donn't sure quite understood you... Script very simple and it's just run other one from itself: BDIR="/home/teamcity/backups/" LOGFILE="/var/log/teamcity_backup.log" ARHNAME="TeamCity" backup () { /home/teamcity/TeamCity/bin/maintainDB.sh backup --all -M -F $BDIR/$ARHNAME >> $LOGFILE } backup; Neither TC_backup.sh or maintainDB.sh doesn't use any user/system variables... – setevoy Jan 17 '14 at 15:46
  • 3
    If it works when run from the command line, and not from cron, 90% of the time it is a problem with the user environment, because that is the key difference between running under an interactive shell and running under cron. You can diagnose this stuff by including more logging, checking stderr, and also running env at the start of the script and capturing the output. Even the $PATH is likely to be different. – EightBitTony Jan 17 '14 at 15:56
  • Thanks, @EightBitTony - I added 2> output as terdon suggested - hope will have additional info. And added env >> file in to backup script beginning. – setevoy Jan 17 '14 at 16:04

You're most likely getting an error that you can't see because you aren't capturing STDERR anywhere. You're only capturing STDOUT. Usually cron will e-mail the user with any output (whether STDOUT or STDERR). Who is receiving mail for the teamcity user on this host?

In TC_backup.sh capture STDERR:

/home/teamcity/TeamCity/bin/maintainDB.sh backup --all -M -F $BDIR/$ARHNAME >>$LOGFILE 2>>$ERRORLOG

Then check your error log.

| improve this answer | |

Сause was in JAVA_HOME variable, as it needed by maintainDB.sh script.

So, getting info from script running from cron:

getenv () {
env >> $BDIR/envlog.log

And than checking file to variable:

$ cat envlog.log | grep JAVA

Show nothing.

After adding:

export JAVA_HOME="/usr/java/jdk1.6.0_45/"
export JAVA_OPTS="-Xmx1536m -XX:MaxPermSize=768m"
export JRE_HOME="/usr/java/jdk1.6.0_45/jre"

In to this very backup script TC_backup.sh - all work good.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.