Sponsored Content
Top Forums Shell Programming and Scripting Calling bash script works when called manually but not via Cron? Post 302939264 by wyclef on Tuesday 24th of March 2015 02:03:18 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 10:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy