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 |
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 |
Quote:
|
If you just want to start it, and make it continue when you log out, you can start it like this:
Code:
nohup scriptname & |
Quote:
nohup:ignoring input and appending output to 'nohup.out' |
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.
|
Quote:
Code:
for i in {1..12}; do date; done |
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.
|
Code:
for ((i=0; i<12; i++)) |
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 & 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 . |
[ana]cron cannot do seconds but systemd timer can https://www.freedesktop.org/software...emd.timer.html
|
Thanks all
|
Quote:
Code:
-rw-r--r-- 1 root root 1736496 Apr 9 14:07 2023-04-08-spx.log Code:
for ((i=0; i<12; i++)) Code:
* * * * * /home/script.sh |
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.
|
does this machine run continuously?
|
All times are GMT -5. The time now is 11:13 AM. |