I am new to crontab and I have two questions, one is an example that I think should be working... and the other is a format question
I execute the following:
EDITOR=emacs crontab -e
And I put in
* * * * * * say test
When I quit out it says
crontab: installing new crontab
But I don't hear the test voice coming every minute like it should.
I used this site as a reference for the format. And I notice they use six fields. However I've inherited a crontab at work that only uses 5. Can you leave out the last? How does the cron know?
Unless OSX is doing something wonky, crontabs only have 5 time fields, and you've got 6. it's
minute hour day_of_month month day_of_week
So, most likely your crontab is trying to execute a script named *, with say test as an argument.
Related
In my current directory, I have a bash script called run_job.sh. This script runs perfectly fine.
I'm trying to schedule this script to run every 10 minutes using a cronjob. Here is the code that I am using:
*/10 * * * * run_job.sh
Now, when I do this, I get the following error:
-bash: */10: No such file or directory when running
I'm new to cron jobs so I'm not sure why I'm getting this error. Any help would be much appreciated.
The error message in your question suggests that you passed the crontab snippet to Bash somehow. That's not how you schedule a job; you type the command crontab -e at the Bash prompt and edit your Cron schedule in your favorite editor. That's where you would type in this snippet. When you save the file and exit the editor, cron will take your new schedule into use.
However, you should be aware that your normal PATH and other features of your interactive environment will not be available to cron jobs. At a minimum, you will probably need to specify the path to your script. If it's in $HOME/bin/run_job.sh, that's what you need to put in the final field in the crontab entry. (There may be more tweaks you have to do which can't be inferred from the information you have provided; see e.g. CronJob not running for further tips.)
What you are showing is the scheduling line from crontab (see man crontab). Further, I recommend against the /10 format, as it is not accepted by cron on all operating systems. To be safe, it is better to be explicit, as in:
0,10,20,30,40,50 * * * * run_job.sh
I wrote a very simple script to copy files from one dir into another one, I want to create a cronjob - by the way, I don't have "crontab" here but "fcrontab" instead since scheduler isn't cron but fcron - so it runs once a week - on Sundays - but I'm not sure whether if it's correct or not. Can you take a look and tell me so?
this is the cronjob:
0 1 * 1-12 SUN /home/myusername/dir/cp.sh
or
0 1 * 1-12 SUN /bin/bash /home/myusername/dir/cp.sh
I've read quite a few posts from here as well as over the web but I'm still confused. Thanks in advance for your answers.
The weekday field contains a number where 0 represents Sunday. There is no support for human-readable weekdays in any crontab variant I have come across.
If you want this to execute every month, just put * for the month.
0 1 * * 0 /home/myusername/dir/cp.sh
As long as the script is executable (chmod +x cp.sh) and has a valid shebang (#!/bin/bash as the very first line of the file) you don't need to explicitly tell the OS to run it with bash, just like on the command line.
crontab runs your jobs from your home directory so you could replace /home/myusername with . if this is running from myusername's account. If $HOME/dir is in your PATH you only need cp.sh (but take care to set the PATH where cron, too, can see it!)
Any output or error messages will be sent by email if your server is set up to handle that. This is slightly obscure and sometimes bewildering, especially if you don't actually have email properly configured, so many users add a redirection to a log file for every cron job.
0 1 * * 0 cp.sh >>cp.sh.log 2>&1
(Some beginners like to redirect everything to /dev/null and then come here to ask us what's wrong when there's an error. Of course, we don't know, either.)
The Stack Overflow crontab tag info page has syntax advice, troubleshooting tips, and a link to a site where you can generate a valid crontab from a newbie-friendly form where you just click buttons and move sliders to say when your job should run.
I have a Python3 program that I need to execute 2 minutes past every hour.
Unable to get python to execute directly from cron, I’ve written a shell script with the commands in it.
The crontab to execute this shell script is this:
*/2 * * * * bash /home/john/PYTHONS/MASTER.sh
The shell script is simple. I’ve tried various versions, including this:
#!/bin/bash
cd /home/john/PYTHONS/
python3 ~/PYTHONS/COMPLEXPYTHON.py
python3 ~/PYTHONS/SIMPLEPYTHON.py
and including this:
#!/bin/bash
cd /home/john/PYTHONS/GDAXDEV/VPUMP
python3 $HOME/PYTHONS/COMPLEXPYTHON.py
python3 $HOME/PYTHONS/SIMPLEPYTHON.py
Here’s the issue:
If I execute the shell script from the command line BOTH python scripts run just fine.
If I execute the shell script from cron, ONLY THE SECOND python script runs. The first one just doesn’t run at all. And, I can’t seem to find out why, or how to fix it.
Any suggestions or help would be greatly appreciated.
I see questions about this issue all around, and no single one of them solved the issue for me. Several hours of testing every combination of variables lead me to a solution that’s reliable.
I’m no longer using a shell script, but invoking python directly in cron.
First, we did this:
$ which python3
/home/john/[path to python]/bin/python3
So, we added the shebang in every one of the python files in the project:
#!/home/john/[path to python]/bin/python3
And, in cron we used the full path to that python3, as well as the full path to the WORKING.py file we wanted to run. We want the thing to run 5 minutes past every hour so:
5 * * * * /home/john/[path to python]/bin/python3 /home/john/[path to dir]/WORKING.py
However, the log files this produced were being placed in /home/john/, and I wanted them in the specific directory. Also, there are a load of additional python files in there – with the same names as python files in nearby directories – so I wanted to be certain that any call gets the right one.
So, I added a CD command at the beginning of the cron command:
5 * * * * cd /home/john/[path to dir]/ && /home/john/[path to python]/bin/python3 /home/john/path to dir]/WORKING.py
It’s been running reliably ever since.
I’m grateful for the resources at stackoverflow and the great and fast help I’ve received in this community, and I hope this entry helps save someone else a whole afternoon of frustration.
I am creating the cron on Mac OS Terminal, here is the code:
contents of home.cron:
* * * * * /users/username/desktop/forTrump/script.sh
then I run it like this:
crontab home.cron
then I get this error:
"home.cron":0: bad minute crontab: errors in crontab file, can't install
Here is the shell file that is opened above, I tested and this runs perfectly by executing ./script.sh from os terminal but I can't seem to run it in cron w/o getting that error.
#!/bin/sh
python /Users/username/Desktop/forTrump/test.py
I already ensure that there were no line breaks causing the bad minute error, thanks in advance for your help.
I have encountered with similar issue today:
"/tmp/crontab.vjQAiZ" 1L, 14C written
crontab: installing new crontab
"/tmp/crontab.vjQAiZ":0: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit?
Same error was raised even I tried to add ABSOLUTELY correct entry in my crontab list.
As usual I have leart google advices. But notihng has helped.
The solution in my case: 100% usage of /var/log/ directory.
It looks like cron can't update own logs and therefore writes similar error when you would like to save your changes.
When I deleted extra log files, issue was solved.
Good luck!
This error (especially if the error indicates that the invalid minute is at column 0) will also occur if the volume where cron files are stored is out of space.
According to crontab.guru both * * * * * and *\1 * * * * are essentially the same thing although you may want to try the alternative just in case your system does not like one or the other.
However in your posted home.cron the path is wrong.
This:
* * * * * /users/username/desktop/forTrump/script.sh
should be:
* * * * * /Users/username/Desktop/forTrump/script.sh
However if the script requires elevated privileges then you will need to use the root crontab and the line used is slightly different due to the addition of a user column before the command like this:
* * * * * root /Users/username/Desktop/forTrump/script.sh
To install that script as root first switch to root like this:
sudo su
followed by:
crontab home.cron
Also seeing you are on Mac OS X make sure the line in home.cron is terminated with just \n and not \r or \n\r
See also: https://www.dougv.com/2006/12/fixing-a-bad-minute-error-message-when-trying-to-use-crontab-with-certain-unix-text-editors/
This problem most often occurs because you’re using a text editor, such as pico, that fakes word wrapping by adding a newline when it reaches a certain column position.
Crontab delimits jobs with line breaks (newlines). Each job occupies one line. Therefore, if crontab sees anything other than an integer in the first column of a line, it throws the “bad minute” error, since the minute argument is the first one crontab encounters.
The simple fix is to go to delete the line breaks added by pico / your *nix editor. You can most easily do that by putting your cursor on the first character of each extra line, then hit the backspace key until that line is joined back up with the previous one, and repeating the process until your entire cron command is on one line.
In my case I got this error inside a docker container. The reason for the error is that there was an empty line at the bottom of the file, removing which the issue went away.
When exactly shall your cron job run - every minute?
m h dom mon dow command
*/1 * * * * /users/username/desktop/againstTrump/script.sh
Is there anything like lint for crontab? I'd like to know that i've got all my spaces and stars sorted out without waiting for something to not work.
There's a Python linter for crons. See chkcrontab project
You can install it via pip:
pip3 install chkcrontab
Example usage:
chkcrontab /etc/cron.d/power-schedule
Checking correctness of /etc/cron.d/power-schedule
E: 15: 0 12 * foo * * root echo hi
e: FIELD_VALUE_ERROR: foo is not valid for field "month" (foo)
e: INVALID_USER: Invalid username "*"
E: There were 2 errors and 0 warnings.
I've found CronWTF to be incredibly helpful when writing crontabs - it translates your stars and commands into something more human friendly, to make it easier to read strange cron jobs.
Better yet, because it's all javascript you can run it locally, and noone need know about your top sekrit cron jobs.
Another alternative if you code ruby is to use the whenever gem - you use a sample ruby file called schedule.rb to parse, and generate crontabs from like so:
every 10.minutes do
command "/usr/bin/my_great_command"
end
Will give you a crontab entry of
0,10,20,30,40,50 * * * * /usr/bin/my_great_command
And this one here:
every 2.days, :at => '4:30am' do
command "/usr/bin/my_great_command"
end
Will give you:
30 4 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * * /usr/bin/my_great_command
I don't think you need a lint for crontab. There's 5 fields that are space separated then a space then the command to run and its args finish off the line.
Also, on Ubuntu at least, crontab won't let you save a bum file. I just tried a few things and it barfed on all of them. I guess that means that crontab is its own 'lint for cron'.
It might be a bit off, but an easy way would be to just load it with a graphical crontab editor like kcron or gcrontab. If you need to call it in a script, this question is about how to do it in php.
I'm not sure if this is the sort of thing you're looking for, but it makes writing crontabs really easy by showing you exactly what you're setting the schedule to:
https://crontab.guru/
You can try shell script named 48-verifycron from Wicked Cool Shell Scripts, 2nd Edition,
if you can't access python and pip to use chkcrontab