Calling bash script works when called manually but not via Cron?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling bash script works when called manually but not via Cron?
# 1  
Old 03-20-2015
Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin.

Basically the path structure is

/home/username/domainname.com/_mybash.sh

where I'm trying to backup domainname.com

If I ssh in and go cd domainname.com and then sh _mybash.sh from the command line. The script runs fine. If I set up a cron to /home/username/domainname.com/_mybash.sh the script runs and attempts to backup everything under username... I don't understand.

This is the script:
Code:
#!/bin/bash

#/************************ EDIT VARIABLES ************************/
projectName=mydomain
backupDir=/home/username/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d-%H%M")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

# Color Output
green='\033[1;32m'
nc='\033[0m' # No Color

# Initial setup
TODAY=$(date)
echo "----------------------------------------------------
$(tput bold)Date:$(tput sgr0) $TODAY
$(tput bold)Host:$(tput sgr0) mydomain.com automated backup"

echo "----------------------------------------------------"
echo "Dumping MySQL..."
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
echo "Done!"

echo "----------------------------------------------------"
echo "Archiving Files..."
tar -zcf $fileName.tar.gz * .htaccess
echo "Done!"
echo "----------------------------------------------------"
echo "Cleaning..."
rm -f $fileName.sql.gz
echo "Done!"

echo "----------------------------------------------------"
mkdir -p $backupDir;
echo "Moving file to backup dir..."
mv $fileName.tar.gz $backupDir
echo "----------------------------------------------------"
echo "Removing old backups..."
find $backupDir -type f -mtime +30 -exec rm {} \;
echo -e "${green}Backup of $projectName Complete!${nc}"

# 2  
Old 03-20-2015
It's probably not finding the paths of certain things.
What user do you run/cron it as?
Put set -x in the top of it to debug. Did you try placing the cd command at the top of the script.
Don't expect the escape sequences to work for color since you are not at standard input and standard output are not terminas
ls when you cron.
Use full paths to exeecutables.
# 3  
Old 03-23-2015
Usually reason is PATH value. Your login env PATH is different as the cron use.

In the terminal session save the env:
Code:
cd /home/username/domainname.com
env > myenv
# or save only PATH
echo "$PATH" > myenv

Then add to the script:
Code:
#!/bin/bash

.  ./myenv   # set env as you have after login

# printout env - compare cron and login session - debug using
env 

...

And then crontab line. Change directory to where you have script and wp-config.php. Here is ex. which save output to some logfile and stderr also.
Ex.
Code:
0 3 * * * (cd /home/username/domainname.com;./_mybash.sh ) > /home/username/domainname.com/cron.log 2>&1

# 4  
Old 03-23-2015
yes, CD!

Here is the latest, and stripped down version. I removed all the tput and formatting stuff since cron doesn't bother with it. A few questions, what permissions would make the most sense from a security standpoint... 755, or 700? How can I make this more secure? How could I organize the archive where there was a www and database folder that the files were placed in before archival? Since this was originally made to be run manually in command prompt, a lot of the output for cron now is sort of irrelevant... are there relevant things I could add to provide more useful info in my cron email?

Thanks

Code:
#!/bin/bash

cd ~/mysub.example.com

#/************************ EDIT VARIABLES ************************/
projectName=mysub_demo
backupDir=/home/username/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d-%H%M")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

# Initial setup
TODAY=$(date)
echo "----------------------------------------------------
Date: $TODAY
Host: mysub.example.com automated backup"

# Backup DB
echo "----------------------------------------------------"
echo "Dumping MySQL..."
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
echo "Done!"

# Backup files
echo "----------------------------------------------------"
echo "Archiving Files..."
tar -zcf $fileName.tar.gz * .htaccess
echo "Done!"
echo "----------------------------------------------------"
echo "Cleaning..."
rm -f $fileName.sql.gz
echo "Done!"

# Move to backup directory
echo "----------------------------------------------------"
mkdir -p $backupDir;
echo "Moving file to backup dir..."
mv $fileName.tar.gz $backupDir

# Keep last 30 Backups
echo "----------------------------------------------------"
echo "Removing old backups..."
find $backupDir -type f -mtime +30 -exec rm {} ;
echo "Backup of Complete!" 



Last edited by wyclef; 03-23-2015 at 09:28 PM..
# 5  
Old 03-24-2015
The line in your script:
Code:
find $backupDir -type f -mtime +30 -exec rm {} ;

should be changed to:
Code:
find $backupDir -type f -mtime +30 -exec rm {} \;

or preferably:
Code:
find $backupDir -type f -mtime +30 -exec rm {} +

On many systems a find -exec primary must be terminated by a semicolon or by a plus sign. The semicolon in your script is eaten by the shell so find won't see it. Using + instead of \; is more efficient in cases when you have more than one file to remove.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-24-2015
Thanks for pointing that out i thought i had changed that. How could i go about catching errors to be output in the email. Also, is it not necessary to have the .sh extension on my bash/cron script? Here is the updated script without all the output.

Code:
#!/bin/bash

cd ~/mysub.example.com

