363

Ok so I have a cron that I need to run every 30 seconds.

Here is what I have:

*/30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''

It runs, but is this running every 30 minutes or 30 seconds?

Also, I have been reading that cron might not be the best tool to use if I run it that often. Is there another better tool that I can use or install on Ubuntu 11.04 that will be a better option? Is there a way to fix the above cron?

caffreyd
  • 853
  • 1
  • 15
  • 23
Matt Elhotiby
  • 39,076
  • 79
  • 210
  • 312
  • CommaToast, and what happens if your Javascript or Java app falls over for some reason and exits? How will it restart? :-) – paxdiablo Feb 17 '15 at 01:24
  • 15
    Add a little NodeJS app, lol. Why not a little c++ app? While we're at it, we can name it 'cron' and run it as a service. – Andrew Mar 20 '15 at 13:51
  • 3
    possible duplicate of [How to run Cronjobs more often than once per minute?](http://stackoverflow.com/questions/4672383/how-to-run-cronjobs-more-often-than-once-per-minute) – rubo77 Apr 23 '15 at 05:15
  • I just found this while looking at user profiles and saw you were online less than 1h ago (just to check if the acc is still in use), is there any specific reason that you didn't accept any of the answers below? – Fabian N. Aug 07 '18 at 22:16

20 Answers20

789

You have */30 in the minutes specifier - that means every minute but with a step of 30 (in other words, every half hour). Since cron does not go down to sub-minute resolutions, you will need to find another way.

One possibility, though it's a bit of a kludge(a), is to have two jobs, one offset by 30 seconds:

# Need these to run on 30-sec boundaries, keep commands in sync.
* * * * *              /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )

You'll see I've added comments and formatted to ensure it's easy to keep them synchronised.

Both cron jobs actually run every minute but the latter one will wait half a minute before executing the "meat" of the job, /path/to/executable.

For other (non-cron-based) options, see the other answers here, particularly the ones mentioning fcron and systemd. These are probably preferable assuming your system has the ability to use them (such as installing fcron or having a distro with systemd in it).


If you don't want to use the kludgy solution, you can use a loop-based solution with a small modification. You'll still have to manage keeping your process running in some form but, once that's sorted, the following script should work:

#!/bin/env bash

# Debug code to start on minute boundary and to
# gradually increase maximum payload duration to
# see what happens when the payload exceeds 30 seconds.

((maxtime = 20))
while [[ "$(date +%S)" != "00" ]]; do true; done

while true; do
    # Start a background timer BEFORE the payload runs.

    sleep 30 &

    # Execute the payload, some random duration up to the limit.
    # Extra blank line if excess payload.

    ((delay = RANDOM % maxtime + 1))
    ((maxtime += 1))
    echo "$(date) Sleeping for ${delay} seconds (max ${maxtime})."
    [[ ${delay} -gt 30 ]] && echo
    sleep ${delay}

    # Wait for timer to finish before next cycle.

    wait
done

The trick is to use a sleep 30 but to start it in the background before your payload runs. Then, after the payload is finished, just wait for the background sleep to finish.

If the payload takes n seconds (where n <= 30), the wait after the payload will then be 30 - n seconds. If it takes more than 30 seconds, then the next cycle will be delayed until the payload is finished, but no longer.

You'll see that I have debug code in there to start on a one-minute boundary to make the output initially easier to follow. I also gradually increase the maximum payload time so you'll eventually see the payload exceed the 30-second cycle time (an extra blank line is output so the effect is obvious).

A sample run follows (where cycles normally start 30 seconds after the previous cycle):

Tue May 26 20:56:00 AWST 2020 Sleeping for 9 seconds (max 21).
Tue May 26 20:56:30 AWST 2020 Sleeping for 19 seconds (max 22).
Tue May 26 20:57:00 AWST 2020 Sleeping for 9 seconds (max 23).
Tue May 26 20:57:30 AWST 2020 Sleeping for 7 seconds (max 24).
Tue May 26 20:58:00 AWST 2020 Sleeping for 2 seconds (max 25).
Tue May 26 20:58:30 AWST 2020 Sleeping for 8 seconds (max 26).
Tue May 26 20:59:00 AWST 2020 Sleeping for 20 seconds (max 27).
Tue May 26 20:59:30 AWST 2020 Sleeping for 25 seconds (max 28).
Tue May 26 21:00:00 AWST 2020 Sleeping for 5 seconds (max 29).
Tue May 26 21:00:30 AWST 2020 Sleeping for 6 seconds (max 30).
Tue May 26 21:01:00 AWST 2020 Sleeping for 27 seconds (max 31).
Tue May 26 21:01:30 AWST 2020 Sleeping for 25 seconds (max 32).
Tue May 26 21:02:00 AWST 2020 Sleeping for 15 seconds (max 33).
Tue May 26 21:02:30 AWST 2020 Sleeping for 10 seconds (max 34).
Tue May 26 21:03:00 AWST 2020 Sleeping for 5 seconds (max 35).
Tue May 26 21:03:30 AWST 2020 Sleeping for 35 seconds (max 36).

