Operating System - HP-UX
1828667 Members
2196 Online
109984 Solutions
New Discussion

Re: How to create Daemon?

 
NUTANKUMAR
Occasional Contributor

How to create Daemon?

Is it possible to create Daemon? if yes, How?
For Eg. If I want to create the Daemon to check the Idle terminal more than 15min. and to kill the same, is it possible Please help.

thankx
5 REPLIES 5
RikTytgat
Honored Contributor

Re: How to create Daemon?

Hi,

Creating a daemon is not very difficult. In attachment you find a C function that does just that.

Hope this helps,
Rik
James R. Ferguson
Acclaimed Contributor

Re: How to create Daemon?

Hi:

In the simplest sense, a daemon is simply a program that performs a task. It sleeps, waiting for an event or a timer to expire, wakes up, does some work, and goes back to sleep.

Typically daemons are named with a "d" suffix, e.g. ftpd for the ftp daemon. This is very useful since many daemons run "forever" but over their elapsed time consume very litte CPU. Suffixing them with a "d" denotes this expectation.

By definition a shell script can be written to execute, for instance, a "while true do" loop; sleep; and wake up every 10 seconds or so to look for work to do. You would launch the script with the form:

# nohup impd 2>&1 /tmp/impd.log &

Depending on the nature of the the daemon you the initiation could be done in the /sbin/rc.d startup directory.

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to create Daemon?

Hi:

A more specific suggestion:

Create a shell scritp daemon as suggested in the abstract above. Since I am not near a server my syntax is from my head, but here's the idea. The output from 'who -u' gives the idle time (which is what you want) and the pid of the shell you want to kill).
Check the positions of the fields from 'who'. You may also have to fixup awk's match for the idle time, but you can get the general idea from this:

while true
do
who -u|awk '{if (split($4,A[1,:]) >= 15} {system(kill $5)}'
sleep 60
done

...JRF...
Tim Malnati
Honored Contributor

Re: How to create Daemon?

A few words of warning here. In general, it is far better to kill off 'inactive' terminal processes at the application level. Is the process actually doing nothing at a command line, or is it in the middle of a opened transaction? Unix can't tell the difference easily. For example, you could be in the middle of installing patches with the analysis complete waiting for you to start the actual install; the daemon comes along after 15 minutes of inactivity and kills it off. This is a relatively easy thing to recover from, but something like an end of month process waiting for an approval step is not.
James R. Ferguson
Acclaimed Contributor

Re: How to create Daemon?

Hi:

Here is a syntatically correct offering of my earlier post. As written this will look for sessions idle for 15-minutes or more.

I would note too, that Tim's comments, above, are well taken. In any event, try:

# who -u|awk 'substr($6,3,2) >= 15 {system("kill -s 9 " $7)}'

...JRF...