LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-26-2017, 02:45 AM   #1
scaela
LQ Newbie
 
Registered: Jul 2017
Posts: 1

Rep: Reputation: Disabled
Post creation of a shell script for an ubuntu ftp server that runs every time a zip file is uploaded and unzips them.


i need help creating a shell script that is triggered by the addition of a zip file to the ftp directory.
this script must unzip all the added .zip files to the directory and then delete the .zip itself
 
Old 07-26-2017, 03:03 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Try installing and using the incron package.
 
Old 08-04-2017, 11:18 AM   #3
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
That can get a bit tricky. You can schedule a cron job to run every five minutes to check for zip files and have it unzip them. Since you're deleting the zip file you won't have to worry about overwriting or unzipping massive amounts of accumulated files.

However, the important part is ensuring that the file transfer is complete before unzipping and deleting. You could use something like this.

WARNING: I wrote this off the top of my head. It's intended to give a general idea of what's necessary. Use at your own risk.

Code:
#! /bin/ksh
PWD=`pwd`
localip="IP address of your FTP server"
#
# File zip files in the FTP directory.
files=`find /ftp/ -name -type f "*.zip -printf  "%h/ %f\n"
#
# If any files exist, check for active FTP connections to the server.
if [ ! -z $files ]
then
  files=session_ips=`netstat -A inet -tn | grep "$localip:21" | \
                     grep "ESTABLISHED" | awk '{print $5}' | awk -F":" '{print $1}' | \
                     sort -ut. -k1,1n -k2,2n -k3,3n -k4,4n`
  #
  # If there are no active connections read the file list one by one
  # Change to the directory it's stored in, unzip and delete it, then change back to the directory it started in.
  if [ ! -z "$session_ips" ]
  then
    echo "$files" | while read file
    do
      WDir=`echo "$file" | awk '{print $1}'`
      WFile=`echo "$file" | awk '{print $NF}'`
      cd "$WDir"
      unzip "$WFile"
      rm "$WFile"
      cd "$PWD"
    done
  fi
fi
Hope this helps MPH426
 
Old 08-04-2017, 11:43 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
As posted by tshikose incron uses inotify. incron/inotify can trigger a script when a file is written to a specific directory so constantly checking for an active FTP connection or the existence of zip files is not required. However, it does show there are usually many ways to accomplish the same task.

Still waiting for the OP to reply to the thread...
 
Old 08-04-2017, 12:32 PM   #5
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
As posted by tshikose incron uses inotify. incron/inotify can trigger a script when a file is written to a specific directory so constantly checking for an active FTP connection or the existence of zip files is not required. However, it does show there are usually many ways to accomplish the same task.

Still waiting for the OP to reply to the thread...
inotify may work for smaller installations, but I found (in my case anyway) that it didn't scale to well. Our FTP server has about 350 active (Not necessarily connected) users at any given time and keeping track with it was a bit difficult. There are over 1000 total users. The more that are active the harder to track files and the more it taxed the CPU. I also was getting multiple file close statements for the same file making it difficult to tell when the file was really closed. Some would show up minutes later.

I ended up catching the FTP connection start and stop times using inet looking for active connections. I captured the start and end times. Then it was just a matter of searching the FTP log file for transferred files during that time frame. It's been working rock solid for about seven years now. However, I would think that inotify is much better now. Maybe I should revisit it.
 
1 members found this post helpful.
Old 08-04-2017, 01:08 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
Thanks for the clarification.
 
Old 08-04-2017, 01:17 PM   #7
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Thanks for the clarification.
Thank You for the reminder! Seven years is a LOOOONG time. Back then I had to write and compile things pretty much from scratch. I'm hoping that incron will have taken care of the issues I had.
 
Old 08-04-2017, 02:11 PM   #8
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
Here's the part that made me stop using it.

From incrond man

Quote:
BUGS
incrond is currently not resistent against looping. Recursive monitoring (whole subtrees) has not been implemented yet.
I'd be leery of monitoring entire FTP subtrees on any but the smallest instance of an FTP server.

I know in my case it's really not an option. With 1000+ users jailed to their own home directory, each having an incoming and outgoing directory, incron chokes. I don't think having 2000+ instances (I only need to monitor the incoming and outgoing directories for each user) would be a good idea either. LOL I think my instance is a bit beyond the scope of what incron is meant to do. But, I'm glad I checked.

I can't pair it down to only monitory active users because inactive users are automatically reactivated when files are put up from the inside for them.

It's shame, it really would simplify my programming.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to extract file which is uploaded to FTP server hruday Linux - Server 3 09-25-2016 07:39 AM
Shell Script to FTP Zip File mpfeiffer311 Programming 11 10-21-2013 08:14 AM
shell script required to get the mail after files are uploaded to ftp siddabathuni Programming 12 10-01-2009 08:08 AM
Upload file to ftp server -vsftp- but can not delete or change the file once uploaded murattas6 Linux - Server 2 06-26-2009 06:00 AM
How to keep the original time of files uploaded to FTP server wondial Linux - Software 1 10-26-2004 10:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration