Daemon ?

How do i write a program that runs as a Daemon ?

Basically i want to achieve this :
I want to invoke a carry out a certain activity (liking invoking a certain application) based on time. That is, i schedule some activities for some time in future. Now, my appln. should continuosly check for time and if it is time to carry out that activity, simply do that, and continue running.

I guess, i must write a program that runs in background as a Daemon.

What language has to be used...Shell Script/C ?

How do i do this ?
Any available code ...
or info. on where i can find help on this would be helpful.


Who is Participating?
tfewsterConnect With a Mentor Commented:
Well said, sereda. Mind if I add some background & tips?

Jitu, cron is the standard Unix scheduler for jobs that you want to run repeatedly: You can specify the start time by the minute, hour, day of week or day of month that you want the job to run. Getting this right needs a bit of practice, so start with seredas example and try different start times.  

Each user has their own "crontab" file, a table of jobs that they want to run, each with it's own start time (as specified above).

You can redirect the output (and any error messages) from the cron job to a log file, so you can check it in the morning (if you don't redirect output, any output will be mailed to you).

Because cron is run as a top level system process and doesn't use your environment, you have to specify full pathnames, like /usr/bin/lp /home/Jitu/my_file

BTW, if you only want to run the job once (e.g. print a large file overnight) you can use "at start_time command", that is run "command" at the specified time. This is much simpler to use!

You should use C.
man fork
man setsid
man exit
man chdir

1. fork.
  2. sleep a while;
  3. exit;
  2. chdir "/";
  3. setsid ()
  4. infinite loop

If you want a code example, i think you should increate point cost ;)
What's wrong with "cron"? What you have described is all done by cron, plus cron has security features and is started when the system boots. You program would have to be restarted if the system was rebooted, plus there would be an additional CPU overhead.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

JituAuthor Commented:
tfewster >
I am new to UNIX. I have no idea what is "cron" and how to use it...
Can u give some input or sample code..Thx.
$ crontab -e
-- your EDITOR appears
-- enter there:
* * * * * echo "Minute tick" > /dev/console
--- save file

Here is an example.
You will receive a message on console once per minute.

to get more information, run
   man crontab
   man cron

Come on Jitu - As an expert yourself, you must know how frustrating it is to wait for someone to accept an answer!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.