[clug] A "How to" SSH question: copy file (scp/sftp) to remote system, then trigger a handler on remote.

Chris Smart clug at christophersmart.com
Thu Jan 21 10:15:04 UTC 2016


On 21/01/16 16:45, steve jenkin wrote:
> I’m looking to move files to a remote system with ssh and have the copy trigger something to deal with them.
> 

Does it have to be ssh? Have you looked into rsyncd?

> scp “uses ssh for data transfer”, then sets owner/group, permissions & timestamp of the remote file.
> <https://en.wikipedia.org/wiki/Secure_copy>
> 

rsync should take care of this for you, and you can use --chmod option.

> It’s possible to code scp as an ssh command, like:
> 	ssh remote ‘cat >tmp;chmod user.group tmp;touch +hhmmss tmp;mv tmp destfile’ <srcfile
> 
> I’d like to trigger a command on the remote system to do something with the recently arrived file, i.e. “pick it up” and process it in some way.
> 

rsyncd should allow you do do something like this via the post-xfer exec
option:
https://download.samba.org/pub/rsync/rsyncd.conf.html

> In the above pseudo-code above:
> 	ssh remote ‘cat >tmp;chmod user.group tmp;touch +hhmmss tmp;mv tmp destfile;exec /path/to/command destfile&’ <srcfile
> 
> On some systems, I’ve implemented an ‘incoming’ directory with a background daemon that regularly scans the directory and process the files one by one if found, otherwise sleeps till next scan.
> 
> Not what I want to do here.
> 
> An alternative is to execute first the processing command and just pipe the data to it via STDIN, passing in a
> Problem with this is:
>  a) organising temp dir for large files, and
>  b) overloading receiving/processing host with too many simultaneous calls
> 

rsync should handle those messy tmp files for you.

> Questions:
> 
> 1. has anyone seen this done? How?
>    There are going to be a lot of subtle errors and race-conditions, not the least, clashing filenames from simultaneous copies.
>     I’m not looking to rediscover them for myself. Life is too short :(
> 

rsync should handle this for you, too.

> 2. Is there a standard/semi-std SSH sub-system on the server side to perform this copy/trigger function?
>     Even non-standard, like creating automatic triggers on specific file-system activity. [Is that a thing in modern linux’s?]
> 

as mentioned above, rsyncd post-xfer exec option should do the trick.

> 3. For extra points, having delivered a file from my local system, I shouldn’t delete it before it’s saved & processed on the remote.
>      [in DB terms, a restartable transaction]
>    That implies either the sending SSH to hang around, waiting for a signal of some sort,
>      or some kind of return signal / file to be returned on next connection.
>    Also, I don’t ever want to get stuck in a loop of ‘send file, crash remote handler, resend file, crash remote, <till end of time>’
>    Seen that more than once :(
> 

rsync should handle this too with --remove-source-files option.

-c

-- 
  _
 °v°
/(_)\
 ^ ^



More information about the linux mailing list