92

I need to run a CRON job every 10 seconds from started time.

In Linux how to run a CRON job on every 10 seconds from the time its started?

I am trying to solve that as following: when I make a request (or start) at 04:28:34 it should start at 04:28:44 not at 4:28:40

This is what I have done

# m h  dom mon dow   command
*/10 * * * * /usr/bin/wget http://api.us/application/

What did I do wrong? Why does this not trigger wget every 10 seconds?

5
  • What should happen if a request is made at 04:28:34 and another request is made at 04:28:37 ? Do you need two requests in that case, or just one?
    – user1233508
    Commented May 18, 2015 at 6:00
  • @DCoder its only one. request made at 04:28:34 should start at 04:28:44 not at 04:28:40
    – dev1234
    Commented May 18, 2015 at 6:02
  • In that case, from the second request's point of view, the request is sent after 7 seconds, not 10. Is that acceptable? Should it keep repeating the request every 10 seconds until you shut down the server, or will it need to stop at some point?
    – user1233508
    Commented May 18, 2015 at 6:27
  • @DCoder it should stop at the given time.
    – dev1234
    Commented May 18, 2015 at 6:36
  • 4
    Use a shell script. cron is the wrong solution here.
    – scai
    Commented May 18, 2015 at 6:46

6 Answers 6

146

To elaborate on Sougata Bose's answer, I think the OP wants a command to be run every 10 seconds from a start time; not 10 seconds after the first minute and every subsequent minute.

cron only has a resolution of 1 minute (there are other tools I think that may have finer resolutions but they are not standard on unix).

Therefore, to resolve your issue you need 60 seconds / 10 seconds = 6 cron jobs, each with a sleep.

e.g. run crontab -e and add the following lines to your chosen editor:

* * * * * ( /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 10 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 20 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 30 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 40 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 50 ; /usr/bin/wget http://api.us/application/ )  
3
  • Not sure if this applies to Linux command line but in java7 and above the @Schedule spring annotation allows for resolutions up to the second. For example, this will fire your task every 10 seconds: */10 * * * * ? Try adding one additional token to your expression and see if that works. Commented Dec 4, 2019 at 14:49
  • below answer is more elegant than this but thanks anyway.
    – danone
    Commented Oct 4, 2020 at 8:45
  • 4
    cron generator codepen.io/04/full/YzYLPqr Commented Apr 8, 2022 at 1:15
52

Another option is to edit your crontab with crontab -e and write:

* * * * * for i in {1..6}; do /usr/bin/wget http://api.us/application/ & sleep 10; done
3
  • 8
    Welcome to SO! Next time, please add a bit more explanation to your answer.
    – B--rian
    Commented Jan 13, 2020 at 14:57
  • 7
    Since the sleep is executed only after the command is finished, the interval may be longer than 10 seconds, depending on how long the command takes. This should be taken into account.
    – D. Mika
    Commented Nov 19, 2021 at 7:21
  • No, if you add & after a command, then the next command will run asynchronously. Commented May 6, 2024 at 12:34
31

*/10 * * * * will run every 10 min.
*/10 * * * * * will run every 10 sec.

You can checkout the cron editor for more options.

[edit]

for more Linux specific:

  • the minimum of cron job in Linux is 1 minute
  • you have to write a script of a loop to run every 10 seconds
    execute_every_10_sec.sh
  • grant your script execute permissions:
    chmod +x execute_every_10_sec.sh
  • script:
#!/bin/bash
for i in {1..6}
do
  /path/to/your/command/to/run/every/10/sec
  sleep 10
done
  • notice and consider the length of you executable job
  • open your crontab file crontab -e and run this script by cron schedule command every 1 min
    * * * * * /path/to/your/command/to/execute_every_10_sec.sh
  • now you have a 10 second job running
6
  • 31
    That does not work FYI. You will get an error
    – Zach Rieck
    Commented Feb 4, 2021 at 0:03
  • 8
    This does not work. Cron on Linux does not support this notation. crontab.cronhub.io is misleading, it does not state which platforms support this. This work on Mac but not on standard Linux server Commented Apr 23, 2021 at 4:15
  • 4
    I guess this doesn't work on ubuntu systems
    – Nikhil VJ
    Commented Jun 23, 2021 at 13:40
  • 3
    This answer is misleading, it does not work and WILL Not work on ubuntu systems as @NikhilVJ said. make use of crontab.guru Commented Jan 19, 2022 at 20:00
  • 1
    Even though this question was for Linux, I came looking for something for node-cron, and it did work. Thank you. Commented Jul 21, 2022 at 1:01
17

Using commas in seconds field works too:

0,10,20,30,40,50 * * * * *
3
  • 4
    This doesn't scare me as much as the other answers, it works and needs more upvotes.
    – BoomZilla
    Commented Aug 10, 2021 at 1:51
  • 8
    It doesn't work on Linux, tested on Ubuntu 20.04 and it doesn't execute the job. As pointed out in other responses, maybe this works on Mac, and other job scheduling tools based on crontab notations but not the crontab for Linux. Commented Oct 6, 2021 at 13:25
  • 1
    doesnt work on linux
    – Mikhail M.
    Commented Oct 25, 2021 at 12:47
8

Use watch instead; for example:

watch -n10 -x your_command
5

If something should run every 10 seconds it does not have to be cron job. It can be a script with endless loop with sleep inside like:

while true
do
  # or whatever command you need to run
  rm -rf /var/www/some-directory
  sleep 10
done

and run it with

nohup bash my-endless-script.sh 

which will run it on background and will continue also if you shut down the terminal.

You can kill the run with kill command.

2
  • The advantage of cronjob is that it provides a log file by default. Commented Apr 7, 2022 at 18:37
  • 1
    The downside, however, is that it can only run once a minute.
    – Čamo
    Commented Apr 8, 2022 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.