user1265101
user1265101

Reputation: 103

Detect SFTP file upload completed

I have an SFTP server (debian ssh server) where remote users are uploading once a while one big file into a specific directory.

My challenge is that I need to detect:

1) that a new file is present.

2) that the upload process/transmission is done.

What would be the best way to do this? Maybe there is already some tools available for this?

Logically thinking I can maybe detect in a first phase that a new file is present in the directory. And in a second phase then loop/wait until the user SFTP disconnects!? After that I could fire the next process...

Thx & cheers, Peter

Upvotes: 7

Views: 6049

Answers (3)

joker
joker

Reputation: 746

In my use case, it's enough to see if any users are currently connected through SFTP. If so, I'll simply consider all files locked.

This can be done (in Linux/OpenSSH) through a ps -ef command in the way described here.

Cheers!

Upvotes: 0

Tushar Mishra
Tushar Mishra

Reputation: 1400

I've had a similar situation. Although I used JSCH but the approach might help.

You may upload the file with some temporary extension. e.g. bigFile.txt.temp . Once the file is uploaded, rename it to the desired file name i.e bigFile.txt and delete the temporary file. JSCH ChannelSftp. In JSCH, rename does the job of keeping only one file. So you need not explicitly delete the temporary file.

Before uploading any newer version of the file, check for the existence of the temporary file - bigFile.txt.temp. If it exists, that means the file is currently getting uploaded. set a reply code or wait for the session to disconnect.

Upvotes: 0

Joni
Joni

Reputation: 111239

For problem 1) you can use the inotify API to watch the directory for changes. You don't say which programming language you use. In Perl you would use Linux::Inotify, in Python pynotify.

For problem 2), one way is to wait until the ssh session disconnects, another is to also use inotify to watch for file close notifications.

Upvotes: 3

Related Questions