This question already has an answer here:
Is `*/1 * * * *` and `* * * * *` equivalent in CRON?
(1 answer)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Which one is the correct syntax for running a job at every hour?
0 0 * * * *
0 0 */1 * * *
Also how are they both different?
The correct syntax for every hour job is 0 * * * *.
But you can use both 0 0 * * * * and 0 0 */1 * * *
Since */1 means every 1 hour/minute/second like the *.
Related
Starting file (/etc/crontabs/root):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
I want to replicate this file, remove comments... and adding the content to the same file, so I run:
sed '/^#/ d; s/\/etc/\/root\/etc/' /etc/crontabs/root >> /etc/crontabs/root
... which should remove all comments, replace /etc with /etc/root... and add the result to the same file.
Wrong output (please note the "extra" line starting with */15):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
*/15 * * * * run-parts /root/etc/periodic/15min
0 * * * * run-parts /root/etc/periodic/hourly
0 2 * * * run-parts /root/etc/periodic/daily
0 3 * * 6 run-parts /root/etc/periodic/weekly
0 5 1 * * run-parts /root/etc/periodic/monthly
*/15 * * * * run-parts /root/root/etc/periodic/15min
Expected/wanted output:
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
*/15 * * * * run-parts /root/etc/periodic/15min
0 * * * * run-parts /root/etc/periodic/hourly
0 2 * * * run-parts /root/etc/periodic/daily
0 3 * * 6 run-parts /root/etc/periodic/weekly
0 5 1 * * run-parts /root/etc/periodic/monthly
Ideas?
You shouldn't write out to the same file that you're reading from. It will read the lines that you added to the file, and continue processing them. So when it gets to the lines with /root/etc, it will replace the /etc in that, producing /root/root/etc. It could conceivably get stuck in an infinite loop, since it keeps extending the file and will never reach the end.
What you can do is copy the crontab file to a new file, then use sed to append to that.
cp /etc/crontabs/root /tmp/new_crontab
sed '/^#/ d; s/\/etc/\/root\/etc/' /etc/crontabs/root >> /tmp/new_crontab
cp /tmp/new_crontab /etc/crontabs/root
If you want a one liner, it's fairly straightfoward using ed:
printf "%s\n" ka '1,$t' "'a,\$g/^#/d" "'a+1,\$s/\/etc/\/root\/etc/" w | ed -s /etc/crontabs/root
or a bit easier to read using a heredoc:
ed -s /etc/crontabs/root <<'EOF'
ka
1,$t
'a,$g/^#/d
'a+1,$s/\/etc/\/root\/etc/
w
EOF
I have 4 types of cron jobs that run at regular intervals. Is it possible to convert the cron format into an interval format?
cron | interval
----------------------
* * * * * | 60 seconds
0 * * * * | 3600 seconds
30 * * * * | 3600 seconds
0 0 * * * | 86400 seconds
Are you looking to convert the CRON string into a human-readable format or just into seconds?
If the former, I found a few options depending on your programming language of choice (I'm sure there are more out there for other languages):
cron-expression-descriptor for C#
cRonstrue for Javascript/Typescript
cron-parser for Java
the fugit gem does exactly what I need
require 'fugit'
c = Fugit::Cron.parse('0 0 * * *')
p c.rough_frequency
=> 86400
I have a cron job that i want to run every second minute of every hour.before i would just run it every minute like
* * * * * /var/www/html/cron.php
but i now need it to run every second minute of ever hour. How can this be done?.
If your operating system is FreeBSD you could use the #every_second for example:
#every_second /var/www/html/cron.php
For other systems, this could work:
Somethinig every hour:
#hourly /var/www/html/cron.php
Or every 45 minutes:
*/45 * * * * /var/www/html/cron.php
From man 5 crontab:
string meaning
------ -------
#reboot Run once, at startup of cron.
#yearly Run once a year, "0 0 1 1 *".
#annually (same as #yearly)
#monthly Run once a month, "0 0 1 * *".
#weekly Run once a week, "0 0 * * 0".
#daily Run once a day, "0 0 * * *".
#midnight (same as #daily)
#hourly Run once an hour, "0 * * * *".
#every_minute Run once a minute, "*/1 * * * *".
#every_second Run once a second.
Also, check this a reference: https://crontab.guru/
In case the system you are using doesn't support the #every_second you could give a try to something like:
* * * * * /var/www/html/cron.php
* * * * * (sleep 30; /var/www/html/cron.php)
Basically, they run at the same time (every minute) but one will wait for 30 seconds before starting, so /var/www/html/cron.php will be called every 30 seconds.
The format for crontab is as follow
m h dom mon dow command
So you can do
0 * * * yourcommand
It will run every hour at 00 minutes
For me, the only solution I could get working is:
2-59/* * * * *
Which translates to simple English as:
At every 60th minute from 2 through 59.
I barely know about cron syntax, so not sure if this is the only way or is at least one of the better ways.
I need my crontab to execute every 2 hours starting at 10am and the last one running at 10pm.
I currently have
0 */2 * * * /directory/job.sh
How do I block out the hours I don't want?
Thanks!
0 10/2 * * * /directory/job.sh would do it.
0 10-22/2 * * * /directory/job.sh would do it and be more explicit.
0 10,12,14,16,18,20,22 * * * /directory/job.sh would do it, too.
I am new to crontab, can someone just tell me how often this task goes.
Thanks.
0 8 * * *
Is it every 8 minutes?
It means every day at 8:00 am
You have to set
*/8 * * * * path to script
every 8 minut