Could you advise me what to write in crontab so that it runs some job (for testing I will use /usr/bin/chromium-browser) every 15 seconds?
-
Instead of crontab, rely on some other event management system, for example: Perl's every.pm module will help you do this.Nikhil Mulley– Nikhil Mulley2012-01-07 18:22:59 +00:00Commented Jan 7, 2012 at 18:22
-
systemd can do thisphuclv– phuclv2017-08-24 02:40:15 +00:00Commented Aug 24, 2017 at 2:40
4 Answers
You can't go below one minute granularity with cron. What you can do is, every minute, run a script that runs your job, waits 15 seconds and repeats. The following crontab line will start some_job every 15 seconds.
* * * * * for i in 0 1 2; do some_job & sleep 15; done; some_job
This script assumes that the job will never take more than 15 seconds. The following slightly more complex script takes care of not running the next instance if one took too long to run. It relies on date supporting the %s format (e.g. GNU or Busybox, so you'll be ok on Linux). If you put it directly in a crontab, note that % characters must be written as \% in a crontab line.
end=$(($(date +%s) + 45))
while true; do
some_job &
[ $(date +%s) -ge $end ] && break
sleep 15
wait
done
[ $(date +%s) -ge $(($end + 15)) ] || some_job
I will however note that if you need to run a job as often as every 15 seconds, cron is probably the wrong approach. Although unices are good with short-lived processes, the overhead of launching a program every 15 seconds might be non-negligible (depending on how demanding the program is). Can't you run your application all the time and have it execute its task every 15 seconds?
-
could also be : for i in
1 2 3 4 ; do some_job & sleep 15; done(the last sleep won't affect the next run), but the way you do it is probably better for the timing of the outgoing cronjob mail (sent just after the last some_job, intead of 15s later in my variant)Olivier Dulac– Olivier Dulac2016-04-27 12:55:49 +00:00Commented Apr 27, 2016 at 12:55 -
Related unix.stackexchange.com/questions/478703/…Tim– Tim2018-10-30 17:08:59 +00:00Commented Oct 30, 2018 at 17:08
Different approach than the others: Run 4 cronjobs, each staggered by 15 seconds:
* * * * * sleep 00; timeout 15s some_job
* * * * * sleep 15; timeout 15s some_job
* * * * * sleep 30; timeout 15s some_job
* * * * * sleep 45; timeout 15s some_job
To prevent the job from interfering with itself, we limit its run-time to 15 seconds via GNU coreutils' timeout for each job. Note, however, that if the job fails to properly exit immediately at the end of 15s, you may still end up with problems. See the command manual for details on how to resolve that if it becomes a problem. Also note, if the command does take longer than 15s, and timeout kills it, you will get a non-zero exit status which will trigger a cronjob-email.
-
1Nice. If a process runs long then the next process would probably end up on a different core as well. Not sure if you can deliberately set core affinity for Cron jobs, but that might make this approach even better.Tim– Tim2018-01-03 11:39:59 +00:00Commented Jan 3, 2018 at 11:39
-
@Tim good point. I addressed that somewhat in an edit.Otheus– Otheus2018-02-05 10:50:24 +00:00Commented Feb 5, 2018 at 10:50
In short, cron is not that granular with time. The shortest period you will get is 1 min.
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .----- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
* * * * /usr/bin/chromium-browser
However you could write a script that would run every 15 seconds...
-
I have a line
* * * * * /usr/bin/chromium-browserin crontab. According to manual it should run every minute but nothing is running.xralf– xralf2011-04-11 19:12:58 +00:00Commented Apr 11, 2011 at 19:12 -
Is it difficult to write such a script? Should I ask another question or could it be written here?xralf– xralf2011-04-11 19:13:53 +00:00Commented Apr 11, 2011 at 19:13
-
Cron jobs don't have a terminal so I would expect the browser to fail. It may do so silently.BillThor– BillThor2011-04-11 19:14:58 +00:00Commented Apr 11, 2011 at 19:14
-
My cron logs are located in /var/log/cron for derivatives of redhat, ie. fedora, centos etc. It also may help for you to describe exactly what you are trying to do so people have a full picture of what is going on.rfelsburg– rfelsburg2011-04-11 19:17:23 +00:00Commented Apr 11, 2011 at 19:17
In your crontab:
* * * * /usr/bin/chromium-browser_starter
Then in /usr/bin/chromium-browser_starter:
#!/bin/sh
# chromium-browser_starter
#
# Schedules /usr/bin/chromium-browser to run every 15 seconds for a minute.
# Intended to be called every minute through crond(8).
for ((secs=0; secs<46; secs+=15)); do
(sleep $secs; /usr/bin/chromium-browser "$@") &
done
wait
-
When I write
ps aux | grep chromafter a minute I can't see chromium-browser running.xralf– xralf2011-04-11 19:53:24 +00:00Commented Apr 11, 2011 at 19:53 -
@xralf - The script only executes for a few seconds at most. Try grepping for
sleep.amphetamachine– amphetamachine2011-04-14 04:31:16 +00:00Commented Apr 14, 2011 at 4:31