Logger in cron script does not work

I tried this script to be called by cron / Scheduled Tasks, but logger does not write anything into syslog. Started from CLI the output of the script is written into syslog. I did run out of ideas to find the cause ...

#!bin/sh
/bin/busybox logger "TEST script"

Thanx, Peter

Just write:

#!/bin/sh

logger "example"
logger -p notice -t example "example notice"
logger -p err -t example "example notice"

Output:

Fri May  8 00:23:26 2020 user.notice root: example
Fri May  8 00:23:31 2020 user.notice example: example notice
Fri May  8 00:23:40 2020 user.err example: example notice

More information:

# logger --help
BusyBox v1.30.1 () multi-call binary.

Usage: logger [OPTIONS] [MESSAGE]

Write MESSAGE (or stdin) to syslog

        -s      Log to stderr as well as the system log
        -t TAG  Log using the specified tag (defaults to user name)
        -p PRIO Priority (numeric or facility.level pair)

With PRIO:

emerg
alert
crit
err
warning
notice
info
debug

thanx vuhuy

I forgot, the output in syslog is:
Fri May 8 00:54:00 2020 cron.info crond[11630]: USER root pid 12247 cmd /etc/crontabs/test.sh

in a cron script, the path of all called programms must be qualified, so "logger "example" does not work, it has to be chaned to "/usr/bin/logger "example" to run in a cron script. But still it does not generate output into syslog.

Peter

No it doesn't have to be qualified. My crons logs are running fine. I just double-checked it for you.

From a script called by cron:

/usr/bin/logger "testbin"
logger "testdirect"

Syslog output:

Fri May  8 01:03:00 2020 user.notice root: testbin
Fri May  8 01:03:00 2020 user.notice root: testdirect

Both direct and fully qualified work. It's /usr/bin/logger by the way for logger.

Here's why the logger command doesn't have to be qualified:

# echo $PATH
/usr/sbin:/usr/bin:/sbin:/bin

As you can see, the root user has the /usr/sbin, /usr/bin, /sbin, and /bin paths in its PATH environment variable. I can see from your log output that you're also running the script as root (technically, cron is running as root, and therefore also the script). Since the logger binary is located in /usr/bin, and the /usr/bin folder is mentioned in the PATH environment variable, it doesn't need to be fully qualified in order to work.

did you try it in a script, called fron cron?

for example in " Scheduled Tasks":

          • /etc/crontabs/test.sh

and the script "test.sh":

/usr/bin/logger "testbin"
logger "testdirect"

Peter

Yes I did ofcourse. I specially doubled checked it for you by setting up a cron.

Fri May  8 01:14:00 2020 cron.info crond[6176]: USER root pid 8231 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:14:00 2020 user.notice root: testbin
Fri May  8 01:14:00 2020 user.notice root: testdirect
Fri May  8 01:15:00 2020 cron.info crond[6176]: USER root pid 8432 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:15:00 2020 user.notice root: testbin
Fri May  8 01:15:00 2020 user.notice root: testdirect
Fri May  8 01:16:00 2020 cron.info crond[6176]: USER root pid 8638 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:16:00 2020 user.notice root: testbin
Fri May  8 01:16:00 2020 user.notice root: testdirect
Fri May  8 01:17:00 2020 cron.info crond[6176]: USER root pid 8775 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:17:00 2020 user.notice root: testbin
Fri May  8 01:17:00 2020 user.notice root: testdirect
Fri May  8 01:18:00 2020 cron.info crond[6176]: USER root pid 8981 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:18:00 2020 user.notice root: testbin
Fri May  8 01:18:00 2020 user.notice root: testdirect
Fri May  8 01:19:00 2020 cron.info crond[6176]: USER root pid 9153 cmd /root/test.sh >/dev/null 2>&1
Fri May  8 01:19:00 2020 user.notice root: testbin
Fri May  8 01:19:00 2020 user.notice root: testdirect
Fri May  8 01:21:00 2020 user.notice root: testbin
Fri May  8 01:21:00 2020 user.notice root: testdirect

It's running for a while now. The last two entries are even run with cronloglevel set to 9 (only display errors).

Thank You. Then I wonder why it does not work on my OpenWrt 19.07.2

Did you chmod your script to have execution rights? Like this

chmod +x /etc/crontabs/test.sh

If not, the logs do mention its being called from cron, but it doesn't really execute so nothing inside the script is being run.

great !!
I erased anything else in the test-script, now it gives back the output into syslog.
I will erase line by line, I guess some line generates an error
exec rights were OK.

Hehe, always run the script itself directly in the shell to see errors. Don't debug in cron, or redirect the output in a cron to a file for debugging purposes.

*/1 * * * * /root/test.sh >> /root/output.txt 2>&1

Redirects the standard out and standard error both to /root/output.txt.

Thank You for Your hint, I will follow it.
I learned it now by wasting a lot of time.

1 Like

I found my tomatoes on my eyes. I started the script with
#!bin/sh
instead of
#!/bin/sh

this is why it did run well started by CLI but not by cron.

Peter