3

I have a java executable jar file, which I am trying to run in a terminal.

I need to auto run the project so that the jar file runs whenever I start the PC like a system service.

anonymous2
  • 4,325

6 Answers6

0

Follow these steps.

  1. Go to /etc/init.d
  2. Add a file (say, example) and place the script you want to execute in that.
  3. Run the command chmod +x /etc/init.d/example (give it execute permissions)
  4. Add # chkconfig: 345 99 10 inside your script
  5. Save it
  6. Start the service with service example start.

This should work as system service/ startup service

Zanna
  • 72,312
0

You can do this by adding the command to the end of /etc/rc.local you'll have to use sudo access to edit this file.

So this command should work for you , replace your_executable.jar with your actual jar file.

sudo echo 'java -jar your_executable.jar' >> /etc/rc.local
storm
  • 5,013
0

First you have to have java installed in your ubuntu.

Second a jar file is not an executable file to run but you need java to run at startup, so you have to make a script , make it executable or put your commands in your /etc/rc.local file :

java -jar file.jar &
exit 0 

GOOD LUCK

Zanna
  • 72,312
0

Try this:

#!/bin/sh
#
# chkconfig: 345 50 83
#
### BEGIN INIT INFO
# Short description: Trigger Beamer on system startup
# Description: Trigger blupay beamer on system start up
### END INIT INFO

#Source function library
. /etc/rc.d/init.d/functions

#targets beamer config file
if [ -f /etc/sysconfig/yourapp]; then
    source /etc/sysconfig/yourapp
fi


#start httpd in the C locale by default.
YOURAPP_LANG=${YOURAPP_LANG-"java"}

# Path to .jar file.
yourapp=path/path_a/path_b
prog=app
pidfile=${PIDFILE-/var/run/yourapp/app.pid}
lockfile=${LOCKFILE-/var/lock/subsys/app}
RETVAL=0

start() { 
    echo -n "starting $prog"
    LANG=${YOURAPP_LANG}
    RETVAL=$?
    java -jar ${yourapp}/yourjarfilename.jar
    echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
stop() {
    echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d  $app
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$BEAMER_LANG $app$OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $beamer due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $app -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"app shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $app
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  force-reload|reload)
        reload
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart||force-reload|reload|status}"
        RETVAL=2
esac

exit $RETVAL  

Go to /etc/sysconfig/, create a file yourapp: Add the following contents:

#configuration file for blupay beamer

#
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
# Add options only if you want to
#OPTIONS=
#

#
# By default, bemaer process is started in the java locale; to 
# change the locale in which the server runs, the BEAMER_LANG
# variable can be set.
#
#YOURAPP_LANG=java
#

#
#By default, your app will create a app.pid 
#
#PID_FILE=/var/run/app/appp.pid
#

Now you should be able to run it as:

sudo service app start  

Be sure to replace app, YOURAPP with proper names

0

A simpler solution than running as a system service is to use supervisord. Please see full instructions at http://supervisord.org/

Short Version

sudo apt install -y supervisor

> cat /etc/supervisor/conf.d/tick.conf 
; tick config file
[program:tick]
command=/tmp/ticker.bash
environment=CTX="bogus-42"


> cat /tmp/ticker.bash
#!/bin/bash
while true ; do
  echo "tick   CTX='${CTX}'   SUPERVISOR_PROCESS_NAME=${SUPERVISOR_PROCESS_NAME}   $(date)"
  sleep 1
done


# ensure supervisor is running
> sudo systemctl restart  supervisor

# ensure tick task is running
> sudo supervisorctl start all
> sudo supervisorctl status
tick      RUNNING   pid 17946, uptime 0:06:11


> d /var/log/supervisor/*                                        
-rw-r--r-- 1 root root  1607 Nov 22 15:17 /var/log/supervisor/supervisord.log
-rw------- 1 root root     0 Nov 22 15:16 /var/log/supervisor/tick-stderr---supervisor-S4zZDy.log
-rw------- 1 root root 16548 Nov 22 15:19 /var/log/supervisor/tick-stdout---supervisor-5z8GTx.log


> sudo tail -3 /var/log/supervisor/tick-stdout---supervisor-5z8GTx.log
tick   CTX='bogus-42'   SUPERVISOR_PROCESS_NAME=tick   Tue Nov 22 15:20:12 PST 2016
tick   CTX='bogus-42'   SUPERVISOR_PROCESS_NAME=tick   Tue Nov 22 15:20:13 PST 2016
tick   CTX='bogus-42'   SUPERVISOR_PROCESS_NAME=tick   Tue Nov 22 15:20:14 PST 2016
0

I am using Ubuntu 16.04 with Xubuntu flavor. Following steps required to run runnable jar in startup.

  1. Create shell script which can run runnable jar
  2. Make that shell script executable.
  3. Go to Settings > Session and Startup

Session & Startup dialog

Add autostart application

Zanna
  • 72,312
Bhushan
  • 111