{"id":4245,"date":"2011-10-11T08:08:37","date_gmt":"2011-10-11T12:08:37","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/?p=4245"},"modified":"2011-10-11T08:08:37","modified_gmt":"2011-10-11T12:08:37","slug":"fsniper","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/fsniper\/","title":{"rendered":"FSniper"},"content":{"rendered":"<p>fsniper is a tool that monitors a given set of directories for new or modified files. Then, based on the file&#8217;s type or name, it invokes a script to be run on that file. Directories are monitored using inotify, instead of simply continuously polling them for changes. Common uses include making a single download directory for all things from a Web browser and having semi-intelligent scripts figure out what to do with those files. You write the scripts yourself.<\/p>\n<p>fsniper uses inotify to watch for when a file is closed after being written to. This means utilities such as touch will cause the event to trigger.<\/p>\n<p>fsniper can watch any number of directories for any number of pattern matches on the files.<\/p>\n<p>Download the <a href=\"http:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2011\/10\/sniper.tar.gz\">sniper.tar.gz here<\/a> (may be old)<br \/>\nLatest version of <a href=\"http:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2011\/10\/fsniper-1.3.1.tar.gz\">fsniper-1.3.1.tar.gz<\/a><\/p>\n<p><!--more--><\/p>\n<p><strong>Example Configuration File<\/strong><\/p>\n<p>Let&#8217;s jump into an example configuration file.<br \/>\n<code><br \/>\nwatch {<\/p>\n<p>    ~\/tmp\/s {<br \/>\n        image\/* {<br \/>\n            handler = echo<br \/>\n        }<\/p>\n<p>        *.extension {<br \/>\n            handler = .\/foo1 a %% b<br \/>\n            handler = mv %% arc-%%.txt<br \/>\n        }<\/p>\n<p>        \/.*regex.*\/ {<br \/>\n            handler = .\/foo2 a %% b<br \/>\n        }<\/p>\n<p>        *\/* {<br \/>\n            handler = echo 2<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<br \/>\n<\/code><br \/>\n&nbsp;<br \/>\nTo start defining directories to watch, you must have a watch block. Each block underneath a watch block is a directory that fsniper monitors for files that are closed.<\/p>\n<p>In this configuration, we are watching only one dir, ~\/tmp\/s. Underneath that directory block, we are watching for 4 different patterns of files. The first pattern, image\/*, looks for files that match a mime-type pattern. Any image file will go into this block. The handler is echo, a standard system command that writes to stdout. fsniper looks for &#8220;%%&#8221; in the handler line. The name of the file that was just closed is substituted in for the %%. If no %% is found in the handler line, it appends it on the end.<\/p>\n<p>The second pattern, *.extension, matches a shell glob for any file with the .extension extension. This pattern has two handlers. .\/foo1 is a custom script we write. If that script returns a return code that is not 0 or 2, it moves on to the next handler. See the Handler Return Codes section.<\/p>\n<p>The third pattern, \/.regex.\/, matches the file name against a regular expression pattern. PCRE support must be compiled in for this pattern to work.<\/p>\n<p>The fourth pattern, \/, matches any file (by mime-type wildcards). You could also use *, which would be a file glob.<\/p>\n<p><strong>Configuration<\/strong>: This section details all the available configuration options.<\/p>\n<p><strong>Matching Patterns<\/strong> &#8211; There are three types of patterns that fsniper can match against.<\/p>\n<p><strong>Mime-Types<\/strong><br \/>\nfsniper can match mime-types, like text\/plain, image\/png, etc. You can use the file utility to figure out the mime-type, use file -ib to get the mime-type that fsniper sees.<br \/>\nYou can also have wildcards on each side of the mime-type, so image\/* would match all images, etc.<\/p>\n<p><strong>Shell Globs<\/strong><br \/>\nThe most common pattern would be shell globs, like .txt or .html. This matches any shell like file pattern.<\/p>\n<p><strong>Regular Expressions<\/strong><br \/>\nfsniper can be compiled with PCRE support to allow more complicated regular expression matching. Regex patterns are enclosed in forward slashes. Consult the PCRE documentation for the flavor of regular expressions supported. Remember, the entire filename (including path) is matched against the regex.<\/p>\n<p><strong>Handlers<\/strong><br \/>\nThe handler scripts do not always have to be custom written. You can use the %% replacement to do some script-like behavior. For example, this handler line:<\/p>\n<p><code>handler = tar cvz archive-%%.tar.gz %%<\/code><br \/>\nThis will create a tar\/gz file of the files that match the file pattern.<\/p>\n<p>Often, custom scripts are easier if they need to perform some complicated tests, or indicate that they will delay.<\/p>\n<p><strong>Handler Search Paths<\/strong><br \/>\nSince fsniper uses the shell to execute handlers, the shell&#8217;s PATH environment variable is respected to find the handlers. fsniper automatically prepends ~\/.config\/fsniper\/scripts into PATH, so it is common to place custom scripts in that directory.<\/p>\n<p><strong>Handler Return Codes<\/strong><br \/>\n0 : the handler handled this file.<\/p>\n<p>2 : the handler will handle this event, but not right now. Retry again later.<\/p>\n<p>other: this handler can not handle this file.<\/p>\n<p><strong>Delays<\/strong><br \/>\nIt is possible that a handler can handle a file, but not at this moment. A common example would be that the handler is supposed to scp a file to a remote server, but that server is currently not available. The handler script returns a 2 to indicate that it can handle it, just not right now. fsniper detects this return code and sleeps for a period of time, then retries the handler.<\/p>\n<p>There are two parameters that control delay handling. These parameters are placed in the configuration file on the top level, aka the same level as the watch block.<\/p>\n<p>delay_time : the amount of time, in seconds, to wait before trying the handler again. If not specified, it defaults to 300, which is 5 minutes.<\/p>\n<p>delay_repeats : the number of repeats it should try before giving up. This can be 0, meaning infinite retries.<\/p>\n<p><strong>fsniper Files<\/strong><\/p>\n<p>By default, fsniper tries to load its configuration from ~\/.config\/sniper\/config. You can override this by specifying a configuration file on the command line.<\/p>\n<p>fsniper keeps a log file in ~\/.config\/sniper\/log. You can use tail -f to monitor fsniper&#8217;s output.<\/p>\n<p><strong>Command Line Options<\/strong><br \/>\n<code><br \/>\n--help<br \/>\nPrints this help text.<br \/>\n--version<br \/>\nPrints version information.<br \/>\n--daemon<br \/>\nRun as a daemon.<br \/>\n--verbose<br \/>\nTurns on debug text.<br \/>\n--sync<br \/>\nSync mode (for debugging).<br \/>\n--log-to-stdout<br \/>\nLog to stdout alongside the usual log file.<br \/>\n<\/code><\/p>\n<p>&nbsp;<br \/>\n<strong>Init Script<\/strong><br \/>\nHere is an init script from From <a href=\"http:\/\/blog.secaserver.com\/2011\/09\/linux-init-script-fsniper\/\" target=\"_blank\" rel=\"noopener noreferrer\">blog.secaserver.com<\/a><\/p>\n<p>1. Create the init script in init.d directory:<br \/>\n<code>$ touch \/etc\/init.d\/fsniper<\/code><\/p>\n<p>2. Using your favourite text editor, open the file \/etc\/init.d\/fsniper and paste following code:<br \/>\n<code><br \/>\nexport HOME=\/root<\/p>\n<p>case \"$1\" in<br \/>\nstart)<br \/>\necho -n \"Starting Fsniper: \"<br \/>\n\/usr\/local\/bin\/fsniper --daemon<br \/>\necho -e \"... [ \\e[00;32mOK\\e[00m ]\"<br \/>\n;;<br \/>\nstop)<br \/>\necho -n \"Shutdown Fsniper: \"<br \/>\nkill -9 `ps aux | grep \"fsniper --daemon\" | grep -v grep | awk {'print $2'}`<br \/>\necho -e \"... [ \\e[00;32mOK\\e[00m ]\"<br \/>\n;;<br \/>\nrestart)<br \/>\n$0 stop<br \/>\nsleep 1<br \/>\n$0 start<br \/>\n;;<br \/>\n*)<br \/>\necho \"Usage: `basename $0` start|stop|restart\"<br \/>\nexit 1<br \/>\nesac<\/p>\n<p>exit 0<br \/>\n<\/code><\/p>\n<p>3. Change the permission to executable:<\/p>\n<p><code>$ chmod 755 \/etc\/init.d\/fsniper<\/code><\/p>\n<p>Done! You can now use following command to start fsniper:<br \/>\n<code><br \/>\n$ service fsniper start<br \/>\n$ \/etc\/init.d\/fsniper start<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>fsniper is a tool that monitors a given set of directories for new or modified files. Then, based on the file&#8217;s type or name, it invokes a script to be run on that file. Directories are monitored using inotify, instead of simply continuously polling them for changes. Common uses include making a single download directory&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/fsniper\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4245","post","type-post","status-publish","format-standard","hentry","category-linux-apps"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>FSniper - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/fsniper\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"FSniper - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"fsniper is a tool that monitors a given set of directories for new or modified files. Then, based on the file&#8217;s type or name, it invokes a script to be run on that file. Directories are monitored using inotify, instead of simply continuously polling them for changes. Common uses include making a single download directory... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/fsniper\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-11T12:08:37+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"FSniper\",\"datePublished\":\"2011-10-11T12:08:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/\"},\"wordCount\":879,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"Apps\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/\",\"name\":\"FSniper - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2011-10-11T12:08:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/fsniper\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"FSniper\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"FSniper - Linux Shtuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/g33kinfo.com\/info\/fsniper\/","og_locale":"en_US","og_type":"article","og_title":"FSniper - Linux Shtuff","og_description":"fsniper is a tool that monitors a given set of directories for new or modified files. Then, based on the file&#8217;s type or name, it invokes a script to be run on that file. Directories are monitored using inotify, instead of simply continuously polling them for changes. Common uses include making a single download directory... Read More","og_url":"https:\/\/g33kinfo.com\/info\/fsniper\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2011-10-11T12:08:37+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/fsniper\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/fsniper\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"FSniper","datePublished":"2011-10-11T12:08:37+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/fsniper\/"},"wordCount":879,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["Apps"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/fsniper\/","url":"https:\/\/g33kinfo.com\/info\/fsniper\/","name":"FSniper - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2011-10-11T12:08:37+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/fsniper\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/fsniper\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/fsniper\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"FSniper"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/4245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=4245"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/4245\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=4245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=4245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=4245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}