What is wrong with these cronjobs? - linux

My expectation is that the below instructions will make my script execute every 5 mins from 0930 to 1730 on weekdays.
30/5 9 * * 1,5 /home/main/.bin/somescript.sh
*/5 10-16 * * 1,5 /home/main/.bin/somescript.sh
0-30/5 17 * * 1,5 /home/main/.bin/somescript.sh
I installed cronie and tried to provide these instructions using crontab -e. I get the following output:
no crontab for main - using an empty one
crontab: installing new crontab
"/tmp/crontab.6hY2xA":1: bad minute
Invalid crontab file, can't install.
I read a tip somewhere that the first and last line can not have cronjob instructions, I've attempted that, but get the same error.
What is my mistake?

The crontab(5) man page (man 5 crontab) says:
Step values can be used in conjunction with ranges. Following a
range with "/" specifies skips of the number's value through
the range. For example, "0-23/2" can be used in the hours field to
specify command execution every other hour (the alternative in the V7 standard
is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after
an asterisk, so if you want to say "every two hours", just use
"*/2".
So a step value like "/5" can be used with a range or an asterisk, but not with a single number.

I had to edit the minute of the first line to 30-59/5

Related

Scheduling a program with cron with a very specific range

I need to run a script every 15 minutes starting at 9:30 AM until 4:00 PM Monday through Friday only. I want to use cron on my linux system.
My attempt looks something like this:
/15, 30 9-16 * * 1-5
My question is if cron will recognize the starting point as 9:30 or if there's another way to do this.
So here is a thought...I haven't tested it, but I plugged it into https://crontab.guru.
Could you try 2 separate entries in your crontab?
One that is the following:
30/15 9-16 * * 1-5 <your command>
And then a second one:
0-29/15 10-16 * * 1-5 <your command>
The first one starts at 9:30 and then runs for the 15 minute intervals between the second half hour (9:30, 9:45, 10:30, 10:45, etc.). The second one starts at 10:00 and only runs for the 15 minute intervals below 30 (10:00, 10:15, 11:00, 11:15, etc.). According to the output from crontab.guru, it looks like it might work.
It's best to use from 9AM to 4PM , as I'm not sure implementing 9.30 till end at 4 in single line
00,15,30,45 09-16 * * 1-5 command*****
If you want exactly start at 9.30, you would end up adding two lines.
30,45 09-15 * * 1-5 command
00,15 10-16 * * 1-5 command

Error in Crontab File

I am trying to run a python script every 5 minutes.
I type the following in terminal after doing crontab -e
*/5****/Users/nishantarora/Documents/cronjob.py (no spaces at all in between)
And this is what I get :
crontab: installing new crontab
"/tmp/crontab.TVJ8UDlEMk":1: bad hour
crontab: errors in crontab file, can't install
Please help me out.
As you mentioned, you did not place any space anywhere. It becomes then also very difficult do understand your line.
The format of a cron command is very much the V7 standard, with a number of upward-compatible extensions. Each line has five time and date fields, followed by a user name if this is the system crontab file,
followed by a command.
source: man 5 crontab
Since they talk about fields, it is evident that these must be white-space delimited.
The error you obtain, i.e. bad hour crontab, makes sense since you only have a single field which represents the minute field, that field, is a bit ill-formatted. I'm surprised it did not complain about that.
Solution to your problem, add a couple of spaces.
*/5 * * * * /Users/nishantarora/Documents/cronjob.py

How can I run my cron every 2 hours everyday which should starts from 12.30pm?

I want to run my cron job every 2 hours everyday which starts from 20.13pm.
Cron should run at 12.30,2.30,4.30 and so on.
Thanks in advance
You can find the documentation with man crontab.
Here you need to execute your command when minute is 30 so let's start with :
30 * * * * * your_command
You need to execute you command every 2 hours (actually every multiple of 2) :
30 */2 * * * your_command
The three last stars from left to right means every day of month, every month and every day of week. So you need to put :
30 */2 * * * your_command
You can use Crontab Generator for these kinds of headaches

How to create crontab to run every one minute using linux command?

I have the perl file named as perl.pl. I want this file to run and output the contents in new file using crontabs?
I dont know the steps how to perform it?Can anyone explain me with screenshots
You will need to open crontab by using:
crontab -e
This will open crontab in a text editor (usually this is vi or vim). Once inside the editor you will want to add:
* * * * * /usr/bin/perl <PATH_TO_DIR>/perl.pl
This will run perl.pl once every minute. The five asterisks mean that the script will run every minute, every hour, every day of month, every month and every day of the week.
For more information on how crontab works read over some of the examples on this page.
I'd suggest a search before asking a question like this: https://stackoverflow.com/a/5398044/225905. TL;DR using all * for hour, month, day, and year will give you crontab once a minute. For example:
* * * * * /path/to/php /var/www/html/a.php
Once a minute is quite frequent for cron, and cron won't repeat any faster than that. It may be better to have your perl script run in the background and repeat its calculation once a minute.

How would I get a cron job to run every 30 minutes?

I'm looking to add a crontab entry to execute a script every 30 minutes, on the hour and 30 minutes past the hour or something close. I have the following, but it doesn't seem to run on 0.
*/30 * * * *
What string do I need to use?
The cron is running on OSX.
Do:
0,30 * * * * your_command
crontab does not understand "intervals", it only understands "schedule"
valid hours: 0-23 -- valid minutes: 0-59
example #1
30 * * * * your_command
this means "run when the minute of each hour is 30" (would run at: 1:30, 2:30, 3:30, etc)
example #2
*/30 * * * * your_command
this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)
example #3
0,30 * * * * your_command
this means "run when the minute of each hour is 0 or 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)
it's another way to accomplish the same results as example #2
example #4
19 * * * * your_command
this means "run when the minute of each hour is 19" (would run at: 1:19, 2:19, 3:19, etc)
example #5
*/19 * * * * your_command
this means "run when the minute of each hour is evenly divisible by 19" (would run at: 1:19, 1:38, 1:57, 2:00, 2:19, 2:38, 2:57, 3:00 etc)
note: several refinements have been made to this post by various users including the author
Try this:
0,30 * * * * your command goes here
According to the official Mac OS X crontab(5) manpage, the / syntax is supported. Thus, to figure out why it wasn't working for you, you'll need to look at the logs for cron. In those logs, you should find a clear failure message.
Note: Mac OS X appears to use Vixie Cron, the same as Linux and the BSDs.
If your cron job is running on Mac OS X only, you may want to use launchd instead.
From Scheduling Timed Jobs (official Apple docs):
Note: Although it is still supported, cron is not a recommended solution. It has been deprecated in favor of launchd.
You can find additional information (such as the launchd Wikipedia page) with a simple web search.
You can use both of ',' OR divide '/' symbols. But, '/' is better. Suppose the case of 'every 5 minutes'. If you use ',', you have to write the cron job as following:
0,5,10,15,20,25,30,35,.... * * * * your_command
It means run your_command in every hour in all of defined minutes: 0,5,10,...
However, if you use '/', you can write the following simple and short job:
*/5 * * * * your_command
It means run your_command in the minutes that are dividable by 5 or in the simpler words, '0,5,10,...'
So, dividable symbol '/' is the best choice always;
You mention you are using OS X- I have used cronnix in the past. It's not as geeky as editing it yourself, but it helped me learn what the columns are in a jiffy. Just a thought.

Resources