2

How do I create a service for my java application where I have to run it with apache commons daemon jsvc. The service should be started at system boot. I have searched and searched, read and read and tried alot of stuff, and nothing seems to work.

I have created a .sh script in the /etc/init.d/path and run all the necessary commands that the "web" suggested like chmod 755 mydaemon.sh and or chmod +x mydaemon.sh, does not work when typed without the .sh, so I do not know why people suggest that. But it looks like this may be an old approach pre v.15 or so. Well at least I have found that some people say that it is deprecated and that systemd is the new black.

I have also tried to create a mydamon.service file in /etc/systemd/system pointing the ExecStart and Stop to my .sh script. I have done all the systemctl daemon-realod -> systemctl start myservice. Here is an example of my .service file

[Unit]
Description=Scheduler Test

[Service]
ExecStart=/etc/init.d/myscheduler.sh start
ExecStop=/etc/init.d/myscheduler.sh stop

[Install]
WantedBy=multi-user.target

here is an example of my .sh script

#!/bin/sh

### BEGIN INIT INFO
#
# Provides: myscheduler
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the myscheduler service
# Description: This file is used to start the daemon and should be placed in /etc/init.d
### END INIT INFO

NAME="myscheduler"
DESC="MyScheduler service"
EXEC=/usr/bin/jsvc
FILE_PATH="/usr/local/$NAME"
#JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
CLASS_PATH="$FILE_PATH/lib/commons-daemon-1.0.15.jar":"$FILE_PATH/myscheduler.jar"
CLASS="com.example.ApiApplication"
USER="root"
PID=$FILE_PATH/example.pid
LOG_OUT=$FILE_PATH/example.out
LOG_ERR=$FILE_PATH/example.err

jsvc_exec()
{
        cd $FILE_PATH
        $EXEC -home $JAVA_HOME -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS
}

case "$1" in
        start)
                echo "Starting the $DESC..."
                jsvc_exec
                echo "The $DESC has started."
                  ;;
        stop)
                echo "Stopping the $DESC..."
                jsvc_exec "-stop"
                echo "The $DESC has stopped."
                  ;;
        restart)
                if [ -f "$PID" ]; then
                        echo "Restarting the $DESC..."
                        jsvc_exec "-stop"
                        jsvc_exec
                        echo "The $DESC has restarted."
                else
                        echo "Service $DESC not running, no action taken."
                        exit 1
                fi
                        ;;
        *)
                echo "usage: daemon {start|stop|restart}" >&2
                exit 3
                ;;
esac

But nothing happens when I try to start it, currently it starts and stops immediately. It looks like my application cannot read one of my properties file which I am pointing to in the /etc/environment file:

quartzconfig="/usr/local/myscheduler/quartz.properties"

The main class of my application can be seen below:

public class ApiApplication implements Daemon{

    private static Scheduler scheduler;

    public static void main(String[] args) {
        try {
            String path = System.getenv("quartzconfig");
            System.out.println(path);
            Properties props = new Properties();

            Parameters parameters = new Parameters();
            FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                    .configure(parameters.properties().setFileName(path));

            Configuration config = builder.getConfiguration();
            props = ConfigurationConverter.getProperties(config);

            SchedulerFactory sf = new StdSchedulerFactory(props);
            scheduler = sf.getScheduler();

            scheduler.start();
        } catch (SchedulerException se){
            se.printStackTrace();
        } catch(ConfigurationException ioe){
            ioe.printStackTrace();
            System.err.println("Could not load properties... something went wrong!");
        }
    }

    @Override
    public void init(DaemonContext daemonContext) 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 process is stopped gracefully shutdown the scheduler
        scheduler.shutdown();
    }

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

And as I can see from the logging path is null.

But what is more strange is I can start my "jsvc" app if I forexample place my myscheduler.sh in ex. /home/john/Downloads/test and manually start it like sh myscheduler.sh start then it works fine, but cannot seem to make it work as a service and on system boot update-rc.d ....

I am new to linux, or at least not that well experienced, I am thinking that it might be some file folder permission access, I would love to just give it all a chmod -R 777 but I am well aware of the consequences and what it could do to my system, tried it once or twice before xD. Running Ubuntu 16.04 on VirtualBox so I just create a new image if it breaks.

  • Welcome to Ask Ubuntu! Please edit and try to ask only one question. You can ask post multiple questions if you want to. As it is now, it is hard to see which of your multiple questions you want to takle first. – MadMike Mar 15 '17 at 10:38
1

Found the solution, had to set EnvironmentFile=/etc/environment and Type=forking option in my .service file then it worked fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.