A possible CRONjob error?

I have been reading the handbook on cronjobs lately in an attempt to learn more about FreeBSD and how to get the most out of it.

So, I recently wrote a small cronjob and implemented it. It runs daily along with the daily system cronjobs which are essentially built-into FreeBSD ver 14.4.

Here is my cronjob as I have written it:
Code:
#/bin/sh
# Automated Lynis Scan Script

#Define log and report paths
LOGFILE="/var/log/lynis-cron.log"
REPORTFILE="/var/log/lynis-report.dat"
/usr/local/bin/lynis "audit system --/usr/local/bin/lynis_cron.sh" --quick > $LOGFILE 2>&1

# Optional: Send an email alert if warnings are found in log
if grep -q "Warning:" $LOGFILE; then mail -s "Lynis Security Warning on $KensOffice" root < LOGFILE
fi

(I am suspicious that I should have [(space) , and (space) ] before and after both the if/then statement.) Like this:

Code:
 if [ grep -q "Warning:" $LOGFILE; ]
then [ mail -s "Lynis Security Warning on $KensOffice" root < LOGFILE ]

I am getting this message in root's mailbox every day.
Code:
Message 3:
From root@KensOffice Tue Jul  7 03:00:00 2026
From: Cron Daemon <root@KensOffice>
To: root
Subject: Cron <root@KensOffice> root    /usr/local/bin/lynis_cron.sh
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Tue, 07 Jul 2026 03:00:00 -0700

/bin/sh: root: not found

So, what does this "root: not found" mean?

Ken Gordon
 
/bin/sh: root: not found[/code]

So, what does this "root: not found" mean?
That's the error message sh gives when trying to execute something that doesn't exist. You can start /bin/sh from the console, say "foo", and you'll get the same "foo: not found" message. So your script attempted to execute "root" as a command.

So let's look at your script, and see whether the word "root" ever shows up. And see: it does! In the if statement you are wondering about. So something in the if statement if formatted wrong, and the word "root" gets interpreted as a command. But why? Looking at it quickly, I'm not sure about the reason, but I have a suspicion: you have not defined the variable KensOffice which you use in the mail command. Or maybe it's the way you formatted the if statement.
To begin with, you do NOT need the [ ] brackets around the logical test. The way the if statement works is this: It takes its first argument and executes it. Usually the first argument is the [ command, which is a real executable (and a version of it is also built into the shell itself to make it run faster). Usually, the [ command looks at all its arguments, evaluates them, and returns a simple 0 / 1 decision (you can read about it in "man test"). But the if command can in theory also be used to execute anything else (like the grep in your example), but in that case you have to be super careful with quoting and spacing.

Here's my suggestion: Simplify it and stretch it out. Example:
Code:
grep -q "Warning" $LOGFILE
grepstatus=$?

if [ $grepstatus -ne 0 ]
then
    cat $LOGFILE | mail -s ...
fi

This way you can test the script by adding print statements in places more easily.
 
Back
Top