7

I want to create cron job that runs a script every 5 seconds. Seeing that cron jobs only allows increments of minutes 0-59 and so on.

I thought to create another script that calls my original script written below.

#!/bin/bash

while true
do
# script in the same directory as this script. is this correct?
bash makemehappy.sh
sleep 1
done

I now, need to know how to run this script every time i boot my computer and for it to start itself if it isn't running for some reason.

I am also aware that running this script every minute wouldn't be a good thing. :)

if there is an easier way to run a script every 5 seconds please advise.

Please and thank you.

Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101
  • 8
    Trying to do something every 5 seconds is really the best solution to your problem? – user347594 Jul 26 '10 at 00:59
  • Possible duplicate of [Running a cron every 30 seconds](https://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds) – Guss Nov 30 '18 at 12:54

6 Answers6

8

I wouldn't use cron for this. I would use that bash script (use an absolute path, unless you want it to be portable and know that the directory structure will be preserved).

Instead, I would just sleep 5, just like you did (only 5 seconds instead of 1).

As far as starting it with your system, that depends on the system. On (some) Linux distros, there's a file called /etc/rc.local in which you can add scripts to run when the system starts. Well... I shouldn't be so general, the distros that I have used have this. If you're running Ubuntu, there is no longer an inittab, they use upstart, btw.

So if you have an endless loop and an entry in /etc/rc.local, then you should be golden for it to run endlessly (or until it encounters a problem and exits).

TCCV
  • 3,142
  • 4
  • 25
  • 30
  • My distro is ubuntu? I can't seem to find that rc.local file in etc directory. suggestions? – Mahdi Yusuf Jul 26 '10 at 01:05
  • what version? (cat /etc/issue) – TCCV Jul 26 '10 at 01:09
  • 10.04 I managed to figure it out. :) by adding it to init.d and updating the update-rc.d – Mahdi Yusuf Jul 26 '10 at 02:13
  • nice! hope that works for you. (any chance of choosing my answer if it helped you? Not trying to be rude, I'm just trying to build my rep, lol) – TCCV Jul 26 '10 at 02:36
  • 2
    note that sleep 5 doesn't guarantee Your code will be executed exactly every 5 seconds, only that it will be at least 5 seconds between subsequent executions. – Paweł Polewicz Oct 29 '10 at 01:15
  • That is a good point. If the system's under heavy load, it could take some time to process the next iteration. (or am I missing something else?) – TCCV Nov 02 '10 at 23:37
2

Try using anacron or, better yet, an init script to start when the computer starts.

If you want the script to "restart itself", you'll need to run something every few minutes to check the original is still running. This can be done in inittab (/etc/inittab) or, on Ubuntu, /etc/event.d. Try man 5 inittab, looking at the 'respawn' option.

Borealid
  • 95,191
  • 9
  • 106
  • 122
1

Init scripts are fine at boot, but don't detect if a process fails and has to be restarted. supervisord does a great job of detecting failed processes and restarting them. I'd recommend a script with a 5-second loop like @Tim described, but wrap supervisord around it to make sure it keeps running.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
1

Some crons have an @reboot time specifier (this covers all the time and date fields). If yours does, you can use that. If this is a "system" service (rather than something running for yourself), the other solutions here are probably better.

wnoise
  • 9,764
  • 37
  • 47
0

As explained in detail in my answer to a similar question, you can use SystemD timer units with whatever schedule that you want - down to a theoretical 1 nanosecond schedule with no sleep kludges

Quick overview:

  1. Setup a SystemD service to run what you want - this can be as simple as:

/home/myusuf3/.config/systemd/user/makemehappy.service

[Unit]
Description=Make me happy
[Service]
ExecStart=/home/myusuf3/.local/bin/makemehappy.sh
  1. Setup a SystemD timer with the schedule that you want, as documented in man systemd.timer:

/home/myusuf3/.config/systemd/user/makemehappy.timer

[Unit]
Description=Schedule to make me happy
[Timer]
OnBootSec=5
OnUnitActiveSec=5
AccuracySec=1
  1. Enable and start the timer:

:

systemctl --user daemon-reload
systemctl --user enable makemehappy.timer
systemctl --user start makemehappy.timer

(after you enable it, it will autostart every time you start the computer, but you probably want to start it now anyway).

Guss
  • 30,470
  • 17
  • 104
  • 128
0

To answer the question in the title, this is how to run a cronjob every 5 seconds :

* * * * * /path/to/script.sh
* * * * * sleep 5 && /path/to/script.sh
* * * * * sleep 10 && /path/to/script.sh
* * * * * sleep 15 && /path/to/script.sh
* * * * * sleep 20 && /path/to/script.sh
* * * * * sleep 25 && /path/to/script.sh
* * * * * sleep 30 && /path/to/script.sh
* * * * * sleep 35 && /path/to/script.sh
* * * * * sleep 40 && /path/to/script.sh
* * * * * sleep 45 && /path/to/script.sh
* * * * * sleep 50 && /path/to/script.sh
* * * * * sleep 55 && /path/to/script.sh

It's not beautiful, but this solution comes with no extra tools nor dependencies. So if you have working cron jobs, this should work right away.

mastef
  • 2,787
  • 1
  • 6
  • 16
  • Maybe `* * * * * for i in {1..10}; do /path/to/script.sh & sleep 5; done` can be taken into account. It is concise. reference: https://stackoverflow.com/a/59718683/1204713 – Justme0 Oct 03 '22 at 05:29
  • @Justme0 but then you depend on how long the `script.sh` execution takes - which will delay every further step. – mastef Oct 11 '22 at 03:48
  • See https://stackoverflow.com/a/43066502/10728554 – mastef Oct 12 '22 at 17:33