G
G
grisha22172014-05-03 22:23:25
linux
grisha2217, 2014-05-03 22:23:25

Execute script every 5 seconds Linux

Hello. It is necessary that the script /var/filter.sh be run every 5 seconds without any commands. Those. I launched it once after rebooting the OS, and then it was already hanging in the processes and being executed.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
starosta6123, 2014-05-03
@grisha2217

I remembered:
# watch --interval=5 /var/filter.sh
another useful application watch
nsk.lug.ru/poleznye-sovety/poleznye-sovety-komanda...
www.opennet.ru/man.shtml?topic=watch&category =1&ru...
You can pipe the output to /dev/null
# watch --interval=5 /var/filter.sh > /dev/null
Not exactly what you want, but take note.
Runs your script at intervals of 5 seconds.
The only thing to keep in mind is that if your script does not have time to execute in five seconds, then there may be an effect of "avalanche birth of new processes". It can especially arise if the script uses locks.
And sleep is very easy en.wikipedia.org/wiki/Sleep
/var/filter.sh

#!/bin/sh
echo "Начинаем..."
while (true) 
do
 echo "Ваш скрипт";
 sleep 5; # пауза 5 секунд
done;

V
Vlad Zhivotnev, 2014-05-04
@inkvizitor68sl

In general, there is snaked for this.
But if we are to make crutches, then:

#!/bin/bash
while :; do sleep 5; flock -n /tmp/lock1 -c /var/script.sh & done

This construction will run the script approximately every 5.04 seconds (in addition to five seconds, time will also be spent calling sleep and execve the script). At the same time, the script's running time itself will no longer affect "every 5 seconds". Flock is needed here in case the script "sticks" - so as not to produce many running copies of the script in the system. If the script goes somewhere outside or into the database, be sure to use flock.
Next, you need to ensure a reliable start of the cycle itself. Here cron will come to the rescue (along with flock). Add the following line to /etc/crontab:
* * * * * root flock -n /tmp/lock2 -c /path/to/script2
Every minute, cron will try to run a second copy of the loop, if lock2 is busy, it will not run.
You can, of course, just add the cycle script to /etc/rc.local, but if it dies, then it will not start later.

B
bahek2462774, 2014-05-03
@bahek2462774

add to cron

V
Vladlen Grachev, 2014-05-03
@gwer

Either in cron, or the body of the script into an infinite loop, at the end (beginning) of which there is a delay (sleep 5).
The second option is more convenient, since it is much easier to unload the script (just kill it, and not cut it out of cron), and also run it again after cutting it out. Yes, and it is more suitable for the conditions specified in the formulation of the question (it may not start automatically after a reboot, if not set to autoload).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question