How to run when you turn on the computer script that repeats the same command every N seconds?

  • 2
    Is the interval a multiple of a minute? if the interval is in minutes, then use cron If it is less than or not a multiple of a minute, then you need to do your service that will run the script at the intervals you need - Tunker

3 answers 3

Use the regular cron scheduler, read the console man crontab

  • one
    And if you need a frequency of less than a minute? - cheops
  • Yes, run it in the background in an eternal loop, let the interval itself track and wake up when necessary. - PinkTux pm
  • 3
    @cheops here is the answer at stackoverflow.com/questions/9619362/… - perfect
  • one
    @perefect, well, if there is nothing better, you can, though the accuracy will be - plus or minus bast (or rather, a few seconds). - cheops
  • @cheops the native function sleep used there and there is no difference where to push it in kroner or in the script. And bastard, and so it will all depend on the response of the executable binary and the system load. - perfect

for Ubuntu 15.04 and older, you can use systemd / Timers

    if n is equal to, for example, 10 seconds, then you can create, for example, six cron-tasks (running every minute - this is the minimum interval supported by popular implementations of the crond program), the second of which is executed with a delay of 10 seconds, the third - 20, and etc .:

     * * * * * /ΠΏΡƒΡ‚ΡŒ/ΠΊ/скрипту/ΠΈΠ»ΠΈ/ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅ ΠΎΠΏΡ†ΠΈΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ * * * * * sleep 10; /ΠΏΡƒΡ‚ΡŒ/ΠΊ/скрипту/ΠΈΠ»ΠΈ/ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅ ΠΎΠΏΡ†ΠΈΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ * * * * * sleep 20; /ΠΏΡƒΡ‚ΡŒ/ΠΊ/скрипту/ΠΈΠ»ΠΈ/ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅ ΠΎΠΏΡ†ΠΈΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ ... 

    other methods: