سؤال

I'm running a tiny script to autostart a node.js-service on my raspberry pi. For that reason, I call it from the end of my /etc/rc.local just before the exit 0.

The script is starting, no problem.

Now, my script looks like this:

#!/bin/bash
echo "Node.js starting..."
LOGFILE=log_`date +%b%d%H%M%S`.log
sudo /opt/node/bin/node /home/pi/www/js/script.js > /home/pi/node_logs/$LOGFILE

But I want to use logger instead. I gave it a try but fails. Can anybody explain to me how to "bind" logger to log only the output of node.js into this logfile? I guess I really misunderstood the way logger works as I'm really not a Linux-pro :-/

هل كانت مفيدة؟

المحلول

logger only logs to the syslog - if you really want to do things this way, then you would have to first edit your syslog.conf file and add something like this:

# node.js logging
local5.*                                                /home/pi/logs/nodejs.log

local5 is a predefined facility, make sure it is not in use elsewhere in your syslog conf. Now you need to restart the syslog service:

/etc/init.d/syslog stop; /etc/init.d/syslog start

And now you can use logger to log your messages:

sudo /opt/node/bin/node /home/pi/www/js/script.js | logger -p local5.info

To have a different log file for each date, you would have to use something like logrotate.

An alternative (better) solution would be to prepend the timestamp to each line before you log it:

while read msg; do
    echo -n "[$(date +"%d/%m/%Y %H:%M:%S")] " >> "$LOGFILE"
    echo "$msg" >> "$LOGFILE"
done < <(sudo /opt/node/bin/node /home/pi/www/js/script.js)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top