Sftp in a crontab

Hello All,

My below scripts is not working in crontab by when executed manually it works

#!/usr/bin/expect

spawn sftp username@ip_address
expect "password:"
send "passwd\n";
expect "sftp>"
send "cd /path/paht2/path3\n"
send "put /export/home/file_*.csv\n"
expect "sftp>"
send "bye\n"
interact

Here is the crontab

35 18 * * * /path/path2/path3/scripts.sh  > /export/home/log 2>&1

Can anyone give me a hint?

Try sourcing your .profile and see if it helps:-

35 18 * * * . ~/.profile; /path/path2/path3/scripts.sh  > /export/home/log 2>&1

Hi Yoda, thanks..but i had the same problem, it sends a file with 0 bytes.

It might work better if you used keys instead of using a brute-forcing tool to inject passwords. Then you could do scp /export/home/file_*.csv username@ip_address:/path/paht2/path3 and it would run without prompting or stalls.

Hello Corona, this is too is not helping. The file is not being sent to the remote path.

Redirect error also to log and check what is going on when the script runs in cron:-

35 18 * * * . ~/.profile; /path/path2/path3/scripts.sh  > /export/home/log 2>/export/home/log 

Please what does this do ~/.profile; when i put it in the cron it does not work at all

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.

Hello

any hint here please

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.

If you could post the output (in CODE tags) from these commands, that will give us a start:-

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:-

env>/tmp/login_env.txt
set>/tmp/login_set.txt

Then write yourself a small script containing:

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

Hi all, please below the output

[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 ----------

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

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

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

[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

Not ~/.profile, . ~/.profile The first . means source, "load this file". Its your profile and should set up PATH properly etc for you.