Why does an ack‑based cron job fail after applying CIS Benchmarks on AlmaLinux 8?
0 reputation · 17 May 2020, 11:54 UTC
0 reputation · 17 May 2020, 11:54 UTC
After hardening an AlmaLinux 8 system with the CIS Security Benchmarks, a cron job that uses ack to search a log file returns zero matches, while the same command works correctly when run from an interactive shell. This causes monitoring or alerting scripts to fail. What explains this discrepancy, and how can the cron job be fixed while remaining compliant with the benchmark?
35275 reputation · 17 May 2020, 22:28 UTC
When a command like ack works in an interactive shell but fails in a cron job after applying CIS (Center for Internet Security) Benchmarks, the discrepancy is typically caused by the difference between the interactive user environment and the restricted cron environment.
ack binary is installed in a location not included in the default cron PATH (e.g., /usr/local/bin)..bashrc or .bash_profile are not sourced.CIS Benchmarks often enforce strict PATH settings and remove unnecessary environment variables to reduce the attack surface. While your interactive shell loads a comprehensive PATH from your profile, crond typically uses a very minimal PATH (often just /usr/bin:/bin). If ack is located elsewhere, the cron job cannot find the executable, resulting in a failure that may manifest as "zero matches" if the script ignores the "command not found" error.
which ack in your interactive shell to find the full path (e.g., /usr/local/bin/ack).ack "pattern" /var/log/app.log to /usr/local/bin/ack "pattern" /var/log/app.log.PATH definition at the top of your crontab file to ensure all tools are reachable.
PATH=/usr/local/bin:/usr/bin:/bin>> /tmp/cron_debug.log 2>&1) to confirm that "command not found" errors have disappeared and matches are being returned.crontab -e.Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 17 May 2020, 22:33 UTC
When the CIS Benchmarks are applied, the cron_t SELinux type is tightened to block outbound sockets. If ack tries to send an acknowledgment over the network, the kernel silently denies the socket creation, and the script exits with no output. The job may also be dropped if the user isn’t listed in /etc/cron.allow.
To confirm, run:
ausearch -m avc -ts recent | grep cron
Any lines ending in denied point to the SELinux restriction. A quick fix is to add a permissive rule for ack or migrate the task to a systemd timer, which is not subject to the cron_t policy.