1834645 Members
2168 Online
110069 Solutions
New Discussion

Cron job

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Cron job

Hi All,

I have a script called run_sftp for transfering a file to another server.The script run_sftp will call a script called sftp. sftp script is using expect instead shell.

When I run it manually, it was running perfectly. But when run it through cronjob, it is not running fine. Nothing on cron log, but the file can not be transfered.

Is it the issue with the environment? How can I make it run through crontab? Pls help. High score will be given.

Thanks and Best Regards,
Negara

# crontab -l|grep sftp
37 3 * * * /opt/sapapp/L6A/SC/scripts/transfer/run_sftp > /tmp/sftp 2>&1

#more /var/adm/cron/log
> CMD: /opt/sapapp/L6A/SC/scripts/transfer/run_sftp > /tmp/sftp 2>&1
> root 16884 c Tue Dec 7 03:37:00 EST 2004
< root 16884 c Tue Dec 7 03:37:15 EST 2004



Santos
12 REPLIES 12
Jean-Luc Oudart
Honored Contributor
Solution

Re: Cron job

Hi Dewa,

usually if you can run manually but not through cron it goes back to environment initialisation. Cron does not run profile therefore it's up to you to initialise the variables in the script.

we have a profile.cron for cron processing.
a script run through cron would be wrapped with another script to initialise required variables.

Regards
Jean-Luc
fiat lux
john korterman
Honored Contributor

Re: Cron job

Hi,
it is probably the missing environment. Try first to look for clues in the mail for the user who executes the cron job.

regards,
John K.
it would be nice if you always got a second chance
bhavin asokan
Honored Contributor

Re: Cron job

hi,

when you are running script ,you are giving the user name & passwd manually?

if it is like that,just make a file .netrc in your users home directory(who is running this script) with the contents like following.(change the entries as per your server .change ip address ,user name which comes after login and passwd)


machine 192.99.3.3 login dba password india
machine 172.25.2.2 login sa password jk123
machine 172.25.3.50 login ora1 password jk456

then run cron

regds,
Dewa Negara_4
Regular Advisor

Re: Cron job

Hi All,

Thanks alot. I am not using .netrc, but using expect script to transfer the file without public/private keys. I attach the script below. I have tried to change the environment, but still can not solve the issue. Pls help. Thanks alot.

Regards,
Dewa

[root@prod:/opt/sapapp/L6A/SC/scripts/transfer]
# ll
total 10
-rw-r--r-- 1 root sys 9 Dec 1 21:55 .password
-rw-r--r-- 1 root sys 97 Dec 7 02:22 batchFile
-rwx------ 1 root sys 122 Dec 7 04:03 run_sftp
-rwx------ 1 root sys 495 Dec 2 02:03 sftp

[root@prod:/opt/sapapp/L6A/SC/scripts/transfer]
# more run_sftp
#!/usr/bin/sh -x

. /opt/soeg/bin/setup_env.sh /opt/soeg/conf/env_vars.conf
cd /opt/sapapp/L6A/SC/scripts/transfer
./sftp

[root@prod:/opt/sapapp/L6A/SC/scripts/transfer]
# more sftp
#!/usr/local/bin/expect

# Initialisation
set authFile "/opt/sapapp/L6A/SC/scripts/transfer/.password"

# Check the authorisation file exists
if {![file exists $authFile]} { ;# Does file exist
send_user "$authFile does not exist; aborting\n"
exit 1
}
set fileFD [open $authFile r] ;# Open the auth file
gets $fileFD authLine ;# Read in 1 line to authLine
close $fileFD

# Transfer file(s)
spawn sftp -b batchFile importve@www.importve.com
expect "password:"
send "$authLine\n";
interact
Santos
Fred Ruffet
Honored Contributor

Re: Cron job

What is content of /tmp/sftp ?

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Dewa Negara_4
Regular Advisor

Re: Cron job

Hi Fred,

1) Content of /tmp/sftp when running trhough crontab:

