跪求大哥们,很急。
脚本要监听一个绝对路径下文件是否被更新,如果更新了,就退出当前脚本,并开始执行下一个脚本。都是TCL脚本
监测/root/a.log是否被修改,被修改则调用脚本/root/a.sh
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <sys/select.h>
#include <errno.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
#define TRUE 1
#define PATH "/root/a.log"
int main()
{
int wd = 0;
int fd = 0;
int ret = 0;
char buf[BUF_LEN] = {0};
char *script_path = "/root/a.sh";
char *p;
struct inotify_event *event;
pid_t pid;
fd = inotify_init();
if(fd < 0)
{
// printf("inotify_init error!\n");
return 0;
}
// printf("fd=%d\n",fd);
wd = inotify_add_watch(fd,PATH,IN_MODIFY);//don't use IN_ALL_EVENTS
if(wd < 0)
{
// printf("inotify_add_watch error!\n");
return 0;
}
// printf("wd=%d\n",wd);
while(TRUE)
{
ret = read(fd,&buf,sizeof(buf));
if(ret < 0)
{
// printf("read error!\n");
return 0;
}
for(p = buf;p < buf + ret;)
{
event = (struct inotify_event *) p;
if(event->mask & IN_MODIFY)
{
// execl("/bin/sh","sh","-pc",script_path,NULL);
pid = fork();
if(pid < 0)
{
return 0;
}
else if(pid == 0)
{
// printf("child\n");
execl("/bin/sh","sh","-pc",script_path,NULL);
exit(0);
}
else
{
wait();
// printf("father\n");
}
}
p += sizeof(struct inotify_event) + event->len;
}
}
inotify_rm_watch(fd,wd);
close(fd);
return 0;
}
/root/a.sh
echo "file changed"
顶!
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y