Skip to content

Commit 94c2f59

Browse files
author
Claire Adams
committed
Add check for max number of allowed characters
According to the crontab man pages, the maximum number of characters in the command field is 998 https://manpages.debian.org/jessie/cron/crontab.5.en.html. This PR adds a check to catch lines that are longer than 998. It updates the test file as well. This change was requested in this issue #22 and I have run into issues with crontab failing because of lines being too long, so having the linter catch this issue will be a big help.
1 parent 468d696 commit 94c2f59

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

chkcrontab_lib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
FILE_RE_WHITELIST = [re.compile(x) for x in
8484
(r'\.in$', r'\.cron$', r'\.disabled$', r'^(\S+\.)?cron\.d$')]
8585

86+
# Per https://manpages.debian.org/jessie/cron/crontab.5.en.html:
87+
# "The maximum permitted length for the command field is 998 characters."
88+
MAX_COMMAND_LENGTH = 998
8689

8790
class FSM(object):
8891
"""Finite State Machine.
@@ -738,6 +741,9 @@ def ValidateAndLog(self, log):
738741
log.LineWarn(log.MSG_BARE_PERCENT, 'A bare % is a line break in'
739742
' crontab and is commonly not intended.')
740743

744+
if len(self.command) > MAX_COMMAND_LENGTH:
745+
log.LineError(log.MSG_COMMAND_LENGTH_ERROR, 'Command length is %d which is greater'
746+
' than %d (max allowed characters)' % (len(self.command), MAX_COMMAND_LENGTH))
741747

742748
class CronLineAt(CronLineTimeAction):
743749
"""For cron lines specified with @ time specs."""
@@ -885,6 +891,7 @@ class LogCounter(object):
885891

886892
_msg_kinds = set(('BARE_PERCENT',
887893
'CHKCRONTAB_ERROR',
894+
'COMMAND_LENGTH_ERROR',
888895
'FIELD_PARSE_ERROR',
889896
'FIELD_VALUE_ERROR',
890897
'INVALID_AT',

tests/test_crontab

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ OK_SPACE=" "
6969
@monthly root Good @ Command
7070
# FAIL 1 for bad time spec.
7171
@m0nthly root Bad @ Command
72+
# FAIL 1 for command too long
73+
0 21 * * wed root /bin/bash/myscript "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"

0 commit comments

Comments
 (0)