LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > Linux - Certification
User Name
Password
Linux - Certification This forum is for the discussion of all topics relating to Linux certification.

Notices


Reply
  Search this Thread
Old 08-08-2006, 08:37 PM   #1
ionic_slimm
LQ Newbie
 
Registered: Feb 2006
Posts: 5

Rep: Reputation: 0
cron job every 2 seconds question


I was stumped on this question on setting up a cron job to run every two seconds...is it possible or this is something that needs to be scripted?
 
Old 08-08-2006, 10:23 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
From man 5 crontab:


The time and date fields are:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

Meaning that the resolution of cron is down to 1 minute increments. Anything smaller would have to be scripted. I would suggest

while true; do echo foo; sleep 2; done
 
Old 08-09-2006, 03:59 AM   #3
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
Cron will only be able to schedule / run a program in per minute intervals. Even then if you are running a task every minute or less then it should be run as a daemon and you should not try and load that onto cron.

The program will need to be written so that it runs as a daemon / background task, or using the while loop as bartonski has shown.
 
Old 08-09-2006, 01:02 PM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by penguintutor
The program will need to be written so that it runs as a daemon / background task, or using the while loop as bartonski has shown.
This begs the question 'how do I turn my 1 line shell script into a daemon'?
 
Old 08-09-2006, 07:18 PM   #5
ionic_slimm
LQ Newbie
 
Registered: Feb 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bartonski
From man 5 crontab:


The time and date fields are:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

Meaning that the resolution of cron is down to 1 minute increments. Anything smaller would have to be scripted. I would suggest

while true; do echo foo; sleep 2; done
-------------------------------------------------------
"while true" The while loop is checking what is true? So the overall solution would be to write a script that has a loop and sleep for the two second intervals then basically use crontab to execute "&" the script?
 
Old 08-09-2006, 11:38 PM   #6
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by ionic_slimm
-------------------------------------------------------
"while true" The while loop is checking what is true? So the overall solution would be to write a script that has a loop and sleep for the two second intervals then basically use crontab to execute "&" the script?
Not quite. A while loop executes as long as its argument is true... therefore "while true" creates an infinite loop.

Putting this inside a cron job would be a bad idea, as cron will not break out of a given loop, it would just launch another infinite loop at the specified time. Eventually, you would end up taking up all of the system resources.

You can break out of the infinite loop manually.

If you want to run this in the background you can simply do

(while true; do echo foo; sleep 2; done) &
 
Old 08-10-2006, 04:53 AM   #7
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
There are essentially 3 steps in this.

1/ Run the command in the background (&)
by running the command with an & it will run in the background. If you are running this from a terminal on the computer then that is sufficient.
If you wanted to run this from a shell that you subsequently close then you need to use nohup (information on running tasks in the background), or if you want it to run all the time that your PC is running then you need to call it as part of the startup scripts (that topic is worthy of another thread - or google on rc.local for rpm based distros, or google init.d for any distro).

2/ Running continuously
The while loop will cause the script to keep running continuously. A "while true" is a never ending loop. See Guide to Bash Programming (Section on loops)

3/ Pause for 2 seconds
The sleep 2 command will cause the program to pause for 2 seconds before it runs again. See man sleep. Doing this will stop the script hogging the CPU, and meet your run every 2 seconds requirement. This time is in addition to how long it takes for your script to run, so if you script ran for 1 second, then effectively it will run every 3 seconds.
 
Old 08-10-2006, 06:22 AM   #8
ionic_slimm
LQ Newbie
 
Registered: Feb 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks! Makes much sense now then before.
 
  


Reply



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
Simple Cron Job question FreezEy Slackware 3 07-11-2006 01:30 PM
Cron Job Question tarballed Linux - Newbie 4 06-20-2006 03:39 PM
Cron Job Question abhijeetudas Linux - Software 2 03-01-2006 11:27 PM
Cron Job Syntax for a task in seconds AJones Linux - Software 2 07-22-2005 12:56 PM
Quick Question about cron job andy18 Linux - General 2 08-11-2003 04:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > Linux - Certification

All times are GMT -5. The time now is 03:18 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
Open Source Consulting | Domain Registration