(I figured this out, it was multiple problems...I'm leaving this as is and explain the process I took to resolve in my answer to this question)
I'm working on a simple Ruby script that runs as a cron job. I'm using Visual Studio Code and the Remote-Containers extension which makes it easy to spin up a Ruby dev environment which runs in a Docker container.
This Docker container is running Debian 10 (Buster). I have tested the script and it runs okay:
sudo /usr/local/bin/ruby /workspaces/main.rb
I've used crontab -e
to add the cron jobs:
* * * * * /usr/local/bin/ruby /workspaces/main.rb
* * * * * sleep 30; /usr/local/bin/ruby /workspaces/main.rb
At first there wasn't any syslog
at all. So I installed rsyslog and logs started going into /var/log/syslog
but there were only a few. Those relating to cron where all about edits I was making with crontab:
May 21 14:29:56 cec3901197dc crontab[3347]: (vscode) LIST (vscode)
May 21 14:30:00 cec3901197dc crontab[3362]: (vscode) BEGIN EDIT (vscode)
I edited /etc/rsyslog.conf
and changed:
#cron.* /var/log/cron.log
to:
cron.* /var/log/cron.log
I then killed and restarted the rsyslogd process:
sudo kill -9 3056
sudo rsyslogd
I can send a test message to the syslog and see it is running:
logger test
Looking at /var/log/syslog
I see the appropriate log entry has been added:
May 21 15:50:29 cec3901197dc vscode: test
I can also see that rsyslog is successfully writing crontab log entries to /var/log/cron.log
:
May 21 15:44:15 cec3901197dc crontab[16490]: (vscode) BEGIN EDIT (vscode)
May 21 15:46:27 cec3901197dc crontab[16490]: (vscode) END EDIT (vscode)
But I'm only seeing these sort of crontab entries, nothing about the cron jobs executing. I'm guessing my jobs may be configured incorrectly...and I've tried a few different variations with none providing any log entries.
Still, even if they are configured incorrectly shouldn't cron still log the errors they produce to cron.log
?
Is there something else I need to get more verbose messages from cron? Or is there something I can change about the commands that will provide more verbose messages?
Update - 5/21/21
I tried this on my local Ubuntu install. I had to login as root using sudo -i
and add to the root's crontab. This worked - I began seeing the logs in my /var/log/
folder.
However, using root on my VSC container didn't work. Based on comparing the logging from my local Ubuntu and the VSC instance I suspect the issue is that syslog is now working fine and the issue is that the cron scheduler isn't actually running...that's why only crontab related entries are showing up in the syslog.
Why isn't cron running?
Answer: The Docker container doesn't start cron by default. Running cron
from the prompt will start it running and it will begin logging correctly to /var/log/cron.log
.
However, even with the cron job executing it still isn't creating the appropriate current_users
or user_changes
files at /var/log/
.
Configuration Status Now
- Still using VSC Remote Containers Ruby Docker container.
- Installed and started
rsyslogd
. - Edit
/etc/rsyslog.conf
to redirect cron related logging tocron.log
(from mainsyslog
) - Kill and restart
rsyslogd
to ensure latest conf is picked up. - Use
sudo -i
to access root. - As root add crontab jobs using
crontab -e
:
* * * * * ruby /workspaces/main.rb
* * * * * sleep 30; ruby /workspaces/main.rb
- Check that cron is actually running the jobs in
cron.log
:
May 21 18:41:01 cec3901197dc CRON[45188]: (root) CMD (sleep 30; ruby /workspaces/main.rb)
May 21 18:41:01 cec3901197dc CRON[45190]: (root) CMD (ruby /workspaces/main.rb)
- Check to see if
current_users
oruser_changes
has been updated, nope. - Check that script can run correctly at command prompt
ruby /workspaces/main.rb
. Yup, runs just fine. Creates appropriate files.
So now I'm really confuzzled. The cron job is running the ruby script but it is not creating the files. I can run the ruby script myself and it does create the files. What gives?