S
S
Stanislav2015-03-04 21:27:05
bash
Stanislav, 2015-03-04 21:27:05

How to create a new log file every week?

In ubuntu, I can say that I am a beginner, I can’t figure out how to create a new file log.
The system is running node.js and npm forever, I start the project as follows
service app start
content app

#!/bin/bash
# see: https://github.com/nodejitsu/forever
# Based on:
# https://gist.github.com/3748766
# https://github.com/hectorcorrea/hectorcorrea.com/blob/master/etc/forever-initd-hectorcorrea.sh
# https://www.exratione.com/2011/07/running-a-nodejs-server-as-a-service-using-forever/

TIMESTAMP=`date +%F-%H%M`
NAME="Site.RU"
NODE_BIN_DIR="/usr/local/bin/node"
NODE_PATH="/usr/local/lib/node_modules"
APPLICATION_PATH="/media/user/web/node/site/bin/www"
PIDFILE="/var/run/site.ru.pid"
LOGFILE="/media/user/web/node/site/log/log-$TIMESTAMP.log"
MIN_UPTIME="5000"
SPIN_SLEEP_TIME="2000"
 

PATH=$NODE_BIN_DIR:$PATH
export NODE_PATH=$NODE_PATH
export NODE_ENV=production
 
start() {
    echo "Starting $NAME"
    forever \
      --pidFile $PIDFILE \
      -a \
      -l $LOGFILE \
      --minUptime $MIN_UPTIME \
      --spinSleepTime $SPIN_SLEEP_TIME \
      start $APPLICATION_PATH 2>&1 > /dev/null &
    RETVAL=$?
}
 
stop() {
    if [ -f $PIDFILE ]; then
        echo "Shutting down $NAME"
        # Tell Forever to stop the process.
        forever stop $APPLICATION_PATH 2>&1 > /dev/null
        # Get rid of the pidfile, since Forever won't do that.
        rm -f $PIDFILE
        RETVAL=$?
    else
        echo "$NAME is not running."
        RETVAL=0
    fi
}
 
restart() {
    stop
    start
}
 
status() {
    echo `forever list` | grep -q "$APPLICATION_PATH"
    if [ "$?" -eq "0" ]; then
        echo "$NAME is running."
        RETVAL=0
    else
        echo "$NAME is not running."
        RETVAL=3
    fi
}
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

As I understand it, the file creation time is cached and a new log file is not created by time. Help implement logging with a new file created every week.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2015-03-05
@ms-dred

You don’t have to worry about the programming language at all - figure out what the built-in logrotate service is, which does just that.
logrotate can rotate logs both by communicating with the application (sending it commands to reopen the file) and by itself doing truncate.
Read the brief manual and look on the internet for a few examples of configurations. If anything, ask in the comments more specifically.

J
jcmvbkbc, 2015-03-04
@jcmvbkbc

Usually, the tasks of logging and logging are separated: the application/daemon only logs to a file with a configured name, and when it receives a special signal, it closes the current file descriptor of the log file and opens a new one.
logrotate rotates the logs by renaming them and sending a signal to the application.
Probably on systems with systemd, it is not logrotate that is responsible for this, but some kind of systemd stray.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question