Shell script running as a daemon?

James McDonald james
Wed Jul 18 19:07:33 PDT 2007


Mateus Interciso wrote:
> Hello, I'm planing on making a (hopefully) simple shell script that act 
> as a daemon. This particular shell script should keep watching a folder, 
> and wenever a new txt file(all files that arrive on that folder are txt) 
> arrives, it should grep trough it, and take some actions.
> My question is if it's possible to make this kind of daemon?
> Any suggestions?Feebacks?
>
> Thanks for your help.
>
> Zarnick
>
>   
This is a handy daemonized perl script which has a nice loop in it which 
you can configure for every x seconds.


#!/usr/bin/perl
use POSIX qw(setsid);
chdir '/' or die "cannot change to /:$!";
open STDIN,'/dev/null' or die "cannot read stdin:$!";
open STDOUT ,'>>/dev/null' or die "cannot write to stdout:$!";
open STDERR ,'>>/dev/null' or die "cannot write to stderr:$!";
defined(my $pid=fork) or die "cannot fork process:$!";
print "$pid\n";
exit if $pid;
setsid;
umask 0;
while(1) {
    sleep(5);
   #write ur event checking code here
   print "Hello World\n";
}



More information about the Linux-users mailing list