# Sftp in a crontab

**URL:** <https://community.unix.com/t/sftp-in-a-crontab/370827>\
**Category:** Shell Programming and Scripting\
**Created:** [February 8, 2018, 12:51pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827 "2018-02-08T12:51:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 8, 2018, 12:51pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/1 "2018-02-08T12:51:40Z")

</div>

Hello All,

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

```nohighlight
#!/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

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

```

Can anyone give me a hint?

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 8, 2018, 1:16pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/2 "2018-02-08T13:16:52Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 8, 2018, 2:19pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/3 "2018-02-08T14:19:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 8, 2018, 2:35pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/4 "2018-02-08T14:35:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 11, 2018, 4:26pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/5 "2018-02-11T16:26:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 11, 2018, 4:33pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/6 "2018-02-11T16:33:17Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 11, 2018, 6:11pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/7 "2018-02-11T18:11:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 11, 2018, 8:28pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/8 "2018-02-11T20:28:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 12, 2018, 12:11pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/9 "2018-02-12T12:11:32Z")

</div>

Hello

any hint here please

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 12, 2018, 12:18pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/10 "2018-02-12T12:18:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [February 12, 2018, 12:31pm UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/11 "2018-02-12T12:31:39Z")

</div>

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

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

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

```

Then write yourself a small script containing:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![gillesi](https://community.unix.com/letter_avatar/gillesi/32/6_449c533f586939b6e5f246632a46421f.png) [@gillesi](https://community.unix.com/u/gillesi)\
**Post date:** [February 14, 2018, 3:20am UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/12 "2018-02-14T03:20:07Z")

</div>

Hi all, please below the output

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

```nohighlight
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

```nohighlight
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

```nohighlight
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

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

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 16, 2018, 10:45am UTC](https://community.unix.com/t/sftp-in-a-crontab/370827/13 "2018-02-16T10:45:51Z")

</div>

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