Вопрос

I'm a Linux novice and this seems like an easily solvable problem but I am at a loss for a solution. I want to execute a script when a certain directory is written to. I plan on having some sort of watcher on that folder that will trigger the script to run. I've found entries online about using incron, watch or inotify. I think incron is what I want but there is absolutely no resource online that steps you through the process. Am I right in wanting to use incron? If so, how do I get it to work? In my incrontab I have

/home/myusername/folder IN_CLOSE_WRITE /var/www/runMe.sh

I've tried some variations of that but it looks like that is correct. I've been looking online for a few hours so I'm pretty frustrated. I created an incron.allow file which lists both the root and myusername, I started the incrond service and yet when the directory specified above gets written to the shell script does not execute. I'm on CentOS 6.5 and the directory is being written to by another machine ssh-ing in. Thank you for any help you provide.

Это было полезно?

Решение

Here is a Gtkmm example for detecting when a file or folder is created inside a directory:

#include <glibmm.h>
#include <giomm.h>
#include <iostream>

void file_changed(const Glib::RefPtr<Gio::File>& file, const Glib::RefPtr<Gio::File>&   otherFile, Gio::FileMonitorEvent event)
{
    if(event == Gio::FILE_MONITOR_EVENT_CREATED ){
        std::cout << "File created: " << file->get_path() << std::endl;
    }
}

int main(int argc, char* argv[]) {
    if(argc < 2) {
        std::cerr << "Usage: " << argv[0] << " directory_to_monitor" << std::endl;
        return 1;
    }
    Glib::init();
    Gio::init();

    Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create();

    Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(argv[1]);
    Glib::RefPtr<Gio::FileMonitor> monitor = file->monitor_directory();
    monitor->signal_changed().connect(sigc::ptr_fun(file_changed));
    loop->run();
    return 0;
}

Compile with

g++ -o test test.cpp $(pkg-config --cflags --libs glibmm-2.4 giomm-2.4)

Другие советы

You should read inotify(7) and consider using incron

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top