script works on command line, not in cron job


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers script works on command line, not in cron job
# 1  
Old 06-26-2007
script works on command line, not in cron job

Hey there, I'm a total newbie unix guy here and just picking this stuff up. Have a very small script I put together that works fine from the command line but not once I put it in a cron job. Searched and found this thread and am wondering it it has something to do with setting variables, though the error I'm getting seems to be with exicuting the date() function.

Here it goes...
Code:
lastUpdate="`more /path/to/date/file/lastUpdated.txt`"; 
daDate="`date '+%Y %m %d'`"; 
if test "$lastUpdate" = "daDate"; 
then wget --output-document="/path/to/docs/$daDate - daFile.pdf" http://www.mysite.com/downloadMe.pdf"; 
fi

the 'lastUpdated' file contains a date like so... "2007 06 26"

Like I said, if I put this in the command line it works great, just not in a cron job.

Thanks in advance for the help.
# 2  
Old 06-26-2007
The environment variable PATH is different for cron jobs than for your shell.
Add 'env >/tmp/cronob.log' at the beginning of your script and find out exactly what the path is, then either modify it in the script, or use the absolute file name for the command.
# 3  
Old 06-26-2007
As mentioned, normal reason for this is the environment (eg all the strings you see printed when you type "set") is minimal when run as a cronjob. This requires you to set any environment variables you will rely on directly in your script.

Also, you are missing the normal "#!/bin/sh" line from the start of your script, this tells the operating system which interpreter/shell to use to run your script.
# 4  
Old 06-26-2007
Both, thanks for the reply.

I put env >/tmp/cronob.log into the script (as well as #!/bin/sh, thanks) and still doesn't seem to be working. I'm not sure what you mean by "you need to set any environment variables". Do you mean I need to do that in this log file?
# 5  
Old 06-26-2007
Jack,
You will understand better if you type the command 'env' in the unix prompt.
Your script may need some of these variables to run properly.
One solution is to add the following line into your script:
Code:
. ~/.profile

# 6  
Old 06-26-2007
Reading the FAQs prior to posting tends to be the most helpful.
# 7  
Old 06-26-2007
Some things that leap out at me are

1. Are you going to set the current directory to the one you expect for this script?

2. is wget on the PATH that your script gets, if not use a fully qualified path to wget

3. did you put the fully qualified path to your script in the crontab?

4. did you set the executable flag on your script?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command works at command line but not from cron

Oracle Linux 6. Trying to set up a simple monitoring of memory usage. This command does exactly what I want at the command line: echo $(date +%Y-%m-%d" "%H:%M:%S) $(grep PageTables /proc/meminfo) >> /home/oracle/meminfo.logBut when I put it in my crontab: * * * * * echo $(date +%Y-%m-%d"... (2 Replies)
Discussion started by: edstevens
2 Replies

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

3. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

4. AIX

Cron job not working but works on other nodes.

Hi All, I have a many cron jobs scheduled in my AIX server. Only one cron not getting executed in the same server but that job is good on all other servers. Here is my cron , which will keep last 30 files and remove others., 00 00 * * * /usr/bin/find /tmp/reports/nmon -name *.nmon -mtime... (9 Replies)
Discussion started by: Thala
9 Replies

5. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

6. UNIX for Dummies Questions & Answers

Works on command line but not in script

Hey guys. Hopefully this is an easy one but having reference similar problems on the web I still can't fix it. I am doing a recursive find and replace from a script. Of course I could just run the damn thing from the command line but it's bugging me now and want to get it working. grep -rl... (4 Replies)
Discussion started by: anthonyjstewart
4 Replies

7. Shell Programming and Scripting

s3cmd works on command line not on cron

Ubuntu 9.10 is my linux distro Based on forums they say that the problem is with environment . here is my case: login as user, then sudo -s using this command: s3cmd put file s3://bucket >>worked! now here is the simple script intended for testing: #! /bin/bash env >/tmp/cronjob.log... (1 Reply)
Discussion started by: qwerty20
1 Replies

8. Shell Programming and Scripting

find cmd works different on cron job ?

/usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a -name '*.csv' \) -o -name '*_xyz_*' \) -mtime $DAYS_AGO -printf %f -printf "\n" | sort -r > $FILES The above command gives different results when run on a cron job. When run manually the result is accurate. (2 Replies)
Discussion started by: nuthalapati
2 Replies

9. Shell Programming and Scripting

Zgrep works at command line but not in script?

Hi all- I'm trying to search through some .gz log files to verify certain feeds have passed through our app. I have a small script that I wrote in hopes that I could automate the checking but haven't been able to get the zgrep to work. When I copy it to the command line directly it works... (2 Replies)
Discussion started by: Cailet
2 Replies

10. Shell Programming and Scripting

Cron job fails, but works fine from command line

I have a very basic script that essentially sends a log file, via FTP, to a backup server. My cron entry to run this every night is: 55 23 * * * /usr/bin/archive_logs The script runs perfectly when executed manually, and actually worked via cron for about three weeks. However, it mysteriously... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question