Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
I wanted to run script for every 5 seconds, It seems with cronjob we cannot execute it. so i wrote a script with while loop
Code:
#!/bin/bash
while true; do
date >> /tmp/save_date
sleep 5;
done
but my question here is how wanted to schedule the cron so that it executes the above script. please suggest. can i still schedule cron to run for 1 minute
You can put @reboot in crontab so the script starts at reboot. Sometimes it's too early - maybe network and other services are not up yet? If so, wait a little before it starts.
Code:
@reboot sleep 60 && scriptname
Another way is to use supervisor instead of cron. It's made for running scripts like this, and will restart it if it crashes.
You can put @reboot in crontab so the script starts at reboot. Sometimes it's too early - maybe network and other services are not up yet? If so, wait a little before it starts.
Code:
@reboot sleep 60 && scriptname
Another way is to use supervisor instead of cron. It's made for running scripts like this, and will restart it if it crashes.
Now the server cannot be rebooted, its up now. so how to handle
Either method works. linux isn't a real time operating system so there is no guarantee what second your script actually starts running. This means the time difference of the last "period" of the prior minute might not be exactly 5 seconds to the start of the 1st "period" of the next minute.
For something you want to run that frequently, this (daemon) approach makes more sense for 2 reasons :
1. cron is only configurable to the minute
2. Each cron job invocation involves the creation of a whole new process env, which takes some time.
You also need to worry about whether runs will overlap each other, which may cause issues, depending on exactly what the process is doing.
Running in daemon mode means only one process will be running at a time, assuming your code in post #1 .
Last edited by chrism01; 04-12-2023 at 12:35 AM.
Reason: typo
Hi all the logs are getting created according to date but the output writing to log file is wrong. I wanted the logs to be written according to each date and end at the same date if next day then the logs should be written accordingly. But my output is weird. may i know whats the wrong with code or cron
Without knowing what command actually does it is not possible to know whats wrong. How long does it take to actually run the it once? If it takes more then a minute to run the command 12 times you will have multiple processes which continue into the next day. You are also missing the sleep 5 command in your loop.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.