FTP session not working on cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP session not working on cron
# 1  
Old 07-14-2015
Hammer & Screwdriver FTP session not working on cron

Hi, I'm experiencing an issue with creating an ftp session via cron.

I am creating an ftp session between a data server and a reporting tool. I have created the script wherein the data server begins the ftp session and puts files into the tool.

I have used the .netrc files for this and I run this command as root user

When I run the command manually (./script.sh), it works normally. It logs in and all files transfer properly. But when I make it a cron job (as root user), it fails and gives me an error "User cannot log in".

See below the .netrc and ftp batch script:

Code:
.netrc
machine <machine-ip> login <user> password <password>

macdef upload
        prompt off
        bin
        mput *.text
        ls
        quit


macdef list
        prompt off
        ls
        quit


Ftp script:
Code:
#!/usr/bin/ksh
#Batch file for uploads

HOST="<machine-ip>"
#datesearch="`TZ=GMT+16 date +%Y-%m-%d`"
#filename="`TZ=GMT+16 date +%Y-%m-%d`_upload"
cd <directory in root>/$datesearch
num=`ls *.text |wc -l`
echo "Number of files to transfer: $num" > <directory in root>/$filename.txt

echo "`date +%m-%d-%Y` `date +%H:%M:%S` : Start script" >> <directory in root>/$filename.txt
echo "$ upload" | ftp $HOST >> <directory in root>/$filename.txt
CONTENT="$(echo "$ list" | ftp $HOST)"


suc=`echo "$CONTENT" |grep *.text |wc -l`
echo "Number of successfully transferred files: $suc" >> <directory in root>/$filename.txt

echo "`date +%m-%d-%Y` `date +%H:%M:%S` : End script" >> <directory in root>$filename.txt


My crontab entry is as follow:
Code:
50 16 * * * /<absolute directory>/ftpscript.sh

Here's the output from the logs after being run on cron:
Code:
Number of files to transfer:     4000
07-14-2015 16:38:00 : Start script
Name (machine-ip:root): User cannot log in.
Number of successfully transferred files:        0
07-14-2015 16:38:20 : End script


Hope you can help me on this.

Thanks!

Last edited by rbatte1; 07-14-2015 at 07:01 AM.. Reason: Wrapped output in CODE tags too
# 2  
Old 07-14-2015
Can I just check that the user running the job (i.e. owning the cron definition) is the same one that has the .netrc with the credentials in.

These lines also concern me:-
Code:
:
:
echo "$ upload" | ftp $HOST >> <directory in root>/$filename.txt
CONTENT="$(echo "$ list" | ftp $HOST)"
:
:

Should there be a space of the $?



Kind regards,
Robin
# 3  
Old 07-14-2015
Quote:
Originally Posted by rbatte1
Can I just check that the user running the job (i.e. owning the cron definition) is the same one that has the .netrc with the credentials in.

These lines also concern me:-
Code:
:
:
echo "$ upload" | ftp $HOST >> <directory in root>/$filename.txt
CONTENT="$(echo "$ list" | ftp $HOST)"
:
:

Should there be a space of the $?



Kind regards,
Robin
Hi Robin (we have the same name!),

The .netrc and the ftp script file are both owned by root user under root group.

For the other concern, $HOST is correct as it is calling the HOST variable, as opposed to the echo $ list where it calls the macro.

Robin

Last edited by rbatte1; 07-14-2015 at 07:11 AM.. Reason: Added ICODE tags
# 4  
Old 07-14-2015
For sending commands in to run macros, that's excellent and I'm always happy to learn something new. Smilie

I can see that the job is run by root too from the output.

Just a stab, but as the profiles that normally set up the environment are not run, perhaps you need to set some yourself. The first one to consider would be HOME.

Can you display the default environment by running this with cron:-
Code:
env | sort > /tmp/my_env
set | sort > /tmp/my_set

Obviously don't post anything sensitive, but can you paste the content here (in CODE tags) or attach the files here?




Robin
# 5  
Old 07-14-2015
Quote:
Originally Posted by rbatte1
For sending commands in to run macros, that's excellent and I'm always happy to learn something new. Smilie

I can see that the job is run by root too from the output.

Just a stab, but as the profiles that normally set up the environment are not run, perhaps you need to set some yourself. The first one to consider would be HOME.

Can you display the default environment by running this with cron:-
Code:
env | sort > /tmp/my_env
set | sort > /tmp/my_set

Obviously don't post anything sensitive, but can you paste the content here (in CODE tags) or attach the files here?




Robin

Thanks for replying, you can see the output below (users are changed to user1, user2 etc.)

Code:
more my_env 
HOME=/export/home/user1
LANG=C
LC_ALL=
LC_COLLATE=
LC_CTYPE=
LC_MESSAGES=
LC_MONETARY=
LC_NUMERIC=
LC_TIME=
LOGNAME=user1
MAIL=/var/mail/user1
OLDPWD=/<dataserver>/root/<directory-to-ftpscript>
PAGER=/usr/bin/less -ins
PATH=/usr/sbin:/usr/bin
PWD=/tmp
SHELL=/usr/bin/pfbash
SHLVL=2
SSH_CLIENT=<ip port>
SSH_CONNECTION=<ip port>
SSH_TTY=/dev/pts/59
TERM=xterm
TZ=localtime
USER=user1
_=/usr/bin/env



