CentOS 5 - problem running script via cron

we have a script to archive some data using yesterdays date to name the created files. We're using the command below in cron but it doesn't work. It does work from the command line. Is our syntax correct? Any help would be appriciated.

archive.sh date -d yesterday +\%Y\%m\%d
fgallagherAsked:
Who is Participating?
 
Kerem ERSOYConnect With a Mentor PresidentCommented:
I've tested it for you if you want to run the script with a parameter then the cron entry should look like this.:

0 0 * * * root /root/scripts/archive `/bin/date -d yesterday \+\%Y\%m\%d`

Here I am assuming that you want to run this at 0:00 every day. As you see it will create an output. Since the session it runs in isn't interactive it will cause an email to root is created everytime the script runs.

If you want to avoid this you can redirect the output to null such as:

0 0 * * * root /root/scripts/archive `/bin/date -d yesterday \+\%Y\%m\%d` >/dev/null  2>&1

Please note that to send the parameter you need to escape the '+' too like '%'. The contents of your script did not think that it  will  be used as na automatic input and did not specify it!!!

if you want to convert the script  so that it won't require a parameter please follow my posting  #24359716

In RedHAT you can create this as a file under /etc/cron.d with a name such as archive and it will be run automoatically when the set time arrives.

Cheers,
K.
 
0
 
Kerem ERSOYPresidentCommented:
Hi,

Why don't you try to put the date command into your script ? It should look like:
.
.
BACKDT=$(date -d yesterday +\%Y\%m\%d)

echo $BACKDT
.
0
 
Kerem ERSOYPresidentCommented:
I guess the problem is the environment in which the script runs does not have the path for date included in the search path list. Try to call date function with full path like :

archive.sh /bin/date -d yesterday +\%Y\%m\%d

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Kerem ERSOYPresidentCommented:
BTW how can you pass the output to the string ?? is it like

date / -d yesterday +\%Y\%m\%d | archive.sh ?
0
 
fgallagherAuthor Commented:
We run

/path_to_script/archive.sh /bin/date -d yesterday +\%Y\%m\%d

0
 
Kerem ERSOYPresidentCommented:
The thing is this way the server would have 4 paremters. Does it assemble then todether and executing later ??

I am trying to understand what does it do and why does it do like that ?

Why doesn't it work say like:

/path_to_script/archive.sh  $(/bin/date -d yesterday +\%Y\%m\%d)

So that it will have only the result as a text input ??

So did running the script as /bin/date in cron solved the problem ??
0
 
Kerem ERSOYPresidentCommented:
Becasue if you run the script that way the parameters wiil be :
$1 = date
$2 = -d
$3 = yesterday
$4 = +%Y%m%d

if all you want is to get the yesterdays date then to get it you should have a line like that:

BCKDT=$($1 $2 $3 $4)

Why do you need to do that ??
0
 
Kerem ERSOYPresidentCommented:
If I were you I'd writen the script like that:

/path_to_Script/scrname yesterday

so that it will calculate the date like that:

case  "$1" in
    'yesterday')
    'today') BCKDT=$(date -d $1 +%Y%m%d)
                ;;
    *) echo Usage: $(basename $0) [yesterday] | [today]
                exit 1
               ;;
esac
0
 
fgallagherAuthor Commented:
we won't know unitl cron kicks off tonight @ 12:02... there is no data from yesterday. It was Sunday. I've attached an basic copy of the script for your review.
0
 
TintinCommented:
Surely you don't literally run

archive.sh date -d yesterday +\%Y\%m\%d

from the command line

I would have thought you'd do

archive.sh `date -d yesterday +\%Y\%m\%d`

or

archive.sh $(date -d yesterday +\%Y\%m\%d)

otherwise archive.sh, would need to something like

date=$(eval $*)

which would be a very, very strange way of specifying the date.
0
 
fgallagherAuthor Commented:
to be honest I took over this from another admin and I don't understand it... I'm all ears if you want to share a better way to get to our end goal.
0
 
Kerem ERSOYPresidentCommented:
#!/bin/bash

BCKDT=$(date -d yesterday +\%Y\%m\%d)


case  "$1" in
             yesterday ) ;;
             today ) ;;
              *) echo Usage: $(basename $0) '[yesterday] | [today]'
                 exit 1
                 ;;
esac

BCKDT=$(date -d $1 +%Y%m%d)


echo  $BCKDT
0
 
Kerem ERSOYPresidentCommented:
TinTin:
 
It must be assembling the parts and later executing it look at ID: 24359349 it does not make sense either it runs or not
0
 
fgallagherAuthor Commented:
i just realized my script didn't upload. here it is.
archive.txt
0
 
Kerem ERSOYPresidentCommented:
Yeah it expects the output of date not the command so your cron entry should read like that:

/PathTo_Script/archive.sh $(/bin/date -d yesterday +\%Y\%m\%d')

This will work.
0
 
Kerem ERSOYPresidentCommented:
Furthermore you don't need to wait till it works. copy the script to another script called say
cp /path/archive.sh /path/archive_test.sh

edit the archive_test.sh nd locate these lines:

   #to tar the file
   tar cfzv $SRC/temp/$DATE.bu.tgz $SRC/$DATE*our_md

Replace them with this one:

   #to tar the file
  echo "test"  > $SRC/temp/$DATE.bu.tgz $SRC/$DATE*our_md

So that script will create a small text file instead.
Now edit your cron file to add this:

*/5 * * * *  root /Path_To_Script/archive_test.sh $(/bin/date -d yesterday +%Y%m%d')

Save & exit

Now the test will fire up every 5 minutes and create the simple test file....

0
 
Kerem ERSOYPresidentCommented:
As far as I understand  this script was written for manual backup and now you are trying to convert it to run automatically ?
0
 
Kerem ERSOYPresidentCommented:
Ooops my bad please modify the line :

 #to tar the file
  echo "test"  > $SRC/temp/$DATE.bu.tgz $SRC/$DATE*our_md

with

 #to tar the file
  echo "test"  > $SRC/temp/$DATE.bu.tgz

in the test script. The script will send the output to the root asan email. Check the status trough root's email account.

0
 
fgallagherAuthor Commented:
As far as I understand  this script was written for manual backup and now you are trying to convert it to run automatically ?

Yes, this is correct.
0
 
Kerem ERSOYPresidentCommented:
ok I'll send you a modified version. As it will alwys get the date of yesterday right ??

Then modify this line:

DATE=$1

with this:

DATE=$(date -d yesterday +%Y%m%d')

So that it will run without any parameters...
0
 
fgallagherAuthor Commented:
we're testing this now. THANK YOU FOR ALL THE HELP! You're really fantastic for doing all that work to help us out!
0
 
Kerem ERSOYPresidentCommented:
Just take your time. Yr welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.