Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

crontab problem

Status
Not open for further replies.

dessie1981

Programmer
May 9, 2006
116
GB
Hi Everyone,

I'm quite new to using cron,
I need to run a shell script every night and i have put the following entry in /etc/crontab

30 4 * * * root /home/c2000/data_files/automation.sh

The script does not seem to be running as i have output logging to a text file and nothing is being outputted when the script is supposed to be being run by cron

The logging works fine works fine when i run the script manually from the command line.

Below are the permissions of the file.

-rwxr-xr-x 1 root root 1571 Aug 16 11:15 automation.sh

Should i change these??

Also in /var/log/cron is can see the below entry

Aug 30 04:30:01 localhost crond[18034]: (root) CMD (/home/c2000/data_files/automation.sh)

Any help would be greatful

Regards
Dessie
 
Dessie, make sure any PATH etc variables which automation.sh would run with interactively are specified explicitly at the start of the script. cron runs in a very limited environment otherwise.

All I ask of you
Is make my wildest dreams come true
 
Hi Ken,

I have alot of txt files and another program which the shell script calls, i did not put in the full path name, dod you think this could be the problem.

///////////////////////////////////////////////////////////

below is the shell script

#Author Des O Connor
#Date 13/08/06
#this file automatically populates the ***** table in the ******** db
echo processing
echo "run on " |date>> update.log

#unzip the file

unzip myfile.zip >> update.log


#Begin awk section

awk 'BEGIN {FS = "\t"} {print $2}' Enhanced-IRL.tsv > part_no.txt
awk 'BEGIN {FS = "\t"} {print $3}' Enhanced-IRL.tsv > manufacturer.txt
awk 'BEGIN {FS = "\t"} {print $4}' Enhanced-IRL.tsv > model.txt
awk 'BEGIN {FS = "\t"} {print $5}' Enhanced-IRL.tsv > price.txt
awk 'BEGIN {FS = "\t"} {print $9}' Enhanced-IRL.tsv > stock.txt
awk 'BEGIN {FS = "\t"} {print $11}' Enhanced-IRL.tsv > category.txt

#Begin SED Escape section

sed -e 's/\\/\\\\/g' model.txt > backslash_escaped.txt
sed -e s/\'/\\\\\\\'/g backslash_escaped.txt > singlequotes_escaped.txt
sed -e 's/"/\\"/g' singlequotes_escaped.txt > doublequotes_escaped.txt
sed -e 's/%/\\%/g' doublequotes_escaped.txt > percentage_escaped.txt
sed -e 's/_/\\_/g' percentage_escaped.txt > underscore_escaped.txt

#rename escaped file

mv underscore_escaped.txt escaped_model.txt

#run perl program which creates flat sqlfile

perl new_prog2.pl >> update.log
echo "" >> update.log
sleep 30

echo "delete from products;" |mysql -u*** -p******* ******* >> update.log

mysql -u*** -p******** ********* < sqltext.txt >> update.log

rm *sv >> update.log
rm *.txt >> update.log
 
As Ken said, maybe source the .profile in the start of the script, like
Code:
. /root/.profile
or something.
Maybe check root's mail - could be that the cron-daemon tried to inform you.
Or have a simple
Code:
echo "Hello!" > /tmp/mytest.out
in the 1st line of your script to make sure it gets executed at all and check the time of the outfile.

If this all doesn't work, try the same maybe directly in root's own crontab.

laters
zaxxon
 
Hi Dessie - it is better to use full paths for scripts to be run from cron, yes.

All I ask of you
Is make my wildest dreams come true
 
Hi again,

I have solved some of the problems by giving the full paths to all files.
I take it cron is running the scripts in its environment.

However the script is still not running the way i need it to from cron.

When i try to source the profile
. /root/.profile
I am getting
/home/c2000/data_files/automation.sh: line 9: /root/.profile: No such file or directory

When i create files using my script running through cron where does cron create the files?

Any more ideas??
Regards
Dessie



 
cron will put your output files wherever you want them (again, better to specify full pathnames and make sure you have permission to write to the location). I've never used the source .profile method, but would sugegst that you start with a search from the / directory for .profile files:

find / -name '.profile'

and we'll take it from there. The alternative is to specify the important variables directly in the script as I suggested previously.

All I ask of you
Is make my wildest dreams come true
 
Ok just a quick question in the mean time.

if i just want to unzip a file using a script in the crontab.
Example

unzip /home/dessie/myfile.zip

Where will these files inflate to?
They don't inflate to /home/dessie

I think this is my problem, How do i inflate these to /home/dessie

Dessie

 
If your crontab is the root crontab (not sure whether it is?), they will inflate into/under root / (or possibly /root in Linux - not my forte, I'm afraid). Not a good idea really. My assumption would be that if it's Dessie's crontab, it would inflate to Dessies home directory, but apparently not. You may have to use the fully qualified pathname for unzip too, and cd to /home/Dessie immediately beforehand.

As I say, I'm more used to the more 'trad' *nixes, so any of the other Linux guys and gals please feel free to chip in and correct me where necessary!!

All I ask of you
Is make my wildest dreams come true
 
man unzip" shows the syntax and options for the unzip program. I'd use "-d /home/dessie" to do this, or you could change to /home/dessie before running unzip.

I don't have .profile anywhere, but you could also source /etc/profile to get the path.

Personally, I set the full path of programs and scripts to be used in my script in variables and use those:

AWK=/bin/awk
SED=/bin/sed
$AWK 'BEGIN {FS = "\t"} {print $2}' Enhanced-IRL.tsv > part_no.txt
$SED -e 's/\\/\\\\/g' model.txt > backslash_escaped.txt
... etc.
 
Yes they are inflating into "/" , do you know how to redirect this, is there a cron config file i can change??

The script is working fine now but would like the zip file to inflate to the correct dir.

Thanks for the help btw.

 
cd to the appropriate directory before the unzip as suggested?

All I ask of you
Is make my wildest dreams come true
 
but the unzip is running as part of my script in the crontab
 
So, put the cd statement in your script:

#!/bin/bash
...
cd /home/dessie
unzip .....
 
Of Course, I dont know where my head is sometimes!!

Thanx
 
Hi Ken,

Thanks for the help, working a treat.

Regards
Dessie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top