I'm running a small server on a BananaPi (ARMV7-A) with Ubuntu 16.04.02 LTS). To automate SSL certfifcate updates with Let's Encrypt certificates I created a script that's started regularly by a cron job. This script is needed because I have to change router configuration for the automatic certificate update to succeed (and I have to change it back afterwards).
I also restart Apache if a certificate renewal takes place. The output (stdout+stderr) of all important commands in this script is written to a temporary file and sent to me by mail.
If I start this script as root from the command line everything works as expected (restarting apache produces no output in the mail I receive). If the same script is started by cron (etc/crontab, user root) the line
systemctl restart apache2 >>${tmp_log_file} 2>&1
produces way too many lines of output on stderr (which is redirect into the specified file) and which I believe come from systemd:
Calling manager for RestartUnit on apache2.service, replace
Sent message type=method_call sender=n/a destination=org.freedesktop.systemd1 object=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=RestartUnit cookie=1 reply_cookie=0 error=n/a
Got message type=method_return sender=n/a destination=n/a object=n/a interface=n/a member=n/a cookie=1 reply_cookie=1 error=n/a
Sent message type=method_call sender=n/a destination=org.freedesktop.systemd1 object=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=GetUnit cookie=2 reply_cookie=0 error=n/a
Got message type=method_return sender=n/a destination=n/a object=n/a interface=n/a member=n/a cookie=3 reply_cookie=2 error=n/a
Sent message type=method_call sender=n/a destination=org.freedesktop.systemd1 object=/org/freedesktop/systemd1/unit/apache2_2eservice interface=org.freedesktop.DBus.Properties member=Get cookie=3 reply_cookie=0 error=n/a
[...]
Got message type=signal sender=n/a destination=n/a object=/org/freedesktop/systemd1/job/110031 interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=15 reply_cookie=0 error=n/a
Got message type=signal sender=n/a destination=n/a object=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=JobRemoved cookie=16 reply_cookie=0 error=n/a
Got result done/Resource temporarily unavailable for job apache2.service
While trying to narrow this down I already tried
- Determine the environment variables seen when the script is started by cron and creating the same environment when starting the script manually
- start the script manually whith </dev/zero >/dev/null as I expected this to remove a controlling terminal ("tty" says "not a tty")
- verify that the script is started as root in both cases
Whatever I tried, when started from the command line the output looks clean and when started by cron I get these strange messages.
This leads me to ask two questions
- What further differences (apart from environment and controlling terminal) can a script see between being called from the command line and by cron? I'd really like to understand this an not just somehow get it to work (the I would use grep to remove the offending lines).
- Does the last line of output which ends with "Resource temporarily unavailable for job apache2.service" actually show an error? At least apache seems to be restarted as requested.