7

I found a script that I can use for my jar file. My question is how can I modify it to log stdout and stderr to file.

As I understand that this line should be modified:

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

What does "/tmp" indicate? /dev/null means it is not directing anuthing to anywhere?

Here is the script I'm using:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac

I would like to log everything to /var/log/myservice.log file. Any help is appreciated!

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
lkallas
  • 1,310
  • 5
  • 22
  • 36
  • I found this very useful for running a jar and choosing logfile - https://stackoverflow.com/questions/31939849/spring-boot-default-log-location – Wil Sep 12 '19 at 13:39

1 Answers1

5

I found this solution:

nohup java -jar $PATH_TO_JAR > /var/log/myservice.log  2>&1 &

When I deleted the log file while the servce was running it didn't create a new one altough it was streaming to stdout.

lkallas
  • 1,310
  • 5
  • 22
  • 36
  • 2
    A couple of comments: 1) 2>&1 means takes stderr (which is the second stream) and redirect that to stdout (which is the first) 2) You might want to do >> instead of > for redirecting the output (> will overwrite any log file that was there; >> will append; not sure on your use case which is the appropiate one). 3) Having your system delete an old log file and create a new one is definitely a separate question) – Foon Sep 18 '15 at 18:21
  • When you remove a file from a directory, you don't actually remove it from the disk. Any program which has the file open can still use it. ALso if the file exists in any other directory, it will continue to do so. BTW can you can still access the file by looking in the `/proc/{pid}/fd/1` – Peter Lawrey Sep 18 '15 at 19:22