Code:
more my_set 
BASH=/usr/bin/bash
BASHOPTS=cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath:xpg_echo
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="11" [3]="2" [4]="release" [5]="sparc-sun-solaris2.11")
BASH_VERSION='4.1.11(2)-release'
COLUMNS=191
DIRSTACK=()
EUID=0
GROUPS=()
HISTFILE=/export/home/user1/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/export/home/user1
HOSTNAME=<hostname>
HOSTTYPE=sparc
IFS=$' \t\n'
LANG=C
LC_ALL=
LC_COLLATE=
LC_CTYPE=
LC_MESSAGES=
LC_MONETARY=
LC_NUMERIC=
LC_TIME=
LINES=37
LOGNAME=user1
MACHTYPE=sparc-sun-solaris2.11
MAIL=/var/mail/user1
MAILCHECK=60
OLDPWD=/<dataserver>/root/<directory-to-ftpscript
OPTERR=1
OPTIND=1
OSTYPE=solaris2.11
PAGER='/usr/bin/less -ins'
PATH=/usr/sbin:/usr/bin
PIPESTATUS=([0]="0" [1]="0")
PPID=2735
PS1='\u@\h:\w\$ '
PS2='> '
PS4='+ '
PWD=/tmp
SHELL=/usr/bin/pfbash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_CLIENT=<ip port>
SSH_CONNECTION=<ip port>
SSH_TTY=/dev/pts/59
TERM=xterm
TZ=localtime
UID=0
USER=user1
_=

Thanks,

Robin
# 6  
Old 07-14-2015
I'm slightly confused by the value of variable HOME. You say that you are running as the root user, but the HOME value is set as /export/home/user1 Should the value not be the home directory of the root user? i.e. / or /root


Robin
# 7  
Old 07-14-2015
Quote:
Originally Posted by rbatte1
I'm slightly confused by the value of variable HOME. You say that you are running as the root user, but the HOME value is set as /export/home/user1 Should the value not be the home directory of the root user? i.e. / or /root


Robin
Ahh yeah, it does. Hmm. Is there a way for me to change this? Although, I accessed the server via ssh using user1@host.ip then changing users via su so this could be the reason?

Also, would this affect the script running via cron as compared to running it manually?

Thanks

Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using pipe in FTP session

Hi All , I want to use pipe in ftp session . Please let me know if there is any possibility I have done FTP to a remote host with proper username and password Now I want to take the latest file in a directory . The below is not workinf ftp> ls -lrt |... (4 Replies)
Discussion started by: Sandeep Lade
4 Replies

2. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

3. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Non-interactive FTP within SSH session not working

Hello everyone! I am trying to log-in to a remote server over SSH, transfer file1 there, perform some checks, capture the results in file2 and transfer file2 back to my local server - all of this non-interactively. Initially, I tried to do this within a singe SSH session, using the following... (2 Replies)
Discussion started by: Subu1987
2 Replies

5. Shell Programming and Scripting

Cron job vanishes if i disconnect the session upruptly

Hi Am logging in to the server as root. Using the export EDITOR=vi option before editing the cron file editing the crontab entry using crontab -e option the additional jobs which i added can be seen immediately, but when i disconnect the session upruptly the cron entry vanishes the next time... (4 Replies)
Discussion started by: aemunathan
4 Replies

6. Shell Programming and Scripting

executing commands through FTP session

Dears, i want to execute unix commands through FTP session those commands like grep XXXXX file name > new file tar new file ..... etc Please let me know how to so thank you (3 Replies)
Discussion started by: faiz
3 Replies

7. UNIX for Dummies Questions & Answers

FTP Session

In FTP session, how could know the present working directory in local machine? pwd command gives the present working directory for remote machine only. (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

8. Shell Programming and Scripting

Looping in FTP session

Hi I am trying rename multiple files in a remote location by connecting through FTP. How do i do this? I get error as for is not FTP command. I looked the FAQ for scripting but it is doesnt relate to my post. Please help. Thanks ftp -in >Error.log <<! open $HOST user $User $Pwd for... (2 Replies)
Discussion started by: appsguy616
2 Replies

9. Shell Programming and Scripting

display while in ftp session

Is there any command which displays date while i am in a ftp session? I tried the date command, but in vain ftp> date ?Invalid command ftp> Can someone please help me. Thanks (2 Replies)
Discussion started by: vivek_damodaran
2 Replies

10. Shell Programming and Scripting

Loops within ftp shell session

Hi guys, i need to script the sending of multiple files through one ftp session. I have done this so far: ---------------------------------------------------------------- #!/bin/sh USER=myuser PASSWD=mypass filenum=$1 x=0 ftp -n 159.167.95.199 <<SCRIPT user $USER $PASSWD binary while ... (4 Replies)
Discussion started by: zaff
4 Replies
Login or Register to Ask a Question