Tue May 26 21:04:05 AWST 2020 Sleeping for 2 seconds (max 37).
Tue May 26 21:04:35 AWST 2020 Sleeping for 20 seconds (max 38).
Tue May 26 21:05:05 AWST 2020 Sleeping for 22 seconds (max 39).
Tue May 26 21:05:35 AWST 2020 Sleeping for 18 seconds (max 40).
Tue May 26 21:06:05 AWST 2020 Sleeping for 33 seconds (max 41).

Tue May 26 21:06:38 AWST 2020 Sleeping for 31 seconds (max 42).

Tue May 26 21:07:09 AWST 2020 Sleeping for 6 seconds (max 43).

If you want to avoid the kludgy solution, this is probably better. You'll still need a cron job (or equivalent) to periodically detect if this script is running and, if not, start it. But the script itself then handles the timing.


(a) Some of my workmates would say that kludges are my specialty :-)

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • 32
    This is a great workaround, so much so i think it transcends it's kludginess – Question Mark Jan 12 '15 at 10:25
  • wouldn't this work too? `* * * * * ( /path/to/executable param1 param2; sleep 30 ; /path/to/executable param1 param2 )` – rubo77 Apr 23 '15 at 05:26
  • 16
    @rubo77, only if it took less than a second to run :-) If it took 29 seconds, it would happen at 0:00:00, 0:00.59, 0:01:00, 0:01:59 and so on. – paxdiablo Apr 23 '15 at 05:30
  • 3
    What are the round brackets for around the second line? – Nigel Alderton Sep 05 '18 at 12:55
  • 1
    Sleep is a perfect idea for run custom second – Abdus Salam Oct 14 '18 at 12:55
  • 2
    This is a lovely solution to an issue that otherwise cripples crons effectiveness for certain tasks that require execution in fractions of minutes. Thank you. – Fiddy Bux Jan 09 '19 at 00:18
  • This solution is indeed evil. Its unneeded duplication and a kludge. Sujoshi's bash loop is better; Adi Chiru's fcron suggestion is better; but if you have any modern Linux OS, the SystemD timer solution below is best. – Guss Oct 11 '19 at 22:21
  • @Guss, I freely admitted it was a kludge but the duplication is local so can be seen (and kept in sync) easily. If you want to ensure you're not having to continuously change both, just make a shell script that gets changed and run it. For a bash-loop-sleep solution, it's not the same thing - a command taking 20s to run will execute every 50 seconds, not 30. On the plus side, `fcron` looks promising (assuming you're allowed to install new stuff) - will have a look into that. No need for me to look into system, we already use it heavily in our embedded system and we're reasonably happy with it. – paxdiablo Oct 12 '19 at 01:31
  • 1
    @Guss, in addition, I've added a comment referencing the `fcron` and `systemd` answers (no point duplicating the info in this answer). If available, `systemd` is preferable even with the minimal extra setup required. – paxdiablo Oct 12 '19 at 01:43
  • I'm wondering why Google finds so many cron tutorials and expression builders that have seconds and not minutes as the first element. Some examples: http://www.cronmaker.com/ https://www.freeformatter.com/cron-expression-generator-quartz.html .... – JustAMartin May 18 '20 at 08:19
  • @JustAMartin, cronmaker doesn't appear to go below minute resolution, and quartz (I believe) *can* actually handle resolutions down to the second. – paxdiablo May 26 '20 at 02:22
  • @paxdiablo cronmaker generates final result with seconds. But ok, it also mentions `CronMaker uses Quartz`. This is so confusing - newcomers to GNU/Linux have no idea which cron is Quartz and which isn't, and if their chosen distro is using Quartz or isn't. Why Quartz is called `cron` at all, and not `quartz` - it would avoid the confusion. – JustAMartin May 26 '20 at 07:43
  • @JustAMartin: sorry, didn't notice that, I just assumed that, because it only allowed you to generate to a minutes resolution, it was standard cron. I agree with your comments about naming but I'm not aware of any distro that uses quartz by default (maybe Oracle Linux because they want to push Java). – paxdiablo May 26 '20 at 12:39
76

You can't. Cron has a 60 sec granularity.

* * * * * cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
* * * * * sleep 30 && cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
wildplasser
  • 38,231
  • 6
  • 56
  • 94
66

If you are running a recent Linux OS with SystemD, you can use the SystemD Timer unit to run your script at any granularity level you wish (theoretically down to nanoseconds), and - if you wish - much more flexible launching rules than Cron ever allowed. No sleep kludges required

It takes a bit more to set up than a single line in a cron file, but if you need anything better than "Every minute", it is well worth the effort.

The SystemD timer model is basically this: timers are units that start service units when a timer elapses.

So for every script/command that you want to schedule, you must have a service unit and then an additional timer unit. A single timer unit can include multiple schedules, so you normally wouldn't need more than one timer and one service.

Here is a simple example that logs "Hello World" every 10 seconds:

/etc/systemd/system/helloworld.service:

[Unit]
Description=Say Hello
[Service]
ExecStart=/usr/bin/logger -i Hello World

/etc/systemd/system/helloworld.timer:

[Unit]
Description=Say Hello every 10 seconds
[Timer]
OnBootSec=10
OnUnitActiveSec=10
AccuracySec=1ms
[Install]
WantedBy=timers.target

After setting up these units (in /etc/systemd/system, as described above, for a system-wide setting, or at ~/.config/systemd/user for a user-specific setup), you need to enable the timer (not the service though) by running systemctl enable --now helloworld.timer (the --now flag also starts the timer immediately, otherwise, it will only start after the next boot, or user login).

The [Timer] section fields used here are as follows:

  • OnBootSec - start the service this many seconds after each boot.
  • OnUnitActiveSec - start the service this many seconds after the last time the service was started. This is what causes the timer to repeat itself and behave like a cron job.
  • AccuracySec - sets the accuracy of the timer. Timers are only as accurate as this field sets, and the default is 1 minute (emulates cron). The main reason to not demand the best accuracy is to improve power consumption - if SystemD can schedule the next run to coincide with other events, it needs to wake the CPU less often. The 1ms in the example above is not ideal - I usually set accuracy to 1 (1 second) in my sub-minute scheduled jobs, but that would mean that if you look at the log showing the "Hello World" messages, you'd see that it is often late by 1 second. If you're OK with that, I suggest setting the accuracy to 1 second or more.

As you may have noticed, this timer doesn't mimic Cron all that well - in the sense that the command doesn't start at the beginning of every wall clock period (i.e. it doesn't start on the 10th second on the clock, then the 20th and so on). Instead is just happens when the timer ellapses. If the system booted at 12:05:37, then the next time the command runs will be at 12:05:47, then at 12:05:57, etc. If you are interested in actual wall clock accuracy, then you may want to replace the OnBootSec and OnUnitActiveSec fields and instead set an OnCalendar rule with the schedule that you want (which as far as I understand can't be faster than 1 second, using the calendar format). The above example can also be written as:

OnCalendar=*-*-* *:*:00,10,20,30,40,50

Last note: as you probably guessed, the helloworld.timer unit starts the helloworld.service unit because they have the same name (minus the unit type suffix). This is the default, but you can override that by setting the Unit field for the [Timer] section.

More gory details can be found at:

Guss
  • 24,799
  • 13
  • 87
  • 109
  • 5
    I often come across better answers like this under the comments section. IMHO, though cron has been staple for scheduled jobs, this answer should be the accepted one since its not a "hack job" of sleeps and risking parallelization of executing long-running tasks considering the interval/frequency needed – Qiqo May 18 '19 at 04:18
  • 5
    superb answer, should be the selected answer – GuidedHacking Feb 21 '20 at 16:42
  • I wonder if it's possible on Alpine because people say it's OpenRC there, not systemd. – Nakilon Oct 29 '20 at 08:30
  • @Nakilon: Alpine was originally meant as a minimalistic OS for containers and as such it doesn't need a complex system runtime such as SystemD. IMO OpenRC is also totally an overkill for a container - if you have a good reason for multi-process containers, use supervisord or something as simple, and I also question your need for running cron in such situations. I'm aware that some people run Alpine in non-container situations, and I wish they'd stop - Alpine hasn't passed even the very basic hardening and QA required for running as a main OS. – Guss Oct 29 '20 at 10:32
  • @Guss, the goal isn't about multi-process but about running the task more frequently than once in a minute. I need it to run once in 10 seconds. I'm currently using cron calling the .sh script with six commands chained like this `{ ruby main.rb & sleep 10 ;} && ... && ruby main.rb`. You see, in the last iteration I don't call `sleep` -- I thought that I need to inline the loop like this because I had the `"process is still running"` error. The error message is gone but it still skips the second iteration I don't why. Still debugging. – Nakilon Oct 29 '20 at 11:01
  • Lets continue this discussion in a chat: https://chat.stackoverflow.com/rooms/223813 ? – Guss Oct 29 '20 at 11:10
37

Cron's granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop and it should do what you need:

#!/bin/env bash
while [ true ]; do
 sleep 30
 # do what you need to here
done
Trevor Boyd Smith
  • 15,512
  • 24
  • 110
  • 159
Marvin Pinto
  • 27,868
  • 7
  • 35
  • 52
  • 60
    Keep in mind that isn't _quite_ the same. If the job takes 25 seconds (for example), it will start every 55 seconds rather than every 30 seconds. It may not matter but you should be aware of the possible consequences. – paxdiablo Mar 08 '12 at 14:52
  • 7
    You could run the job in background, then it will run in almost exactly 30 seconds. – Chris Koston Mar 22 '13 at 15:50
  • 1
    while [true]do sleep 30 # do what you need to here done --------- done should be in small case – temple Nov 28 '13 at 21:02
  • 1
    Won't the `while [ true ]` cause you to have lots of instances of the same script, since cron will start a new one every minute? – Carcamano Jan 23 '14 at 15:09
  • 2
    You can do `sleep $remainingTime` where remainingTime is 30 minus the time the job took (and cap it at zero if it took > 30 seconds). So you take the time before and after the actual work, and calculate the difference. – mahemoff Sep 15 '14 at 10:29
  • @ChrisKoston The danger is that the job takes longer than 30 seconds if you run it in the background, so it could spin up dozens of jobs until the server runs out of memory. – mahemoff Sep 15 '14 at 10:30
  • Absolutely, the assumption is that the script, on average takes less then 30 seconds to execute. For the protection you can check how many processes you have running right before executing the script and set the threshold. num=\`pidof your_process | wc -w\` – Chris Koston Sep 15 '14 at 17:53
  • If you divide the seconds by more than 60, you get a decimal. ie. */30 = 2, but */120=.5. Has anyone tried this? Does it work, or just not even run the cron job or? – Mike Szostech Nov 23 '17 at 00:14
  • Just for future info, you can use the `:` or `true` builtins (eg: `while true`) in `bash` (or `dash` or `zsh` for that matter), no need for the `[ ]` characters. – paxdiablo Apr 25 '20 at 01:06
24

No need for two cron entries, you can put it into one with:

* * * * * /bin/bash -l -c "/path/to/executable; sleep 30 ; /path/to/executable"

so in your case:

* * * * * /bin/bash -l -c "cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'' ; sleep 30 ; cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''"

rubo77
  • 15,234
  • 23
  • 111
  • 195
Andrew
  • 678
  • 4
  • 8
  • 14
    Note: this only runs correct if the script takes less than a second to run – rubo77 Apr 23 '15 at 05:48
  • 2
    Rubo - if the job is taking seconds long (instead of milli or microseconds) to complete, then you wouldn't run it every thirty seconds to be able to run twice per minute. So yes, start with 30 then subtract from that the approximate number of seconds per run, if greater than 1 second. – Andrew Apr 30 '15 at 15:51
  • 2
    This also does not help if you want to receive error reports for each run separately. – joeln Jul 03 '19 at 01:43
  • joelin - the command I give does not prevent getting log or output data, I simplified the command to address the question. To capture logging, each command can/should have output redirected if you need logging, e.g script/rails runner -e production '\''Song.insert_latest'\'' could be written as script/rails runner -e production '\''Song.insert_latest'\'' 2>&1 > /path_to_logfile and again can be done for each command within the single cron entry. – Andrew Sep 08 '19 at 15:52
  • It works! Thank you. – Prashant Kumar Jan 20 '21 at 20:46
15

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.

Create a shell script every-5-seconds.sh using bash while loop as shown below.

$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.sh
 sleep 5
done

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

$ nohup ./every-5-seconds.sh &
Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
Pankaj Shinde
  • 2,102
  • 1
  • 25
  • 35
  • 7
    The time will drift. For example, if `backup.sh` takes 1.5 seconds to run, it will execute every 6.5 seconds. There are ways to avoid that, for example `sleep $((5 - $(date +%s) % 5))` – Keith Thompson Oct 15 '14 at 06:37
  • I am new to nohup, while executing your example, nohup is returning 'no such file or directory'. After some searches, you seems to missed out 'sh' after nohup. Like this: $ nohup sh ./every-5-seconds.sh – VHanded Oct 23 '15 at 05:40
14

You can check out my answer to this similar question

Basically, I've included there a bash script named "runEvery.sh" which you can run with cron every 1 minute and pass as arguments the real command you wish to run and the frequency in seconds in which you want to run it.

something like this

* * * * * ~/bin/runEvery.sh 5 myScript.sh

aioobe
  • 383,660
  • 99
  • 774
  • 796
shlomia
  • 840
  • 9
  • 13
12

Use watch:

$ watch --interval .30 script_to_run_every_30_sec.sh
Smittey
  • 2,410
  • 10
  • 28
  • 33
user7079817
  • 121
  • 1
  • 2
  • can i use something like `$ watch --interval .10 php some_file.php`? or `watch` works only with .sh files ? – Yevhenii Shashkov Jun 23 '17 at 10:38
  • You can run anything with watch. However the interval is between the end and start of the next command, so `--interval .30` will not run twice a minute. I.e `watch -n 2 "sleep 1 && date +%s"` it will increment every 3s. – jmartori Jun 20 '19 at 21:37
  • please note that `watch` was designed for terminal use, so - while it can work without a terminal (run with `nohup` then logout) or with a fake terminal (such as `screen`) - it has no affordances for cron-like behavior, such as recovering from failure, restarting after boot, etc'. – Guss Nov 29 '19 at 11:32
7

Currently i'm using the below method. Works with no issues.

* * * * * /bin/bash -c ' for i in {1..X}; do YOUR_COMMANDS ; sleep Y ; done '

If you want to run every N seconds then X will be 60/N and Y will be N.

Nakilon
  • 32,203
  • 13
  • 95
  • 132
sujoshi
  • 87
  • 1
  • 4
  • 1
    You probably want to change `YOUR_COMMANDS` to `YOUR_COMMANDS &`, so that the command is launched to the background, otherwise, if the command takes more than a fraction of a second - it will delay the next launch. So with X=2 and Y=30, if the command takes 10 seconds - it will launch on the minute and then 40 seconds later, instead of 30. Kudus to @paxdiablo. – Guss Oct 12 '19 at 07:06
  • For some reason, if I omit the `/bin/bash -c` part (including the argument quotes) the script only runs every minute, ignoring the iteration (in my case `X=12` and `Y=5`). – abiyi Oct 27 '19 at 21:48
  • I get `process already running`. Probably because the `sleep` needs some miliseconds more to die. It would be nice to not call the last `sleep`, i.e. for example call them only 5 times for 1 minute with 10 sec pauses. – Nakilon Oct 29 '20 at 09:57
6

Use fcron (http://fcron.free.fr/) - gives you granularity in seconds and way better and more feature rich than cron (vixie-cron) and stable too. I used to make stupid things like having about 60 php scripts running on one machine in very stupid settings and it still did its job!

Adi Chiru
  • 69
  • 1
  • 5
6

in dir /etc/cron.d/

new create a file excute_per_30s

* * * * * yourusername  /bin/date >> /home/yourusername/temp/date.txt
* * * * * yourusername sleep 30; /bin/date >> /home/yourusername/temp/date.txt

will run cron every 30 seconds

lingyfh
  • 1,185
  • 17
  • 22
3

Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds. The alternative :

Create a script to execute every 30 seconds:

#!/bin/bash
# 30sec.sh

for COUNT in `seq 29` ; do
  cp /application/tmp/* /home/test
  sleep 30
done

Use crontab -e and a crontab to execute this script:

* * * * * /home/test/30sec.sh > /dev/null
Marcin Orlowski
  • 67,279
  • 10
  • 112
  • 132
szaier
  • 39
  • 3
  • 3
    if I understand this correctly, this script runs 30 times and waits 30 seconds in between every iteration. How does it make sense to run it every minute in cron? – FuzzyAmi Dec 26 '17 at 10:37
2

write one shell script create .sh file

nano every30second.sh

and write script

#!/bin/bash
For  (( i=1; i <= 2; i++ ))
do
    write Command here
    sleep 30
done

then set cron for this script crontab -e

(* * * * * /home/username/every30second.sh)

this cron call .sh file in every 1 min & in the .sh file command is run 2 times in 1 min

if you want run script for 5 seconds then replace 30 by 5 and change for loop like this: For (( i=1; i <= 12; i++ ))

when you select for any second then calculate 60/your second and write in For loop

Community
  • 1
  • 1
2

Have a look at frequent-cron - it's old but very stable and you can step down to micro-seconds. At this point in time, the only thing that I would say against it is that I'm still trying to work out how to install it outside of init.d but as a native systemd service, but certainly up to Ubuntu 18 it's running just fine still using init.d (distance may vary on latter versions). It has the added advantage (?) of ensuring that it won't spawn another instance of the PHP script unless a prior one has completed, which reduces potential memory leakage issues.

bnoeafk
  • 447
  • 4
  • 11
2

You can run that script as a service, restart every 30 seconds

Register a service

sudo vim /etc/systemd/system/YOUR_SERVICE_NAME.service

Paste in the command below

Description=GIVE_YOUR_SERVICE_A_DESCRIPTION

Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=YOUR_COMMAND_HERE
Restart=always
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

Reload services

sudo systemctl daemon-reload

Enable the service

sudo systemctl enable YOUR_SERVICE_NAME

Start the service

sudo systemctl start YOUR_SERVICE_NAME

Check the status of your service

systemctl status YOUR_SERVICE_NAME
1

Thanks for all the good answers. To make it simple I liked the mixed solution, with the control on crontab and the time division on the script. So this is what I did to run a script every 20 seconds (three times per minute). Crontab line:

 * * * * 1-6 ./a/b/checkAgendaScript >> /home/a/b/cronlogs/checkAgenda.log

Script:

cd /home/a/b/checkAgenda

java -jar checkAgenda.jar
sleep 20
java -jar checkAgenda.jar 
sleep 20
java -jar checkAgenda.jar 
jfajunior
  • 986
  • 12
  • 18
  • what does 1-6 represent? – Phantom007 Aug 15 '19 at 06:14
  • @Phantom007 1-6 represents Monday to Saturday, where "-" is a range and "0" is Sunday. Here is a good link that explains very well all the fields and where you can test it: "https://crontab.guru/#*_\*_\*_\*_1-6" – jfajunior Aug 20 '19 at 14:35
0

I just had a similar task to do and use the following approach :

nohup watch -n30 "kill -3 NODE_PID" &

I needed to have a periodic kill -3 (to get the stack trace of a program) every 30 seconds for several hours.

nohup ... & 

This is here to be sure that I don't lose the execution of watch if I loose the shell (network issue, windows crash etc...)

Thomas
  • 813
  • 6
  • 20
-1

Run in a shell loop, example:

#!/bin/sh    
counter=1
while true ; do
 echo $counter
 counter=$((counter+1))
 if [[ "$counter" -eq 60 ]]; then
  counter=0
 fi
 wget -q http://localhost/tool/heartbeat/ -O - > /dev/null 2>&1 &
 sleep 1
done
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Lo Vega
  • 59
  • 2
  • Even assuming that `60` should be a `30`, you might want to move that `wget` *inside* the `if` statement, otherwise it's executed every second. In any case, I'm not sure how this is any better than just a single `sleep 30`. If you were monitoring the actual UNIX time rather than your counter, it would make a difference. – paxdiablo Jul 13 '18 at 01:54
  • echo the counter print out, you can find out time delayed by EXCUTE COMMAND if NOT run wget in background. – Lo Vega Jul 18 '18 at 02:03
-1

Through trial and error, I found the correct expression: */30 * * ? * * * This translates to every 30 seconds. Reference: https://www.freeformatter.com/cron-expression-generator-quartz.html They have provided the expression for running every second: * * * ? * * * */x is used to run at every x units. I tried that on the minute's place and viola. I'm sure others have already found this out, but I wanted to share my Eureka moment! :D

Reeth
  • 36
  • 1
  • 7
-5

Why not just adding 2 consecutive command entries, both starting at different second?

0 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''
30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''
Arūnas Kiršis
  • 185
  • 1
  • 14
  • Just to be clear, this was downvoted because the first place is for minutes, not seconds, so this would have these two scripts running 30 minutes apart from one another. – lewsid Feb 11 '21 at 22:38