[IRCServices Coding] Crontab Script

V13 v13 at it.teithe.gr
Sun Jun 2 05:37:03 PDT 2002


 Andrew, I suppose you can include/use the attached code (or any modified 
version of it) in services, without causing portability problems. It will 
solve the pidfile problem and the need of a keepalive script. It just creates 
and locks the pidfile using an advisory lock. This way it stays locked while 
services are running. OS will unlock it if services die. I'm currently using 
it in some progs of mine without any problems. It seems to compile and work 
on linux, freebsd, irix and solaris without problems. Since this is a 
modified version of the one i'm using, this may be incorect.

 There are 3 functions. pidfile_islocked(), pidfile_dolock(), 
pidfile_dounlock(). That check for a lock, lock and unlock the pidfile. They 
also create and remove the pidfile. I'm also using pidfile_islocked to get 
the current pid of the running copy. This way I can kill the running copy 
when the program is run with (for exampe) '-k' by 

pid=pidfile_islocked(PIDFILE);
if (pid)
	kill(pid,SIGTERM);

 In case of fork(), you have to call pidfile_islocked() once before the fork() 
is done and pidfile_dolock() after the fork, so that the lock will not go 
away. I'm attaching an example program too.

 There exists a race condition, but i prefer this from the one that exist in 
every keepalive script.

<<V13>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: example.c
Type: text/x-csrc
Size: 301 bytes
Desc: not available
Url : ../attachments/20020602/3c6a97c2/example.c
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pidfile.c
Type: text/x-csrc
Size: 2338 bytes
Desc: not available
Url : ../attachments/20020602/3c6a97c2/pidfile.c