Sftp in a crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sftp in a crontab
# 8  
Old 02-11-2018
What operating system are you using?

What shell are you using as your login shell?

In what way is ~/.profile not working? Please show us the exact output it produces (in CODE tags).

Most problems that we see in this forum where something works when run from a login shell but fails when run by cron are because:
  • A different shell than your login shell is being used by cron to run scripts.
  • Your shell's initialization scripts that are run when you login are not being run by cron so environment variables that your script may depend up have not been set.
  • Your login shell runs scripts in an interactive environment; cron runs jobs without connection to a controlling terminal so there is no way for you as a user to respond to prompts written by your script.
Yoda's suggestion to run ~/.profile was an attempt to set the environment variables usually set by your login shell for the cron job you are running. But that suggestion makes some assumptions about what shell you use as your login shell and about the shell used by cron to run jobs on your operating system.
# 9  
Old 02-12-2018
Hello

any hint here please
# 10  
Old 02-12-2018
There have been questions asked that you didn't answer yet. Your chances of getting a reasonable hint depend on / increase with your answers given... You should supply any detail possible for error analysis and elimination.
# 11  
Old 02-12-2018
If you could post the output (in CODE tags) from these commands, that will give us a start:-
Code:
uname -a
ps -f

When you log in (or start a new terminal) what gets displayed on your screen? Paste the screen text highlighting any errors into the thread, again in CODE tags or paste an image if you can do that.

I'm assuming that you run the script with the same account both from the command line and from cron, but please confirm the user(s) and their UID with id on the command line and pasting in the relevant line from crontab -l or copied from /etc/cron.d/whatever-you-called-it, or that could be /etc/cron.hourly/whatever or other variation, but I presume you know which one is is written in. It would be useful to know.

For differences between your login shell and the environment provided when cron runs something, can you do this:-
Code:
env>/tmp/login_env.txt
set>/tmp/login_set.txt

Then write yourself a small script containing:
Code:
env>/tmp/cron_env.txt
set>/tmp/cron_set.txt

.... and run it with cron, just the once will be fine, but don't use at because that will use your login shell environment too.

Did you take Corona688's suggestion to create ssh-keys in post 4? This would simplify your script somewhat.

Did you take Yoda's suggestion in post 6? Can you paste the error file it wrote? (in CODE tags) Perhaps run with and without the leading ~/.profile ; so we can see if the difference is useful.


If you answer the questions raised, then people will be happy to help, but without this information we're just guessing blind. We do not know what your server has set up.


I hope that this helps you help us to help you,
Robin

Last edited by rbatte1; 02-12-2018 at 01:40 PM..
# 12  
Old 02-14-2018
Hi all, please below the output

Code:
[gilles@servername RA_Folder]$ uname -a
Linux servername 2.6.32-279.el6.x86_64 #1 SMP Wed Jun 13 18:24:36 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
[gilles@servername RA_Folder]$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
gilles     47041 47035  0 04:28 pts/0    00:00:00 -bash
gilles     47299 47041  2 04:52 pts/0    00:00:00 ps -f
[gilles@servername RA_Folder]$ cd /etc/cron.d/
[gilles@servername cron.d]$ ls -lrt
total 16
-rw-r--r--. 1 root root 459 Mar 17  2010 sa-update
-rw-r--r--. 1 root root 113 Mar  4  2011 0hourly
-rw-r--r--. 1 root root 235 Mar 28  2012 sysstat
-rw-r--r--. 1 root root 108 Apr 26  2012 raid-check

---------- Post updated 02-13-18 at 12:03 AM ---------- Previous update was 02-12-18 at 11:49 PM ----------

Code:
more login_env.txt
HOSTNAME=servername
SHELL=/bin/bash
TERM=xterm
HISTSIZE=2000
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
HISTFILESIZE=1000000000
USER=gilles

MAIL=/var/spool/mail/gilles
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/export/home/gilles/bin
PWD=/etc/cron.d
LANG=en_US.UTF-8
KDE_IS_PRELINKED=1
MODULEPATH=/usr/share/Modules/modulefiles:/etc/modulefiles
LOADEDMODULES=
KDEDIRS=/usr
HISTCONTROL=ignoredups
SHLVL=1
HOME=/export/home/gilles
LOGNAME=gilles
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh
MODULESHOME=/usr/share/Modules
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
HISTTIMEFORMAT=%F %T %t
module=() {  eval `/usr/bin/modulecmd bash $*`
}
_=/bin/env
OLDPWD=/tmp

