0

I'm trying to run a simple JSVC program. However it does not start when I run the script. I'm also not getting any errors in the log files, terminal or any syslogs I can find.

It does not seem that JSVC validates the class paths because if I purposely make them invalid I still don't get an error about it.

It DOES validate other parameters that it will complain about if wrong or missing so JSVC IS installed.

Java home should also be correct as it complained about that before.

Script:

#!/bin/sh

# Setup variables
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/default-java
CLASS_PATH="/usr/share/java/commons-daemon.jar":"/home/xxxuserxxx/Desktop/Tutorials/StackOverflow_version/EchoTest.jar"
CLASS=Main
USER=xxxuserxxx
PID=/tmp/example.pid
LOG_OUT=/tmp/example.out
LOG_ERR=/tmp/example.err

do_exec()
{
    $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 -wait 20 $CLASS
}

case "$1" in
    start)
        do_exec
            ;;
    stop)
        do_exec "-stop"
            ;;
    restart)
        if [ -f "$PID" ]; then
            do_exec "-stop"
            do_exec
        else
            echo "service not running, will do nothing"
            exit 1
        fi
            ;;
    *)
            echo "usage: daemon {start|stop|restart}" >&2
            exit 3
            ;;
esac

Code:

import java.util.Timer;
import org.apache.commons.daemon.*;

public class Main implements Daemon {


private static Timer timer = null;

public static void main(String[] args) {
    System.out.println("Hello");
    timer = new Timer();
    timer.schedule(new EchoTask(), 0, 1000);
}

@Override
public void init(DaemonContext dc) throws DaemonInitException, Exception {
    System.out.println("initializing ...");
}

@Override
public void start() throws Exception {
    System.out.println("starting ...");
    main(null);
}

@Override
public void stop() throws Exception {
    System.out.println("stopping ...");
    if (timer != null) {
        timer.cancel();
    }
}

@Override
public void destroy() {
    System.out.println("done.");
}

}

import java.util.Date;
import java.util.TimerTask;

public class EchoTask extends TimerTask {
    @Override
    public void run() {
        System.out.println(new Date() + " running ...");
    }
}

1 Answers1

0

Ok I think I made an error setting up all the constants.

The code was based on this Q/A: How to convert a java program to daemon with jsvc?

It started working after I removed the wait parameter and expanded the full class/package path in the class parameter.

The error and output files are working, they must have been fixed by something I did earlier.. I guess.