[root@prod:/var/adm]
# more /tmp/sftp
+ . /opt/soeg/bin/setup_env.sh /opt/soeg/conf/env_vars.conf
+ [[ /opt/soeg/conf/env_vars.conf = -help ]]
+ [[ 1 != 1 ]]
+ [[ ! -a /opt/soeg/conf/env_vars.conf ]]
+ cat /opt/soeg/conf/env_vars.conf
+ read -r Env_variable Value
+ export SOE=/opt/soeg
+ read -r Env_variable Value
+ export SOEG=/opt/soeg
+ read -r Env_variable Value
+ export SOEAPPINST=/usr/local/applications
+ read -r Env_variable Value
+ export SOEAPPCONF=/usr/local/applications/conf
+ read -r Env_variable Value
+ export SOESTARTSTOP=/usr/local/applications/startstop
+ read -r Env_variable Value
+ export SOEBIN=/opt/soeg/bin
+ read -r Env_variable Value
+ export SOEDOC=/opt/soeg/doc
+ read -r Env_variable Value
+ export SOECONF=/opt/soeg/conf
+ read -r Env_variable Value
+ export SOEINSTCONF=/opt/soeg/install/conf
+ read -r Env_variable Value
+ export SOELOG=/var/opt/soeg/log
+ read -r Env_variable Value
+ export SOERPT=/var/opt/soeg/rpt
+ read -r Env_variable Value
+ export SOEDATA=/opt/soeg/data
+ read -r Env_variable Value
+ export SOEBKUP=/var/opt/soeg/sys_snapshot
+ read -r Env_variable Value
+ export SOESEC=/opt/soeg/sectools
+ read -r Env_variable Value
+ export SOESECRPT=/var/opt/soeg/sectools/rpt
+ read -r Env_variable Value
+ export SAVENODE=/var/opt/soeg/savenode
+ read -r Env_variable Value
+ export BKP_LOG=/var/opt/soeg/log
+ read -r Env_variable Value
+ export RECOVERY=/opt/soeg/recovery
+ read -r Env_variable Value
+ export RECOVERYBIN=/opt/soeg/recovery/bin
+ read -r Env_variable Value
+ export RECOVERYLOG=/var/opt/soeg/recovery/log
+ read -r Env_variable Value
+ export RECOVERYRPT=/var/opt/soeg/recovery/rpt
+ read -r Env_variable Value
+ export RECOVERYDOC=/opt/soeg/recovery/doc
+ read -r Env_variable Value
+ export RECOVERYCONF=/opt/soeg/recovery/conf
+ read -r Env_variable Value
+ export RECOVERYDATA=/opt/soeg/recovery/data
+ read -r Env_variable Value
+ export PERF=/var/opt/perf
+ read -r Env_variable Value
+ export PERFLOG=/var/opt/perf/datafiles
+ read -r Env_variable Value
+ export SUPPORTW=/opt/soeg/supportw
+ read -r Env_variable Value
+ export SUPPORTWBIN=/opt/soeg/supportw/bin
+ read -r Env_variable Value
+ export SUPPORTWLOG=/var/opt/soeg/supportw/log
+ read -r Env_variable Value
+ export SUPPORTWRPT=/var/opt/soeg/supportw/rpt
+ read -r Env_variable Value
+ export SUPPORTWDOC=/opt/soeg/supportw/doc
+ read -r Env_variable Value
+ export SUPPORTWCONF=/opt/soeg/supportw/conf
+ read -r Env_variable Value
+ export SUPPORTWDATA=/opt/soeg/supportw/data
+ read -r Env_variable Value
+ export SYSMGR=/usr/local/sysmgr
+ read -r Env_variable Value
+ export SOEINST=/opt/soeg/install
+ read -r Env_variable Value
+ export SOEINSTBIN=/opt/soeg/install/bin
+ read -r Env_variable Value
+ export SOEINSTLOG=/var/opt/soeg/install/log
+ read -r Env_variable Value
+ export APPSTDS=/usr/local/applications/app_stds
+ read -r Env_variable Value
+ export CONTROLM=/opt/ctms/ctm
+ read -r Env_variable Value
+ export SYBASE=/opt/ctms/ctm/sybase
+ read -r Env_variable Value
+ export DSLISTEN=CTRLM
+ read -r Env_variable Value
+ export DSQUERY=CTRLM
+ read -r Env_variable Value
+ export DSCONSOLE=CTRLM
+ read -r Env_variable Value
+ export CONTROLM_USER=ctms
+ read -r Env_variable Value
+ export CONTROLM_DATABASE=ctms
+ read -r Env_variable Value
+ export NDMAPICFG=/opt/cdirect/ndm/cfg/cliapi/ndmapi.cfg
+ read -r Env_variable Value
+ export NBBIN=/usr/openv/netbackup/bin
+ read -r Env_variable Value
+ export NBCONF=/usr/openv/netbackup/conf
+ read -r Env_variable Value
+ export NBLOG=/usr/openv/netbackup/logs/bp_notify
+ read -r Env_variable Value
+ cd /opt/sapapp/L6A/SC/scripts/transfer
+ ./sftp
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com
spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFile importve@www.importve.com

