Variable problem in script when using crontab


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variable problem in script when using crontab
# 1  
Old 11-14-2009
Variable problem in script when using crontab

Hi, this is my first shell script. Anyway i am trying to ftp files from one machine to another. The name of the script is ifsftp_orabkp. The code is as follows:

HOST="122.122.122.22"
USER="ftp_ftp"
PASSWD="ftp.222222"
Y1=`(perl -e '@y=localtime(time());printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
Y2=`(perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
D1=$(printf "$Y1" | cut -c 3-8)
D2=$(printf "$Y2" | cut -c 3-8)
cd /exports
ftp -nv $HOST << END_SCRIPT > /home/oracle/oracle_logs/ftpout_ora.txt
quote USER $USER
quote PASS $PASSWD
prompt
cd /erdbvol1/IFS_BKPS/ora_bkp
mput *$D1*
mput *$D2*
END_SCRIPT

When I try to run it using the following it works without any problems:
./ifsftp_orabkp

However when I use crontab :
45 10 * * * /home/oracle/oracle_scripts/ifsftp_orabkp >> /home/oracle/oracle_logs/ftpora.log 2>&1

ftpora.log shows the following errors:
$ cat ftpora.log
sh: /home/oracle_scripts/ifsftp_orabkp: not found
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 1: `$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected

Please help!
# 2  
Old 11-14-2009
This script is relying on the environment being set. However this is not the case if it is run from crontab. In this case probably only the PATH variable will need to be set somewhere at the beginning. It should contain the paths to all the external commands that are bing called. I do not know where your perl binary is located. Possibly this is enough:
Code:
PATH=/bin:/usr/bin

# 3  
Old 11-14-2009
Hi, thanks for responding, I tried that but it didnt work. I also modified the script a bit to simplify:

HOST="133.133.133.33"
USER="ttt123"
PASSWD="tt22222"
PATH=/bin:/usr/bin
Y1="20091114"
D1=$(printf "$Y1" | cut -c 3-8)
cd /exports
ftp -nv $HOST << END_SCRIPT > /home/oracle/oracle_logs/ftpout_ora.txt
quote USER $USER
quote PASS $PASSWD
prompt
cd /erdbvol1/IFS_BKPS/ora_bkp
mput *$D1*
END_SCRIPT

./ifsftp_orabkp is working, however from crontab there is still the error:

syntax error at line 7: `D1=$' unexpected

Is the syntax of the D1 variable correctly
# 4  
Old 11-14-2009
Better put a shebang (#!) with the shell name and its full path on your first line too. It may be that the default shell is not the same as the shell this script needs to be run in. It could be that it then uses a very classic Bourne shell that does not know the $(...) construct.
# 5  
Old 11-14-2009
Hi, on the first line I added:
#!/bin/ksh

but im still getting :
syntax error at line 7: `D1=$' unexpected..
# 6  
Old 11-14-2009
Echo your env in your script and have it run from cron.

So put this at the beginning of your script

env > /tmp/env

Have that run from cron, and then post the results. This is a simple way to trouble shoot enviornment variable problems. You can compare to how its running from cron, from when you're executing it yourself.
# 7  
Old 11-14-2009
The cron does NOT automatically source your profile. You have to source it in your script, which is a good practice anyway.

#!/usr/bin/ksh


#
# Note this is (dot)space(tilde)/.profile
#
. ~/.profile (or whichever profile is appropriate for your default shell)

This should solve your problem.

Hope it helps. Good luck.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Script problem when running on crontab

Hi guys! I created a backup script that works fine when I run manually, but when I put a crontab job to execute it the result are not the expected. (not a time problem). Here is my script: bash-3.00# cat /bk_tool/backup2.sh #!/usr/bin/csh clear set DIR_HOST='SCP08' ... (3 Replies)
Discussion started by: andredemartini
3 Replies

2. UNIX for Dummies Questions & Answers

Problem with a script in crontab

Hi guys, I have a shell script which i have put on crontab to run every min. Problem is that is isn't running. But if i run the script from the existing location it runs perfectly. but if i got out of the directory and run it giving the path. it doesn't work. sh script_name.sh ----... (3 Replies)
Discussion started by: nick1982
3 Replies

3. Shell Programming and Scripting

Problem with perl script on crontab

Hi everybody, I have a perl script. I want to run it as a cron job every 10 minutes. I insert the command into crontab , using crontab -e. The problem is, I didnt get any output from script inside crontab, whereas when I run it manually through 'perl myscript.pl' it running well. Here is the cron... (2 Replies)
Discussion started by: franzramadhan
2 Replies

4. HP-UX

Problem with script arguments in crontab

I have a script scheduled in cron as shown below. 16 12 * * * /users/mydir/bin/process_master.ksh `date '+%Y%m%d'` >> /users/mydir/process_master.out This script executes correctly when invoked on command line, but doesn't work the same when scheduled in crontab. Crontab is working fine... (3 Replies)
Discussion started by: mk1216
3 Replies

5. UNIX for Dummies Questions & Answers

How to retrieve the value of variable in shell script which is called by crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (4 Replies)
Discussion started by: jadoo_c2
4 Replies

6. Shell Programming and Scripting

environment variable in shell script called through crontab

Please help me on below.. https://www.unix.com/shell-programming-scripting/141533-retrieve-value-environment-variable-shell-script-called-crontab.html#post302442024 I'm still here. I can still see you! (0 Replies)
Discussion started by: jadoo_c2
0 Replies

7. Shell Programming and Scripting

Retrieve the value of environment variable in shell script which called from crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (0 Replies)
Discussion started by: jadoo_c2
0 Replies

8. Shell Programming and Scripting

Problem with crontab running a script

I am trying to use the CRON utility in Fedora 11 & CentOS... I intend to run a script which pops up a warning message every hour and i made the following entry using "CRONTAB -e " * * * * * sh /bin/myscript.sh But this does not seem to be running. Another thing to note is that,... (4 Replies)
Discussion started by: Vabiosis
4 Replies

9. Linux

Problem with crontab + bash script

Crontab: 16 14 * * * /root/bin/./empty_mail.sh >> /root/bin/log.txt L=`mailq |grep -c frozen` echo "${NUMMAIL} frozen mail to be removed from the mailq" #echo "Press Enter to continue" #read CONTINUE echo -en "-> " for i in `mailq | grep frozen | awk '{print $3}'` ; do exim4 -Mrm $i... (1 Reply)
Discussion started by: wessberg
1 Replies

10. Solaris

Unix script Problem with crontab

Hi Experts, Am facing some problems with script (Sun solaris 9). My script (sqlplus.sh)looks like this. . . ............................................... set 'date' dd=$3 export dd mon=$2 export mon yyyy=$6 export yyyy cd /oracle/P47/saparch... (8 Replies)
Discussion started by: vrjalli
8 Replies
Login or Register to Ask a Question