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.
- First we need to test the connection
$ 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:282024/11/27 13:32:37 INFO Licensed for: UltimateEnter Password:The key of 10.0.0.24:22 is unknown. Do you want to add this key to known_hosts (y/n): y2024/11/27 13:32:44 INFO Connection successful
- Then we need to store the password in a file and rerun the connection test
$ echo "pass" > /apps/job1_pass$ connect test sftp -h 10.0.0.24 -l monitor02 --password-file /apps/job1_pass2024/11/26 22:19:02 INFO License valid to: 2026-04-13 18:06:282024/11/26 22:19:02 INFO Licensed for: Ultimate2024/11/26 22:19:04 INFO Connection successful$ cat /apps/job1_passENC:LyHTac9EUBH8W4WO1RvyIB3m2pg32c5EnkUcTRefVSk=
- Then we need to prepare the
connect sftp send
command
$ 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-color2024/11/26 22:31:02 INFO License valid to: 2026-04-13 18:06:282024/11/26 22:31:02 INFO Licensed for: Ultimate2024/11/26 22:31:02 INFO Using flag file: /tmp/job12024/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=22024/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=22024/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=22024/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 Removing flag file: /tmp/job1
- Lastly we need to add the send command to crontab.
Let’s open the crontab editor with crontab -e
and add desired command
*/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.
*/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