LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-04-2023, 02:31 AM   #1
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Rep: Reputation: 0
cronjob run every 5 seconds


Hi all,

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

Last edited by sag2662; 04-04-2023 at 02:34 AM.
 
Old 04-04-2023, 02:38 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

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.
 
Old 04-04-2023, 02:45 AM   #3
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
Hi

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
 
Old 04-04-2023, 02:47 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
If you just want to start it, and make it continue when you log out, you can start it like this:
Code:
nohup scriptname &
It will run unitil crash or reboot.
 
Old 04-04-2023, 02:52 AM   #5
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
If you just want to start it, and make it continue when you log out, you can start it like this:
Code:
nohup scriptname &
It will run unitil crash or reboot.
have error
nohup:ignoring input and appending output to 'nohup.out'

Last edited by sag2662; 04-04-2023 at 02:58 AM.
 
Old 04-04-2023, 03:02 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,968

Rep: Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333
cronjob can start a script in every minute, so you only need to write a script (loop) to execute something 12 times and put that script into crontab.
 
Old 04-04-2023, 03:10 AM   #7
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
cronjob can start a script in every minute, so you only need to write a script (loop) to execute something 12 times and put that script into crontab.
so which means i can run use for loop ? and schedule cron to every 1 minute ?

Code:
for i in {1..12}; do date; done
or 

for (let i = 0; i < 12; i++)

Last edited by sag2662; 04-04-2023 at 03:12 AM.
 
Old 04-04-2023, 06:00 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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.
 
Old 04-04-2023, 07:06 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,809

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
Code:
for ((i=0; i<12; i++))
0 .. 55 seconds, then cron starts it again.

Last edited by MadeInGermany; 04-04-2023 at 07:08 AM.
 
Old 04-04-2023, 08:11 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The nohup ... & cmd is fine, you just need to tell it where to o/p like eg
Code:
nohup /path/to/script >/path/to/logdir/script.log 2>&1 &
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
 
Old 04-05-2023, 01:04 AM   #11
lvm_
Member
 
Registered: Jul 2020
Posts: 957

Rep: Reputation: 340Reputation: 340Reputation: 340Reputation: 340
[ana]cron cannot do seconds but systemd timer can https://www.freedesktop.org/software...emd.timer.html
 
Old 04-05-2023, 02:02 AM   #12
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Thanks all

Last edited by sag2662; 04-05-2023 at 02:43 AM.
 
Old 04-11-2023, 02:57 AM   #13
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sag2662 View Post
Thanks all
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

Code:
-rw-r--r-- 1 root root 1736496 Apr  9 14:07 2023-04-08-spx.log
-rw-r--r-- 1 root root 1986480 Apr 10 19:37 2023-04-09-spx.log
-rw-r--r-- 1 root root 1452102 Apr 11 07:53 2023-04-10-spx.log
-rw-r--r-- 1 root root  359724 Apr 11 07:53 2023-04-11-spx.log
Code:
for ((i=0; i<12; i++))
do
command >> /tmp/log/$(date +'%Y-%m-%d')-spx.log
done
cronjob
Code:
* * * * * /home/script.sh

Last edited by sag2662; 04-11-2023 at 03:16 AM.
 
Old 04-11-2023, 05:36 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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.
 
Old 04-11-2023, 05:38 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,968

Rep: Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333Reputation: 7333
does this machine run continuously?
 
  


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
video repeats 0.5 seconds every 60 seconds WilliamTexelHampton Linux - Newbie 6 06-29-2017 11:10 AM
cronjob to run every 15 minutes? your_shadow03 Linux - Newbie 5 11-19-2009 10:58 PM
[SOLVED] Experiencing freezing up to 5 seconds every 10 seconds, could these be the problem? Switch7 Slackware 10 11-16-2009 04:36 PM
how do i get a cronjob to run every two hours in AIX: 00 */2 * * * command no work boyd98 AIX 1 10-19-2009 06:33 PM
[SOLVED] make cronjob run every 10mins qwertyjjj Linux - Newbie 2 08-13-2009 08:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:47 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