LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2006, 01:44 PM   #1
dimmak
LQ Newbie
 
Registered: Jun 2005
Posts: 7

Rep: Reputation: 0
Daemon


Hello,

I'm new to linux and i would like to have a daemon running every 5 minutes and executing a perl script.

I tried some cron jobs but it seems difficult to understand hence i dont know if this is the right thing to do!
 
Old 11-23-2006, 03:34 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
If you like to run some command at certain intervals, cron is probably the best choise for you. If you'd like to time a command to be run at some time, like an alarm clock in the morning, you could use at. A daemon, on the other hand, is considered a program that sits in the background waiting for something and acting as the "something" happens, for example -- so it's running all the time, not just at certain intervals. If you wish to execute a perl script, use cron:

Code:
crontab -e
will open the crontab file (file which includes the commands to be run along with the times; one per line) for the current user. You could also add an -u user option to edit user's crontab.

The file is edited as follows: each line contains one command to be executed at the given time. The line goes like follows:

* * * * * /path/to/command --possible-options

the stars you see in the front tell when the command is to be run. A star like above means "all the time" or "always" (so don't put all stars). I don't necessarily remember which star is which, but it reads in the man page of crontab:
Code:
man crontab
If I remember correctly, the first one means minutes (0-59), second means hours (0-23), third means the day (1-31), fourth the month (1-12) and the last one the day of the week. They might also be in a different order, so read the man page to make sure.

Basically you just put them there, a number, space, number, space etc. and after the weekday a space and the command, just as you would run it from the console. It's a good habit to write full paths (i.e. /usr/bin/date instead of date). And like I said, if you want to omit some time ("run every day of the month"), use a star. Like

Code:
10 12 * * * /usr/bin/date > ~/date.txt
which should run /usr/bin/date and put the output into a file called date.txt under your home directory, and run the command 10 minutes past 12 every day of the month, every month and every weekday (weekday is useful, if you only want to run the command at Mondays, and only if Monday is some certain day like 16.4. or only at Tuesdays in March).

You now notice that you can't give smaller times than "each one minute". How to run a command every 10 seconds? Easy - write a shell script that runs the wanted command, waits for 10 seconds, runs it again etc. until it has done so for a full minute. Make cron run that script every minute and -- ta-daa, it's there

I hope this cleared things a bit. I know the format of the crontab file is confusing, but if you read the manpage I suggested, it has got some examples at the bottom which should clarify the format to you.

If the crontab file is opened in vi editor (you don't edit the file directly, crontab command does this for you), here's a quick guide on how to use it:

- press INSERT or I (big i) to enter the INSERT MODE - this is where you type text (otherwise you just wander around the file and do other stuff)
- once you've written/edited the file, press ESC to get back to COMMAND MODE - this is where you give commands to Vi.
- once you're in COMMAND MODE, type :w (shift-dot and w) and ENTER to save (write) the file, then :q and ENTER to quit -- or just write :wq to save and quit at the same time.

Another way around is to use your favourite editor by setting the console editor variable to point to your favourite editor before running crontab.

EDIT: in most cases it's a good habit to press ENTER to add a linefeed at the end of the last line, so that the last line of the file becomes an empty line; it's not always needed, but in some cases parsing a configuration file may fail at the last line if it's not empty; some programs tend to expect the file to end at an empty line. I'm not sure if crontab wants this, but generally it doesn't harm you to insert one blank line at the end of the file (and not leave a complete written line to be the last one).

Last edited by b0uncer; 11-23-2006 at 03:37 PM.
 
Old 11-23-2006, 03:53 PM   #3
dimmak
LQ Newbie
 
Registered: Jun 2005
Posts: 7

Original Poster
Rep: Reputation: 0
thanks for your detailed answer i definitely don't feel like a newbie anymore!

I will try it thanks again !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
at daemon monideepa Linux - Software 4 02-16-2006 10:26 PM
Daemon arunachalam Linux - Newbie 1 09-08-2005 07:22 AM
daemon or not? ZX_SA Linux - General 2 08-08-2005 02:13 AM
daemon nobu Slackware 1 01-09-2003 09:02 AM
What is a daemon? nutshell Linux - General 3 02-26-2002 07:25 AM


All times are GMT -5. The time now is 11:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration