Go Monitor directory Change applet.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Flag" "Log" "Os/exec" "Strings" "Sync" "Time" "Github.com/go-fsnotify/fsnotify") Var (sleeptime Intpath stringcmd Stringargs []string] func init () {flag. Intvar (&sleeptime, "T", "-t=30") flag. Stringvar (&path, "P", "./", "-p=filepath or Dirpath") flag. Stringvar (&cmd, "C", "", "-c=command") str: = flag. String ("A", "", "-a=" args1 args2 "') flag. Parse () args = strings. Split (*str, "")}func Main () {Watch, err: = Fsnotify. Newwatcher () if err! = Nil {log. PRINTLN ("Init Monitor Error:", err.) Error ()) return}if err: = Watch.add (path); Err! = Nil {log. PRINTLN ("ADD Monitor Path Error:", path) Return}var (cron bool = Falselock = new (sync). Mutex)) for {select {case event: = <-watch.events:log. Printf ("Monitor event%s", event.) String ()) if!cron {cron = Truego func () {T: = time. After (time. Second * time. Duration (sleeptime)) <-tif err: = Call (cmd, args ...); Err! = Nil {log. PRINTLN (Err)}lock. Lock () cron = Falselock. Unlock ()} ()}case err: = <-watch.errors:log. PrINTLN (ERR) Return}}}func call (Programe string, args ... string) error {cmd: = Exec.command (programe, args ...) BUF, err: = cmd. Output () if err! = Nil {return err}log. Printf ("\n%s\n", String (BUF)) return nil}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.