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.
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
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
A simpler solution than running as a system service is to use supervisord
. Please see full instructions at http://supervisord.org/
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
Follow these steps.
/etc/init.d
example
) and place the script you want to execute in that.chmod +x /etc/init.d/example
(give it execute permissions)# chkconfig: 345 99 10
inside your scriptservice example start
.This should work as system service/ startup service
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
I am using Ubuntu 16.04 with Xubuntu flavor. Following steps required to run runnable jar in startup.
which java
)