the content of login_set.txt

Code:
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=168
CVS_RSH=ssh
DIRSTACK=()
EUID=500
GROUPS=()
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTFILE=/export/home/gilles/.bash_history
HISTFILESIZE=1000000000
HISTSIZE=2000
HISTTIMEFORMAT='%F %T %t'
HOME=/export/home/gilles
HOSTNAME=servername
HOSTTYPE=x86_64
ID=500
IFS=$' \t\n'
KDEDIRS=/usr
KDE_IS_PRELINKED=1
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=44
LOADEDMODULES=
LOGNAME=gilles

---------- Post updated at 12:10 AM ---------- Previous update was at 12:03 AM ----------

Yoda and Corona suggestions did not work, because the file log is not being created, and the file is not being sent to the remote server. As i said my issue is to get the file to the remote server using cron. When using the script suggested by Corona there's no output at all. Even when i add this
Code:
23 05 * * * ~/.profile; /export/home/gilles/RA_Folder/ra_scripts.sh  > /export/home/gilles/RA_Folder/log 2> /export/home/gilles/RA_Folder/log

, i have no file log created

---------- Post updated at 05:42 PM ---------- Previous update was at 12:10 AM ----------

Hello,

The given information is not enough?or is something i'm not doing right?can someone tell me something?

---------- Post updated 02-14-18 at 03:20 AM ---------- Previous update was 02-13-18 at 05:42 PM ----------

Here is the output in the log

Code:
[gilles@servername RA_Folder]$ more log
spawn sftp username@17.2.10.14
Connecting to 17.2.10.14...
username@17.2.10.14's password:
sftp> cd /path1/path2/path3
sftp> put /export/home/gilles/RA_Folder/file_*.csv
Uploading /export/home/gilles/RA_Folder/file_*.csv to /path1/path2/path3/file_20180214011501.csv


Last edited by gillesi; 02-13-2018 at 01:19 AM..
# 13  
Old 02-16-2018
Not ~/.profile, . ~/.profile The first . means source, "load this file". Its your profile and should set up PATH properly etc for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Crontab

Hello, I have a situation with crontab command on AIX , when i run a script with crontab i receive empty output , when i run it manually i have information in it,beside the empty output ,SAS log is empty too, i don't know why isn't creating it , could someone told me how could verify crontab... (3 Replies)
Discussion started by: rimob
3 Replies

2. Shell Programming and Scripting

Needed SFTP script from windows to UNIX server and from UNIX to windows server(reverse SFTP)

hi guys, i need a script to sftp the file from windows to unix server ....(before that i have to check whether the file exists in the windows server or not and again i have to reverse sftp the files from unix to windows server..... regards, Vasa Saikumar. (13 Replies)
Discussion started by: hemanthsaikumar
13 Replies

3. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

4. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

5. Programming

SFTP fails from crontab but works from terminal

Dear community, I'm driving crazy with a strange issue. I have a simple script to transfer a file to a remote system:#!/bin/bash echo "put /tmp/server.log" > /tmp/server1_transfer.sftp sftp -b /tmp/server1_transfer.sftp user@10.99.1.2:Between client and server there is a SSH KEY, so if I run... (15 Replies)
Discussion started by: Lord Spectre
15 Replies

6. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

7. AIX

Crontab

How can i schedule a batch job/script to run every minute every day? Thanks (3 Replies)
Discussion started by: mrn6430
3 Replies

8. AIX

SFTP Failed---Request for subsystem 'sftp' failed on channel 0

Hi, While I am trying SFTP my machine to another unix machine , it was working fine till 10 min back. But now i am getting the below error "Request for subsystem 'sftp' failed on channel 0" Could you please someone help me to solve or analyise the root cause... Cheers:b:, Mahiban (0 Replies)
Discussion started by: mahiban
0 Replies

9. UNIX for Advanced & Expert Users

Crontab in HP-UX

Hello! I have a problem on adding a new crontab. Currently we have a running jobs scheduled on a crontab and they are all successfully running. However, when adding or inserting a new scheduled job, the script doesn't work. Is there any limitations on this? Please advise. Thank you very... (13 Replies)
Discussion started by: Tris
13 Replies

10. UNIX for Dummies Questions & Answers

Crontab help

I have made changes to crontab using: crontab -e I then save them with :wq but the changes aren't being seen. The file mod date has not changed. What am I doing wrong? (2 Replies)
Discussion started by: ssmiths001
2 Replies
Login or Register to Ask a Question