When dealing with daemons managed by Upstart, logging output becomes crucial for debugging and monitoring. The behavior differs between Ubuntu versions, particularly when moving from 0.3.9 (Hardy) to 0.6.x (Lucid).
In Upstart 0.3.9, you could use the console logged
stanza to capture output:
description "My Daemon Service"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
console logged
exec /path/to/your/daemon
This would automatically log to /var/log/upstart/your-daemon.log
. However, this method was deprecated in later versions.
For newer Upstart versions, consider these approaches:
Method 1: Direct Output to syslog
description "Modern Daemon Service"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
exec /path/to/daemon 2>&1 | logger -t mydaemon
This pipes both stdout and stderr to syslog, viewable in /var/log/syslog
.
Method 2: Custom Log File
description "Custom Log Daemon"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
script
exec /path/to/daemon >> /var/log/mydaemon.log 2>&1
end script
The transition period requires special handling. If you need compatibility across versions:
description "Cross-version Daemon"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
script
# For 0.3.9
if [ -e /etc/init.d/.upstart-version ] &&
[ "$(cat /etc/init.d/.upstart-version)" = "0.3.9" ]; then
exec /path/to/daemon
else
# For 0.6.x
exec /path/to/daemon >> /var/log/mydaemon.log 2>&1
fi
end script
Remember to configure log rotation in /etc/logrotate.d/
:
/var/log/mydaemon.log {
weekly
missingok
rotate 4
compress
delaycompress
notifempty
create 640 root adm
}
For systems migrating to systemd, the logging approach changes completely:
[Unit]
Description=My Daemon Service
[Service]
ExecStart=/path/to/daemon
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=mydaemon
[Install]
WantedBy=multi-user.target
When managing daemons with Upstart on Ubuntu servers, one common pain point is capturing the application's stdout/stderr output. The behavior varies significantly between Upstart versions, particularly when moving from Hardy (0.3.9) to Lucid (0.6.x) and later.
In Ubuntu Hardy (0.3.9), the console logged
stanza directs output to the system log file:
# Example Upstart job for 0.3.9
description "My Daemon Service"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
console logged
exec /usr/local/bin/mydaemon --verbose
This logs to /var/log/daemon.log
by default. You can verify with:
grep 'My Daemon' /var/log/daemon.log
For Ubuntu Lucid (0.6.5) and later, console logged
was deprecated. Instead, use these approaches:
Method 1: Direct to syslog
description "Modern Daemon"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
# Log to syslog with custom facility
console none
exec /usr/local/bin/mydaemon 2>&1 | logger -t mydaemon
Method 2: Custom Log File
description "File-Logged Daemon"
author "Your Name"
start on runlevel [2345]
stop on runlevel [016]
console none
script
exec /usr/local/bin/mydaemon >> /var/log/mydaemon.log 2>&1
end script
For production systems, create a logrotate config at /etc/logrotate.d/mydaemon
:
/var/log/mydaemon.log {
weekly
missingok
rotate 12
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
/usr/bin/killall -HUP mydaemon || true
endscript
}
When troubleshooting:
- Check
initctl log-priority
to verify logging level - Use
initctl emit --no-wait mydaemon
for manual triggering - Monitor with
tail -f /var/log/syslog | grep mydaemon