简体   繁体   中英

I want to run a bash script when an scp connection ends

I am building a server that accepts scp connections and then adds the files that have just been transferred to a git repository. I have tested the .bash_logout script, and it does not execute when the scpd session ends. Is there another hook that I can use to run a script when scpd exits?

Alternatively, I can use at(1) command in the .bashrc file to execute the script that does the work, say, 5 minutes after. I think, but I am not sure, that I need not worry about races because I think the files will come in infrequently.

Thank you

If scp always copies to the same place then this approach way work. It has the added bonus that copy via samba, CIFS etc... will work too.

Also, you could spawn the inotifywait shell script from the then nohup it so it executes after the shell exits. The other would be to start a process at boot time to monitor the directory using inotifywait and use that to add to git.

Of course, you could simply setup a git server, but I assume there is a good reason that is not desirable.

inotifywait

inotifywait can trigger upon changes to a directory.

It can wait for changes,additions,moves in a directory

  if inotifywait -e modify -e move -e create -r somedirectory ; then
   #  wait wile more activity occurs, up to 30 seconds
   while inotifywait -e modify -e move -e create -t 30 -r somedirectory ; do echo "waiting" ; done
   # do git add with the new files in somedirectory

   # delete the files from somedirectory

 fi

You could run this script as a cronjob:

#!/bin/sh
targetdir="whereever your files are going"
[ "$targetdir" -nt "~/flag" ] && your_script.sh
touch ~/flag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM