Skip to content

Linux Crontab

Sending files to a remote SFTP on a schedule

Section titled “Sending files to a remote SFTP on a schedule”

Let’s say we’d like to send all .zip files from /data folder
to SFTP server 10.0.0.24 using monitor02 user and pass password.
Moreover we’d like those files to be send automatically every 10 minutes.

  1. First we need to test the connection
Terminal window
$ connect test sftp -h 10.0.0.24 -l monitor02 -p -
2024/11/27 13:32:37 INFO License valid to: 2026-04-13 18:06:28
2024/11/27 13:32:37 INFO Licensed for: Ultimate
Enter Password:
2024/11/27 13:32:41 INFO Testing connection to sftp://[email protected]
The key of 10.0.0.24:22 is unknown. Do you want to add this key to known_hosts (y/n): y
2024/11/27 13:32:44 INFO Connection successful
  1. Then we need to store the password in a file and rerun the connection test
Terminal window
$ echo "pass" > /apps/job1_pass
$ connect test sftp -h 10.0.0.24 -l monitor02 --password-file /apps/job1_pass
2024/11/26 22:19:02 INFO License valid to: 2026-04-13 18:06:28
2024/11/26 22:19:02 INFO Licensed for: Ultimate
2024/11/26 22:19:02 INFO Testing connection to sftp://[email protected]
2024/11/26 22:19:04 INFO Connection successful
$ cat /apps/job1_pass
ENC:LyHTac9EUBH8W4WO1RvyIB3m2pg32c5EnkUcTRefVSk=
  1. Then we need to prepare the connect sftp send command
Terminal window
$ connect send sftp \
-h 10.0.0.24 \
-l monitor02 \
--password-file /apps/job1_pass \
-s /data \
--file-mask "*.zip" \
--delete \
--parallel 3 \
--flag /tmp/job1 \
--batch \
--no-color
2024/11/26 22:31:02 INFO License valid to: 2026-04-13 18:06:28
2024/11/26 22:31:02 INFO Licensed for: Ultimate
2024/11/26 22:31:02 INFO Using flag file: /tmp/job1
2024/11/26 22:31:02 INFO Sending files to sftp://[email protected]:
2024/11/26 22:31:05 INFO Transferring file=/data/file3.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO File deleted file=/data/file3.zip sftpSessionID=2
2024/11/26 22:31:05 INFO File transferred successfully bytesTransferred=1048576 duration=98ms transferRate=10.12MB/s file=/data/file3.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO Transferring file=/data/file1.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO File deleted file=/data/file1.zip sftpSessionID=2
2024/11/26 22:31:05 INFO File transferred successfully bytesTransferred=1048576 duration=91ms transferRate=10.98MB/s file=/data/file1.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO Transferring file=/data/file2.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO File deleted file=/data/file2.zip sftpSessionID=2
2024/11/26 22:31:05 INFO File transferred successfully bytesTransferred=1048576 duration=28ms transferRate=34.87MB/s file=/data/file2.zip sftpSessionID=2 targetFolder=""
2024/11/26 22:31:05 INFO Disconnected sftpSessionID=3 target=sftp://[email protected]:
2024/11/26 22:31:05 INFO Disconnected sftpSessionID=1 target=sftp://[email protected]:
2024/11/26 22:31:05 INFO Disconnected sftpSessionID=2 target=sftp://[email protected]:
2024/11/26 22:31:05 INFO Removing flag file: /tmp/job1
  1. Lastly we need to add the send command to crontab.

Let’s open the crontab editor with crontab -e and add desired command

Terminal window
*/10 * * * * /usr/bin/connect send sftp -h 10.0.0.24 -l monitor02 --password-file /apps/job1_pass -s /data --file-mask "*.zip" --delete --parallel 3 --flag /tmp/job1 --batch --no-color

And then save and exit the editor.

To log the execution of the job to a file add >>/apps/job1.log at the end of the command and also add --no-color flag.

Terminal window
*/10 * * * * /usr/bin/connect send sftp -h 10.0.0.24 -l monitor02 --password-file /apps/job1_pass -s /data --file-mask "*.zip" --delete --parallel 3 --flag /tmp/job1 --batch --no-color >>/apps/job1.log