spawn sftp -b batchFileymhung04

2) Content of /tmp/sftp when running manually:

[root@prod:/var/adm]
# more /tmp/sftp
+ . /opt/soeg/bin/setup_env.sh /opt/soeg/conf/env_vars.conf
+ [[ /opt/soeg/conf/env_vars.conf = -help ]]
+ [[ 1 != 1 ]]
+ [[ ! -a /opt/soeg/conf/env_vars.conf ]]
+ read -r Env_variable Value
+ cat /opt/soeg/conf/env_vars.conf
+ export SOE=/opt/soeg
+ read -r Env_variable Value
+ export SOEG=/opt/soeg
+ read -r Env_variable Value
+ export SOEAPPINST=/usr/local/applications
+ read -r Env_variable Value
+ export SOEAPPCONF=/usr/local/applications/conf
+ read -r Env_variable Value
+ export SOESTARTSTOP=/usr/local/applications/startstop
+ read -r Env_variable Value
+ export SOEBIN=/opt/soeg/bin
+ read -r Env_variable Value
+ export SOEDOC=/opt/soeg/doc
+ read -r Env_variable Value
+ export SOECONF=/opt/soeg/conf
+ read -r Env_variable Value
+ export SOEINSTCONF=/opt/soeg/install/conf
+ read -r Env_variable Value
+ export SOELOG=/var/opt/soeg/log
+ read -r Env_variable Value
+ export SOERPT=/var/opt/soeg/rpt
+ read -r Env_variable Value
+ export SOEDATA=/opt/soeg/data
+ read -r Env_variable Value
+ export SOEBKUP=/var/opt/soeg/sys_snapshot
+ read -r Env_variable Value
+ export SOESEC=/opt/soeg/sectools
+ read -r Env_variable Value
+ export SOESECRPT=/var/opt/soeg/sectools/rpt
+ read -r Env_variable Value
+ export SAVENODE=/var/opt/soeg/savenode
+ read -r Env_variable Value
+ export BKP_LOG=/var/opt/soeg/log
+ read -r Env_variable Value
+ export RECOVERY=/opt/soeg/recovery
+ read -r Env_variable Value
+ export RECOVERYBIN=/opt/soeg/recovery/bin
+ read -r Env_variable Value
+ export RECOVERYLOG=/var/opt/soeg/recovery/log
+ read -r Env_variable Value
+ export RECOVERYRPT=/var/opt/soeg/recovery/rpt
+ read -r Env_variable Value
+ export RECOVERYDOC=/opt/soeg/recovery/doc
+ read -r Env_variable Value
+ export RECOVERYCONF=/opt/soeg/recovery/conf
+ read -r Env_variable Value
+ export RECOVERYDATA=/opt/soeg/recovery/data
+ read -r Env_variable Value
+ export PERF=/var/opt/perf
+ read -r Env_variable Value
+ export PERFLOG=/var/opt/perf/datafiles
+ read -r Env_variable Value
+ export SUPPORTW=/opt/soeg/supportw
+ read -r Env_variable Value
+ export SUPPORTWBIN=/opt/soeg/supportw/bin
+ read -r Env_variable Value
+ export SUPPORTWLOG=/var/opt/soeg/supportw/log
+ read -r Env_variable Value
+ export SUPPORTWRPT=/var/opt/soeg/supportw/rpt
+ read -r Env_variable Value
+ export SUPPORTWDOC=/opt/soeg/supportw/doc
+ read -r Env_variable Value
+ export SUPPORTWCONF=/opt/soeg/supportw/conf
+ read -r Env_variable Value
+ export SUPPORTWDATA=/opt/soeg/supportw/data
+ read -r Env_variable Value
+ export SYSMGR=/usr/local/sysmgr
+ read -r Env_variable Value
+ export SOEINST=/opt/soeg/install
+ read -r Env_variable Value
+ export SOEINSTBIN=/opt/soeg/install/bin
+ read -r Env_variable Value
+ export SOEINSTLOG=/var/opt/soeg/install/log
+ read -r Env_variable Value
+ export APPSTDS=/usr/local/applications/app_stds
+ read -r Env_variable Value
+ export CONTROLM=/opt/ctms/ctm
+ read -r Env_variable Value
+ export SYBASE=/opt/ctms/ctm/sybase
+ read -r Env_variable Value
+ export DSLISTEN=CTRLM
+ read -r Env_variable Value
+ export DSQUERY=CTRLM
+ read -r Env_variable Value
+ export DSCONSOLE=CTRLM
+ read -r Env_variable Value
+ export CONTROLM_USER=ctms
+ read -r Env_variable Value
+ export CONTROLM_DATABASE=ctms
+ read -r Env_variable Value
+ export NDMAPICFG=/opt/cdirect/ndm/cfg/cliapi/ndmapi.cfg
+ read -r Env_variable Value
+ export NBBIN=/usr/openv/netbackup/bin
+ read -r Env_variable Value
+ export NBCONF=/usr/openv/netbackup/conf
+ read -r Env_variable Value
+ export NBLOG=/usr/openv/netbackup/logs/bp_notify
+ read -r Env_variable Value
+ cd /opt/sapapp/L6A/SC/scripts/transfer
+ ./sftp
spawn sftp -b batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:
sftp> lcd /opt/sapapp/L6A/SC/data/Venezuela/Purchases
sftp> cd /data/web/90558/www/sap/
sftp> put Purchase.txt
Uploading Purchase.txt to /data/web/90558/www/sap/Purchase.txt
Uploading Purchase.txt to /data/web/90558/www/sap/Purchase.txt
sftp> bye


