LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   cronjob run every 5 seconds (https://www.linuxquestions.org/questions/linux-server-73/cronjob-run-every-5-seconds-4175723738/)

sag2662 04-04-2023 02:31 AM

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

Guttorm 04-04-2023 02:38 AM

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.

sag2662 04-04-2023 02:45 AM

Quote:

Originally Posted by Guttorm (Post 6422192)
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

Guttorm 04-04-2023 02:47 AM

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.

sag2662 04-04-2023 02:52 AM

Quote:

Originally Posted by Guttorm (Post 6422194)
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'

pan64 04-04-2023 03:02 AM

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.

sag2662 04-04-2023 03:10 AM

Quote:

Originally Posted by pan64 (Post 6422199)
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++)


michaelk 04-04-2023 06:00 AM

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.

MadeInGermany 04-04-2023 07:06 AM

Code:

for ((i=0; i<12; i++))
0 .. 55 seconds, then cron starts it again.

chrism01 04-04-2023 08:11 PM

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 .

lvm_ 04-05-2023 01:04 AM

[ana]cron cannot do seconds but systemd timer can https://www.freedesktop.org/software...emd.timer.html

sag2662 04-05-2023 02:02 AM

Thanks all

sag2662 04-11-2023 02:57 AM

Quote:

Originally Posted by sag2662 (Post 6422401)
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

michaelk 04-11-2023 05:36 AM

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.

pan64 04-11-2023 05:38 AM

does this machine run continuously?


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