10

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.

Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50

6 Answers6

10

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.
stacker-baka
  • 333
  • 6
  • 12
Adil
  • 2,092
  • 3
  • 25
  • 35
  • 1
    The [chkcrontab](https://github.com/lyda/chkcrontab) is now on Github and on [pypi.org](https://pypi.org/project/chkcrontab/) – Pablo Bianchi Mar 21 '20 at 20:34
8

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
Chris Adams
  • 2,721
  • 5
  • 33
  • 45
5

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'.

Adam Hawes
  • 5,439
  • 1
  • 23
  • 30
  • 3
    Most crons also require a newline at the end of the file. And not everybody installs cron entries interactively. If you are using some sort of automation or large scale configuration management system, you'll be able to easily/accidentally bypass any sanity checks that the system has as a defense mechanism. I'd strongly argue that lint for cron is an abundantly reasonable idea. (Indeed, searching for one is how I found this old question.) – wrosecrans May 25 '16 at 01:33
  • On Ubuntu you also need to provide the username after the time fields – Chris Beach Aug 01 '17 at 08:54
2

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.

Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    ssh admin@myhost kcron Error: cannot open display Not everyone has a GUI. – Adam Hawes Jan 26 '09 at 12:29
  • Well, you almost certainly have a GUI somewhere. Use ssh X forwarding and voilà - The window appears on your screen – phihag Jan 26 '09 at 14:08
  • 1
    Not if you're on one of my servers it won't. Installing enough X libs (plus GTK/Qt/KDElibs/etc) to get a graphical editor is a no-no on a serious server. Size reasons aside, we like to keep off everything that can present a possible attack vector. – Adam Hawes Jan 29 '09 at 05:08
2

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/

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
0

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

stacker-baka
  • 333
  • 6
  • 12