Santos
Fred Ruffet
Honored Contributor

Re: Cron job

What happens if you launch your script with nohup ? (I mean "if not attached to a terminal")

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Dewa Negara_4
Regular Advisor

Re: Cron job

Hi Fred,

I tried to use nohup, but again it was running well when run it manually, but not using cron. Contect of /tmp/sftp is exactly the same.

[root@prod:/opt/sapapp/L6A/SC/scripts/transfer]
# crontab -l|grep sftp
54 4 * * * nohup /opt/sapapp/L6A/SC/scripts/transfer/run_sftp > /tmp/sftp 2>&1

Santos
Fred Ruffet
Honored Contributor

Re: Cron job

If it runs without a terminal, then it is an environment problem. Try to source your profile at begining of the script. Expect is probably missing a variable.

add this line :
. $HOME/.profile

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
bhavin asokan
Honored Contributor

Re: Cron job

hi,

sorry i forgot to ask you.
you are having cron.allow or cron.deny files in your /var/adm/cron directory.if existing the cron.allow file should contain the user name by which you are running sftp and the cron.deny file should not.(cron.allow with some user name s will allow only that users to run the cron.)

note:if cron.allow is there root entry should be must on that file.

regds,
Jean-Luc Oudart
Honored Contributor

Re: Cron job

Just to add on Fred's last post.

If there is any interaction in the .profile, I would suggest you take a copy into a profile.con file and remove the interaction(s).

Regards
Jean-Luc
fiat lux
Srini_22
New Member

Re: Cron job

Hi, we too had the same problem. We are through now.

My suggestion is, search for sftp in your machine/server (Eg./usr/local/bin/sftp).
In your script add this path to the PATH variable. Make sure that sftp path is available to your cronjob script. Hope this works fine.

thanks
Allada