![]() Top > Linux > lsyncd イベントとシステムコマンド連携
lsyncd を使ったファイル変更をトリガーとする OS コマンド連携
Layer2 の機能を使う設定ファイル(プログラム)のサンプル
システムコマンド連携 サンプル要件
-- -- User configuration file for lsyncd. -- -- Layer2 example for os.execute. -- -- -- BEGIN Customize <rivus.jp> -- local ltrim = function(s, c) return string.match(s, "[^"..c.."].*") end local rtrim = function(s, c) return string.match(s, ".*[^"..c.."]") end local bracket = function(s) return "["..tostring(s) .."]" end local split = function(s, sep) local res={}; if (sep == nil) then return {s} end for p in string.gmatch(s, "([^"..sep.."]+)") do res[#res+1] = p end return res end -- log event info (debug) local event_info = function(e, ...) local msg = bracket(...) msg = msg.."*[evnet:" .. e.etype .."]" msg = msg.."[source Pathname:"..e.sourcePathname.. "]" msg = msg.."[target Pathname"..e.targetPathname.. "]" log("All others", msg) end -- MyCustom = {} MyCustom.new = function() local obj = {} -- -- variable -- obj.command_path = "/usr/bin" obj.include = "" obj.extensions = {} -- -- function -- -- contains (extension check) obj.contains = function(fname) if (#obj.extensions == 0) then return true end for i, m in ipairs(obj.extensions) do if (string.match(fname, ".+"..m.."$")) then return true end end return false end -- touch file obj.touch_r = function(e) -- event_info(e, "touch_r") if (e.isdir or not obj.contains(e.sourcePathname)) then log("All others", "skip touch ".. bracket(e.sourcePath)) else local binary = obj.command_path .. '/touch' local bin_opt = "--reference="..e.sourcePathname local file = e.targetPathname -- ************************* -- ** binary command here ** touch (binary ver.) spawn(e, binary, bin_opt, file) log("All others", "touch ".. bracket(file)) end end -- remove file obj.remove = function(e) -- event_info(e, "remove") if (e.isdir or not obj.contains(e.sourcePathname)) then log("All others", "skip remove ".. bracket(e.sourcePath)) else -- ********************* -- ** OS command here ** local ret = os.remove(e.targetPathname) log("All others","remove "..bracket(e.targetPathname).." = ".. bracket(ret)) end end -- move file obj.move = function(oe, de) -- event_info(oe, "move orig") -- event_info(de, "move dest") -- -- # mv origFile destFile local ofile_s = oe.sourcePathname -- sync{source=}/origFile local dfile_s = de.sourcePathname -- sync{source=}/destFile -- local ofile_t = oe.targetPathname -- sync{target=}/origFile local dfile_t = de.targetPathname -- sync{target=}/destFile -- local in_ext_o = obj.contains(ofile_s) -- extension check (original file) local in_ext_d = obj.contains(dfile_s) -- extension check (renamed file) if (oe.isdir or (in_ext_o == false and in_ext_d == false)) then -- rename dir or extension not match / # mv abc.mismatch_ext1 abc.mismatch_ext2 log("All others", "skip move ".. bracket(ofile_s).."->"..bracket(dfile_s)) elseif (in_ext_o and in_ext_d) then -- rename / # mv abc.txt abc.log -- ********************* -- ** OS command here ** local ret = os.rename(ofile_t, dfile_t) log("All others", "move ".. bracket(ofile_t).."->"..bracket(dfile_t).." = "..bracket(ret)) elseif (in_ext_d == false) then -- target file remove (extension no match) / # mv abc.txt abc.mismatch_ext -- ********************* -- ** OS command here ** local ret = os.remove(ofile_t) log("All others", "move -> remove "..bracket(ofile_t).." = "..bracket(ret)) elseif (in_ext_o == false) then -- target file touch (extension match) / # mv abc.mismatch_ext abc.txt local binary = obj.command_path .. "/touch" local bin_opt = "--reference="..dfile_s local file = dfile_t local shell_cmd = binary .. " "..bin_opt.." "..file -- ************************ -- ** shell command here ** touch (shell ver.) spawnShell(oe, shell_cmd) log("All others","move -> touch ".. bracket(dfile_t)) else log("Error","move -> nop ".. bracket(ofile_s).. " -> " .. bracket(dfile_s)) end end -- -- setttings obj.settings = function(mySettings) if (mySettings.command_path ~= nil) then local s = rtrim(mySettings.command_path, "/ ") obj.command_path = s; end if (mySettings.include ~= nil) then local s = ltrim(rtrim(mySettings.include, ", "),", ") s = string.gsub (s, "%.", "%%.") -- escape dotmark obj.include = s obj.extensions = split(s, ",") end log("Normal","command path = "..bracket(obj.command_path)) log("Normal","include filter = "..bracket(obj.include)) end -- -- event handler obj.myHandler={ delay = 0, maxProcesses = 1, -- onCreate = obj.touch_r, onModify = obj.touch_r, onMove = obj.move, onDelete = obj.remove, onStartup= 'if [ "$(ls -A ^source)" ];then rsync -a --include "*/" --exclude "*" ^source ^target;fi' } return obj end myCustom = MyCustom.new() -- -- MyCustom settings -- see also : MyCustom::settings -- myCustom.settings { command_path = "/bin/", include=".txt,.log,.doc" -- only file extension } -- END Customize -- -- -- lsyncd config -- -- settings { logfile = "/var/log/lsyncd.log", statusFile = "/tmp/lsyncd.stat", statusInterval = 10, } -- sync { myCustom.myHandler, source = "/tmp/my_src", target = "/tmp/my_trace", }
コマンドラインでの動作確認
Lua の感想
関連事項
|