Linux - CertificationThis forum is for the discussion of all topics relating to Linux certification.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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?
-------------------------------------------------------
"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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.