#/************************ EDIT VARIABLES ************************/
projectName="mysub_demo"
backupDir="/home/username/_backups"
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d-%H%M")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

# Backup DB
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz

# Backup files
tar -zcf $fileName.tar.gz * .htaccess
rm -f $fileName.sql.gz

# Move to backup directory
mkdir -p $backupDir;
mv $fileName.tar.gz $backupDir

# Keep last 30 Backups
find $backupDir -type f -mtime +30 -exec rm {} +

Do I need to be wrapping each step in some kind of if else to do error checking? Something like the following?

Code:
if [ "$?" = "0" ]; then
    #some command
else
    echo "Error. Couldn't do some command!" 1>&2
    exit 1
fi

---------- Post updated at 01:08 PM ---------- Previous update was at 07:48 AM ----------

---------- Post updated at 02:03 PM ---------- Previous update was at 01:08 PM ----------

Sorry for the additional post but this is a work in progress and keeps changing. This is the latest... trying to do a bit of checking and handling here... really dont know what I'm doing though. Could use some advice.

Code:
#!/bin/bash

cd ~/mysub.example.com || exit

#/************************ EDIT VARIABLES ************************/
projectName="mysub_demo"
backupDir="/home/username/_backups"
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d-%H%M")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

# Backup DB
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz

# Backup files
tar -zcf "$fileName.tar.gz" * .htaccess &&
    rm -f "$fileName.sql.gz"

# Move to backup directory
mkdir -p "$backupDir";
mv "$fileName.tar.gz" "$backupDir"

# Keep last 30 Backups
find "$backupDir" -type f -mtime +30 -exec rm {} +


Last edited by wyclef; 03-24-2015 at 03:10 PM..
# 7  
Old 03-26-2015
use full path for starts

I would start (as already pointed out) to use full pathname to all of your binaries.

example. Original:
Code:
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)

Change to:
Code:
host=$(/bin/grep DB_HOST "wp-config.php" | /bin/cut -d "'" -f 4)

Repeat. Also, the host variable above is looking for wp-config.php file (your wordpress configuration file). Your first cd command is:
Code:
cd ~/mysub.example.com

That is NOT an absolute path (it's relative). So cron won't find it.

To find the exact locations of the files, in your CLI just run
Code:
which <binary>

to find out where it is. Such as
Code:
which tar

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script runs manually, but not from cron

Hi, I "borrowed" a script I found online, to start a SAP router application on a Solaris 11 (SPARC) server. The script runs fine when calling it manually, but when I schedule it to run from cron, it doesn't. I don't see any warning or failure messages anywhere, just nothing happens. ... (11 Replies)
Discussion started by: bredman
11 Replies

2. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

3. Shell Programming and Scripting

Why my git command has no output in crontab but works well run this script manually?

cat /home/lyang001/update.sh #!/bin/sh #shopt -s expand_aliases HOME_DIR=/home/lyang001/updates UPDATE_MAIL=${HOME_DIR}/updates.mail rm $UPDATE_MAIL -rf cd $HOME_DIR/wr-kernel git log --no-merges --since="20 day ago" --name-status --pretty=format:"%an %h %s %cd" origin/WRLINUX_5_0_1_HEAD >>... (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Shell script not getting called through cron job but executes fine manually.

Hi, My shell script not getting called through cron job. The same works fine when executed manually. I tried to generate logs to find if the scripts has some errors related to path using following command- trying to execute .sh file every 5 mins: */5 * * * * /home/myfolder/abc.sh... (17 Replies)
Discussion started by: Dejavu20
17 Replies

5. Shell Programming and Scripting

Sendmail works from script, but not when called from Apache

Hi, I am building a web interface to run a series of shell scripts that reside on the web server. The bash script are written such that they can be used independently for the task they are meant for, or the same scripts can be run from this web UI. The scripts are mostly for doing software... (1 Reply)
Discussion started by: MacQAGuy
1 Replies

6. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #sample1.sh #!/bin/ksh . ./sample2.sh echo $fileExist #sample2.sh #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of sample2.sh back to sample1.sh? Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

7. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

8. Shell Programming and Scripting

Script behaves different when run from cron vs. manually

Hey all, Just wanted to get some input on a script I am using to import files into a MySQL database. The process is pretty simple: my main server exports these files and FTPs them. I have a script that FTPs them to the machine running that runs this script. The FTP script runs without issue... (2 Replies)
Discussion started by: billtwild
2 Replies

9. UNIX for Dummies Questions & Answers

Works Manually - not in CRON

I've got a ksh script that works like a charm when I run it manually. When I set it up in a cron, I keep getting this error in my log: syntax error at line 90: `$' unexpected Here's my snippet of code starting at line 90: while ] do sleep 900 done What's the... (5 Replies)
Discussion started by: dstinsman
5 Replies

10. Shell Programming and Scripting

Can run script Manually, but not through Cron?

Hi all, I have a main script (called OracleCleanup) that runs some sql queries. that runs off a wrapper script which contains the sources for the login information and and JOB_HOME (the script is below). When I schedule this job in the cron the log says that it cannot open my list file, which... (4 Replies)
Discussion started by: MadHatter
4 Replies
Login or Register to Ask a Question