[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

CRONJOB dilemma...

Just wondering if I'm missing anything here. There is a script which runs ok when I initiate it on my Linux machine, however, when I set up crontab no data is output to the files. Why would the script give me data when I initiate it manaully versus when I set up a cron job? Here is the script and crontab. Please let me know what may be causing this.

SCRIPT:
#! /bin/sh

YMONTH=`date --date="yesterday" "+%b"`
YDAY=`date --date="yesterday" "+%e"`
fDAY=`date --date="yesterday" "+%d"`

fDaTe=$YMONTH" "$YDAY

sMonth=`date --date="yesterday" "+%m"`
sDay=`date --date="yesterday" "+%d"`
sYear=`date --date="yesterday" "+%y"`

sDate=$sDay"."$sMonth"."$sYear

fEDR=`ls -lt *edr* | grep "$fDaTe" | cut -c 65-66 | head -1`
lEDR=`ls -lt *edr* | grep "$fDaTe" | cut -c 65-66 | tail -1`

fEDR=`expr $fEDR - 1`
lEDR=`expr $lEDR + 1`
c=`expr $lEDR + 1`
sfname="ipn_edr_"

for country in "FRANCE" "GERMANY" "ITALY" "GBR"
do

i=$fEDR
 while [ $i -lt $c ]
 do

  efname=$sfname$i".dat"
  grep -w $country $efname | grep $sDate >>temp$country$fDAY
  i=`expr $i + 1`
 
 done
 
done


grep "RELAY" tempGBR$fDAY >>tempRELAY$fDAY
grep "RELAY" tempITALY$fDAY >>tempITALYr$fDAY
grep "RELAY" tempFRANCE$fDAY >>tempFRANCEr$fDAY

grep "Unexpected Data Value" tempGBR$fDAY >>tempUDV$fDAY
grep "Unexpected Data Value" tempITALY$fDAY >>tempITALYu$fDAY
grep "Unexpected Data Value" tempFRANCE$fDAY >>tempFRANCEu$fDAY

rm tempGBR$fDAY
rm tempITALY$fDAY
rm tempFRANCE$fDAY
________________________________________________________
ENTRY IN CRONTAB:
0 12 * * 1-7 /star/igate/data/getEDRdata.sh
________________________________________________________
0
djwburr
Asked:
djwburr
  • 4
  • 3
  • 2
  • +3
1 Solution
 
Gabriel OrozcoSolution ArchitectCommented:
I would say when you run this script from your session, all your variables, like PATH are there, but when you run this from the crontab line, these variables do not exist.

try setting the full path for date, and check all other commands you are referencing so you do not need any external variable for this to work.
0
 
svsCommented:
Wrong PATH variable is the prime candidate.  Also, are you sure that script actually ran?  Check cron log for this.
0
 
djwburrAuthor Commented:
So here is my PATH... do I need to modify it so that my cronjob will work? This is the only other command it appears I am referencing.

user@can-3-1-ap:~>% echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/cmcluster/bin:/software/oracle/920/bin:/usr/local/cmcluster/bin:.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
omarfaridCommented:
Hi,

I could see that in many places you redirect output. But, the basic question is to where?

Try to put full path names for the input/output files.

Also, define all necessary env variables like PATH, etc.

0
 
djwburrAuthor Commented:
Sorry... the script I mentioned above "getEDRdata.sh" is located in the PATH /star/igate/data/getEDRdata.sh

Since my HOME is /star/user and the script is located in /star/igate/data/getEDRdata.sh do I need to change anything?
0
 
omarfaridCommented:
Hi,

Two things:

- include /star/igate/data in PATH

PATH=$PATH:/star/igate/data ; export PATH

- either switch to a certain dir then start reading / writing files in the dir or relative to it, or set the full path name for files
0
 
djwburrAuthor Commented:
okay... I'll try that and post in a bit
0
 
Duncan RoeSoftware DeveloperCommented:
You should put a PATH= line right at the front of your script, specifying the entire path. CRON jobs usually start with a path that doesn't include /usr/local/* or oracle &c.
The next line in your script should be "cd  /star/igate/data". Now you will be in the right directory to read / write files.
The command in your crontab already correctly starts the script by its full pathname /star/igate/data/getEDRdata.sh. No need to add that directory to your PATH therefore. Your path includes ".", (working directory), so by cd-ing to /star/igate/data/ you have access to other commands in that directory already.
Once you have made these changes, you should find that "/star/igate/data/getEDRdata.sh" works as a command invoked from anywhere (e.g. your home directory).
0
 
TintinCommented:
There's nothing in the script that requires adding anything to the path.  It just needs the 'cd' to the correct directory to make it work.
0
 
Gabriel OrozcoSolution ArchitectCommented:
I think "date" exists in the path and will not be reachable until it is referecend correctly.

to see where is "date" issue
whereis date
or
which date

and then update your script.
also try always to refer full paths when you are creating scripts for crontab, since it will be much easier to make it work at the first try.
0
 
TintinCommented:
Redimido.

Why do you think the date command would be any different to 'ls' or any other standard command in /usr/bin?
0
 
Gabriel OrozcoSolution ArchitectCommented:
I don't think they are different. I just had problems with path in the past and have solved referencing every command with full path.

that's my point.

Regards
0
 
djwburrAuthor Commented:
I'm testing out the number of suggestions made here and will update soon... .thanks everybody for your help!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now