?
Solved

CentOS 5 - problem running script via cron

Posted on 2009-05-11
22
Medium Priority
?
775 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:fgallagher
  • 15
  • 6
22 Comments
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24358797
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24358812
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359182
BTW how can you pass the output to the string ?? is it like

date / -d yesterday +\%Y\%m\%d | archive.sh ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:fgallagher
ID: 24359260
We run

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

0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359326
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359349
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359384
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
 

Author Comment

by:fgallagher
ID: 24359398
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
 
LVL 48

Expert Comment

by:Tintin
ID: 24359434
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
 

Author Comment

by:fgallagher
ID: 24359483
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359488
#!/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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359498
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
 

Author Comment

by:fgallagher
ID: 24359522
i just realized my script didn't upload. here it is.
archive.txt
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359557
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359606
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359614
As far as I understand  this script was written for manual backup and now you are trying to convert it to run automatically ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359652
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
 

Author Comment

by:fgallagher
ID: 24359696
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24359716
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
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 2000 total points
ID: 24359868
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
 

Author Comment

by:fgallagher
ID: 24364828
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24364909
Just take your time. Yr welcome.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question