Logrotate: lines must begin with a keyword or a filename
Fix the Logrotate error lines must begin with a keyword or a filename caused by Windows CRLF line endings or invalid syntax in the configuration file.
- When Logrotate Produces This Error
- What Causes "lines must begin with a keyword or a filename" in Logrotate
- Windows-Style Line Endings (CRLF)
- Missing Opening or Closing Braces
- Missing endscript Keyword
- Invalid Directive or Typo
- How to Fix "lines must begin with a keyword or a filename" in Logrotate
- How to Verify the Fix
- Edge Cases and Variations
- Related Logrotate Errors
Logrotate produces the "lines must begin with a keyword or a filename" error when the configuration file contains Windows-style line endings (CRLF) or invalid syntax that Logrotate cannot parse.
When Logrotate Produces This Error
Logrotate displays the "lines must begin with a keyword or a filename" error when it reads a configuration file that contains characters it cannot interpret. The full error message includes the file path and line number:
error: /etc/logrotate.d/myapp:3 lines must begin with a keyword or a filename (possibly due to a missing end script or a missing '{' or '}')Logrotate produces this error during manual execution with
logrotate -d or
logrotate -f, and during the automated daily cron job. The error prevents Logrotate from processing the affected configuration file entirely.
What Causes "lines must begin with a keyword or a filename" in Logrotate
Windows-Style Line Endings (CRLF)
Logrotate encounters this error when a configuration file contains carriage return characters (
\r\n, known as CRLF line endings) instead of Unix-style line feeds (
\n, LF). This happens when the configuration file is created or edited on a Windows system and transferred to the Linux server via SFTP, SCP, or a shared folder. The
\r character is invisible in most editors but Logrotate interprets it as part of the directive name, which causes a parse failure.
Missing Opening or Closing Braces
Logrotate produces this error when a configuration block is missing its opening
{ or closing
} brace. Each log file block requires braces to delimit its directives. A missing brace causes Logrotate to misinterpret subsequent lines as top-level statements rather than block-level directives.
Missing endscript Keyword
Logrotate reports this error when a
prerotate,
postrotate,
firstaction, or
lastaction script block is not terminated with the
endscript keyword. Logrotate reads all subsequent lines as part of the script until it encounters something it cannot interpret.
Invalid Directive or Typo
Logrotate produces this error when a configuration file contains an unrecognized directive. Misspelled directives such as
compres instead of
compress or
rotat instead of
rotate trigger this parse error.
How to Fix "lines must begin with a keyword or a filename" in Logrotate
Convert Windows-style line endings to Unix format using
dos2unix. Install the utility if it is not present:sudo apt install dos2unixConvert the configuration file:
sudo dos2unix /etc/logrotate.d/{YOUR_APP}The
dos2unixutility removes all\rcharacters from the file, converting CRLF line endings to LF.Check the file for hidden carriage return characters if
dos2unixis not available:cat -A /etc/logrotate.d/{YOUR_APP}Lines ending with
^M$contain carriage returns. Lines ending with only$are correct. Remove carriage returns withsed:sudo sed -i 's/\r$//' /etc/logrotate.d/{YOUR_APP}Verify the configuration file syntax. Check that every log file block has matching opening and closing braces:
/var/log/myapp/*.log { daily rotate 7 compress }Verify that every script block ends with
endscript:postrotate systemctl reload myapp endscriptCheck for misspelled directives. Logrotate does not accept abbreviated or misspelled directive names. Review each line against the Logrotate directives reference.
Test the fixed configuration in debug mode:
sudo logrotate -d /etc/logrotate.d/{YOUR_APP}Logrotate should parse the file without errors and display the actions it would take.
How to Verify the Fix
Logrotate processes the configuration file without the parse error. Run debug mode and confirm clean output:
sudo logrotate -d /etc/logrotate.d/{YOUR_APP}Force a rotation to verify that the configuration works end-to-end:
sudo logrotate -vf /etc/logrotate.d/{YOUR_APP}Edge Cases and Variations
The error persists after running dos2unix.Check for other invisible characters such as byte order marks (BOM) at the beginning of the file. Remove BOM characters with:
sudo sed -i '1s/^\xEF\xBB\xBF//' /etc/logrotate.d/{YOUR_APP}The error points to a specific line but the line looks correct.The actual problem may be on a preceding line. A missing
} on an earlier block causes Logrotate to misparse all subsequent lines. Check the entire file, not only the reported line number.
The error appears after copying configuration from a web page.Web browsers may insert non-breaking spaces, smart quotes, or other Unicode characters that Logrotate cannot parse. Retype the configuration manually in a terminal editor such as
nano or
vi.
Related Logrotate Errors
Logrotate: logs are not rotating-- covers cases where Logrotate runs but skips rotation due to permissions or state file issues.
Logrotate: Permission denied-- covers filesystem and SELinux permission issues that block Logrotate operations.