run jar-file on startup linux - linux

I've used a standard bash-startup script for autostarting my Teamspeak Server - which works perfectly. Now I've done the same for the JTS3 Server Mod, problem: It's a .jar-file and doesn't start. If I run the .jar/startscript manually it works just fine.
Here's the script in /etc/init.d/
#!/bin/bash
### BEGIN INIT INFO
# Provides: jts3servermod
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: JTS3 Server Mod
### END INIT INFO
# INIT Script by www.SysADMINsLife.com
######################################
# Customize values for your needs: "User"; "DIR"
USER="ts"
DIR="/home/ts/JTS3ServerMod"
###### Teamspeak 3 server start/stop script ######
case "$1" in
start)
su $USER -c "${DIR}/jts3servermod_startscript.sh start"
;;
stop)
su $USER -c "${DIR}/jts3servermod_startscript.sh stop"
;;
restart)
su $USER -c "${DIR}/jts3servermod_startscript.sh restart"
;;
status)
su $USER -c "${DIR}/jts3servermod_startscript.sh status"
;;
*)
echo "Usage: {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
of course it is linked in the runlevel-folders with update-rc.d jts3servermod defaults.
the servermod startscript (jts3servermod_startscript.sh) is:
#!/bin/sh
# JTS3ServerMod Linux start script
# Created by Stefan "Stefan1200" Martens
# The author of this script is not responsible for any damage or data loss!
JAVA_COMMANDLINE_PARAMETERS="-mx30M" # You can add java arguments here, like the -mx30M argument!
JTS3SERVERMOD_COMMANDLINE_PARAMETERS="" # You can add JTS3ServerMod arguments here, like the -config and -log argument!
BINARYPATH="$(pwd)" # This have to point to the JTS3ServerMod directory!
# Don't change the lines below, if you are not a sh script expert!
cd "${BINARYPATH}"
BINARYNAME="JTS3ServerMod.jar"
ROOTUID="0"
case "$1" in
java)
if which java >/dev/null 2>&1 ; then
echo "Java is already installed:"
java -version
else
if [ "$(id -u)" -ne "$ROOTUID" ] ; then
echo "Start this script as root to start the automatic installation of the Java runtime environment."
echo "You can also read the system requirements of the JTS3ServerMod in the readme.txt file for a manual installation of the Java runtime environment."
exit 6
else
read -p "Do you wish to install the Java runtime environment? (y/n) " yn
case $yn in
[Yy]* ) installJava; break;;
* ) echo "Aborted!"; exit 6;;
esac
fi
fi
;;
start)
if ! which java >/dev/null 2>&1 ; then
echo "The JTS3ServerMod needs the Java runtime environment installed to run!"
echo "Start this script with the java argument as root to start the automatic installation of the Java runtime environment:"
echo "$0 java"
echo "You can also read the system requirements of the JTS3ServerMod in the readme.txt file for a manual installation of the Java runtime environment."
exit 6
fi
if [ "$(id -u)" -eq "$ROOTUID" ] ; then
echo "For security reasons it is prefered not to run the JTS3ServerMod as root!"
fi
if [ -e jts3servermod.pid ]; then
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "The JTS3ServerMod is already running, try restart or stop!"
exit 1
else
echo "jts3servermod.pid found, but no JTS3ServerMod running. Possibly your previously started JTS3ServerMod crashed!"
echo "Please view the logfile for details."
rm -f jts3servermod.pid
fi
fi
echo "Starting the JTS3ServerMod..."
if [ -e "$BINARYNAME" ]; then
java ${JAVA_COMMANDLINE_PARAMETERS} -jar ${BINARYNAME} ${JTS3SERVERMOD_COMMANDLINE_PARAMETERS} > /dev/null &
PID=$!
ps -p ${PID} > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "JTS3ServerMod could not start!"
else
echo $PID > jts3servermod.pid
echo "JTS3ServerMod started, for details please view the log file!"
fi
else
echo "Could not find the file $BINARYNAME, aborting!"
exit 5
fi
;;
stop)
if [ -e jts3servermod.pid ]; then
echo -n "Stopping the JTS3ServerMod.."
if ( kill -TERM $(cat jts3servermod.pid) 2> /dev/null ); then
c=1
while [ "$c" -le 120 ]; do
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo -n "."
sleep 1
else
break
fi
c=$(($c+1))
done
fi
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "JTS3ServerMod is not shutting down cleanly - killing!"
kill -KILL $(cat jts3servermod.pid)
else
echo "done"
fi
rm -f jts3servermod.pid
else
echo "No JTS3ServerMod running (jts3servermod.pid is missing)!"
exit 7
fi
;;
restart)
$0 stop && $0 start || exit 1
;;
status)
if [ -e jts3servermod.pid ]; then
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "JTS3ServerMod is running!"
else
echo "JTS3ServerMod seems to have died!"
fi
else
echo "No JTS3ServerMod running (jts3servermod.pid is missing)!"
fi
;;
*)
echo "Usage: ${0} {start|stop|restart|status|java}"
exit 2
esac
exit 0
I also tried to write an own simple script for only starting the jar and catching the pid and writing it into a file.
None of these scripts wanted to start my .jar file during the startup.
As you can see, the first script is a template for starting Teamspeak Servers during boot. I only replaced some stuff with my Server Mod paths and scripts and put into /etc/init.d/ (chmod 755 was executed of course).
Maybe some of you have other approaches to solve that strange circumstances.

I don't know if the problem was solved in the meantime, but while experimenting with the same issue. I figured out that the script could not find the jar file. So I added:
#!/bin/sh
# JTS3ServerMod Linux start script
# Created by Stefan "Stefan1200" Martens
# The author of this script is not responsible for any damage or data loss!
cd "$(dirname "$0")"
...
At the beginning of the jts3servermod_startscript.sh to ensure that the "working directory" is correct.
BTW - My init script:
jts-bot
#!/bin/sh
### BEGIN INIT INFO
# Provides: jts-bot
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: JTS3-Bot for TeamSpeak
### END INIT INFO
USER="teamspeak"
DIR="/home/teamspeak/JTS3ServerMod"
###### JTS3ServerMod start/stop script ######
case "$1" in
start)
su $USER -c "${DIR}/jts3servermod_startscript.sh start"
;;
stop)
su $USER -c "${DIR}/jts3servermod_startscript.sh stop"
;;
restart)
su $USER -c "${DIR}/jts3servermod_startscript.sh restart"
;;
status)
su $USER -c "${DIR}/jts3servermod_startscript.sh status"
;;
*)
echo "Usage: -bash {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0

Related

Start an Electron app at boot on Raspberry Pi 3 with yocto

I built an image with X11 using yocto for a Raspberry Pi 3 and a touchscreen. I can start my app built with Electron (chromium) by running commands manually in a serial session:
export DISPLAY=:0
/usr/lib/node/electron/dist/electron --no-sandbox /home/root/app
I though to use an init.d script to do it automatically at startup but I'd like to do it the proper way. I tried to create an .Xsession file in my user directory with the commands above but it doesn't work and I don't know if I can get logs of what happened.
According to this wiki, there is a lot of steps at X11 startup. Currently, I only see a Terminal (from Matchbox I guess) and a mouse cursor.
What's the "standard" way to start an app with the system and is there a way to remove the cursor for a touchscreen?
Edit
Here is the content of my /etc/X11 directory:
Xsession
Xsession.d/
xinit/
xorg.conf
Xsession:
#!/bin/sh
if [ -x /usr/bin/dbus-launch ]; then
# As this is the X session script, always start a new DBus session.
eval `dbus-launch --sh-syntax --exit-with-session </dev/null`
echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
fi
. /etc/profile
if [ -f $HOME/.profile ]; then
. $HOME/.profile
fi
SYSSESSIONDIR=/etc/X11/Xsession.d
export CLUTTER_DISABLE_MIPMAPPED_TEXT=1
for SESSIONFILE in $SYSSESSIONDIR/*; do
set +e
case "$SESSIONFILE" in
*.sh)
. "$SESSIONFILE"
;;
*.shbg)
"$SESSIONFILE" &
;;
*~)
# Ignore backup files
;;
*)
"$SESSIONFILE"
;;
esac
set -e
done
exit 0
xorg.conf: empty.
Xsession.d/:
13xdgbasedirs.sh
30xinput_calibrate.sh
89xdgautostart.sh
90XWindowManager.sh
89xdgautostart.sh:
XDGAUTOSTART=/etc/xdg/autostart
if [ -d $XDGAUTOSTART ]; then
for SCRIPT in $XDGAUTOSTART/*; do
CMD=`grep ^Exec= $SCRIPT | cut -d '=' -f 2`
$CMD &
done
fi
90XWindowManager.sh:
if [ -x $HOME/.Xsession ]; then
exec $HOME/.Xsession
elif [ -x /usr/bin/x-session-manager ]; then
exec /usr/bin/x-session-manager
else
exec /usr/bin/x-window-manager
fi
There is also a file /etc/xserver-nodm/Xserver:
#!/bin/sh
# This script is only needed to make sure /etc/X11/xserver-common
# can affect XSERVER, ARGS & DPI: otherwise systemd could just use
# /etc/default/xserver-nodm as EnvironmentFile and sysvinit could just
# source the same file
. /etc/profile
# load default values for XSERVER, ARGS, DISPLAY...
. /etc/default/xserver-nodm
# Allow xserver-common to override ARGS, XSERVER, DPI
if [ -e /etc/X11/xserver-common ] ; then
. /etc/X11/xserver-common
if [ ! -e $XSERVER ] ; then
XSERVER=$(which $XSERVER)
fi
fi
if [ -n "$DPI" ] ; then
ARGS="$ARGS -dpi $DPI"
fi
exec xinit /etc/X11/Xsession -- $XSERVER $DISPLAY $ARGS $*
and a file /etc/rc5.d/S09xserver-nodm:
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: xserver
# Required-Start: $local_fs $remote_fs dbus
# Required-Stop: $local_fs $remote_fs
# Default-Start: 5
# Default-Stop: 0 1 2 3 6
### END INIT INFO
killproc() { # kill the named process(es)
pid=`/bin/pidof $1`
[ "$pid" != "" ] && kill $pid
}
read CMDLINE < /proc/cmdline
for x in $CMDLINE; do
case $x in
x11=false)
echo "X Server disabled"
exit 0;
;;
esac
done
case "$1" in
start)
. /etc/profile
#default for USER
. /etc/default/xserver-nodm
echo "Starting Xserver"
if [ "$USER" != "root" ]; then
# setting for rootless X
chmod o+w /var/log
chmod g+r /dev/tty[0-3]
# hidraw device is probably needed
if [ -e /dev/hidraw0 ]; then
chmod o+rw /dev/hidraw*
fi
fi
# Using su rather than sudo as latest 1.8.1 cause failure [YOCTO #1211]
su -l -c '/etc/xserver-nodm/Xserver &' $USER
# Wait for the desktop to say its finished loading
# before loading the rest of the system
# dbus-wait org.matchbox_project.desktop Loaded
;;
stop)
echo "Stopping XServer"
killproc xinit
sleep 1
chvt 1 &
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 { start | stop | restart }"
;;
esac
exit 0
The correct way to define a complete X session depends on your session manager: on Yocto that is often matchbox-session or mini-x-session. From your description I'd guess you're using mini-x-session (it happens to start a terminal and a window-manager if session file is not found).
Quoting mini-x-session:
if [ -e $HOME/.mini_x/session ]
then
exec $HOME/.mini_x/session
fi
if [ -e /etc/mini_x/session ]
then
exec /etc/mini_x/session
fi
So adding a /etc/mini_x/session script should work.
By the way, in your session file you may also want to start a window manager (X can do weird things without one):
your-app-here &
exec matchbox-window-manager

Can't resart liquidsoap without killing process manually

I am currently running Liquidsoap on Ubuntu 14.4, streaming to Icecast, hosted on the same box.
My setup is running correctly, however when running sudo service liquidsoap restart, I get the following error:
fatal error exception unix.unix_error(50, "bind", "" )
In order to restart liquid soap, I need to kill the process or reboot.
It then runs correctly. Until I need to restart for whatever reason.
As a side note, liquidsoap created a user and group called liquidsoap, however I am running sudo commands via another user I created.
Does anyone have any ideas?
Fixed by enabling pid file creation.
Copy of my init.d - https://gist.github.com/anonymous/d7e232fc280d2fe1df56
#!/bin/sh
### BEGIN INIT INFO
# Provides: liquidsoap
# Required-Start: $remote_fs $network $time
# Required-Stop: $remote_fs $network $time
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts the liquidsoap daemon
# Description:
### END INIT INFO
user=liquidsoap
group=liquidsoap
prefix=/usr
exec_prefix=${prefix}
confdir=/etc/liquidsoap
liquidsoap=${exec_prefix}/bin/liquidsoap
rundir=/var/run/liquidsoap
# Test if $rundir exists
if [ ! -d $rundir ]; then
mkdir -p $rundir;
chown $user:$group $rundir
fi
case "$1" in
stop)
echo -n "Stopping liquidsoap channels: "
cd $rundir
has_channels=
for liq in *.pid ; do
if test $liq != '*.pid' ; then
has_channels=1
echo -n "$liq "
start-stop-daemon --stop --quiet --pidfile $liq --retry 4
fi
done
if test -n "$has_channels"; then
echo "OK"
else
echo "no script found in $confdir"
fi
;;
start)
echo -n "Starting liquidsoap channels: "
cd $confdir
has_channels=
for liq in *.liq ; do
if test $liq != '*.liq' ; then
has_channels=1
echo -n "$liq "
start-stop-daemon --start --quiet --pidfile $rundir/${liq%.liq}.pid \
--chuid $user:$group --exec $liquidsoap -- -d $confdir/$liq
fi
done
if test -n "$has_channels"; then
echo "OK"
else
echo "no script found in $confdir"
fi
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload}"
exit 1
;;
esac
None of mentioned solutions solved my issue. I had to fix it recently by setting proper ownership and permissions right after starting daemons. After killing the processes and restarting, further restarts work fine.
start-stop-daemon --start --quiet --pidfile $rundir/${liq%.liq}.pid \
--chuid $user:$group --exec $liquidsoap -- -d $confdir/$liq && sleep 1 && chmod 600 $rundir/${liq%.liq}.pid && chown root:root $rundir/${liq%.liq}.pid

script doesn't start on boot with init

I have the following script
#!/bin/sh
# chkconfig: 345 99 01
# description: some startup script
### BEGIN INIT INFO
# Provides: weblogic
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop OurDB
# Description: OurDB is a very fast and reliable database
# engine used for illustrating init scripts
### END INIT INFO
. /etc/rc.d/init.d/function
service=startWebLogic.sh
user=*******
password=******
dbschemaname=******
hostname=*******
port=********
weblogic_start()
{
pgrep -f startWebLogic.sh > /dev/null
if ! [ $? -eq 0 ]; then
echo "exit" | sqlplus -L "$user/$password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$hostname)(Port=$port))(CONNECT_DATA=(SID=$dbschemaname)))" | grep "Connected to" > /dev/null
if [ $? -eq 0 ]; then
/home/usr/Oracle/Middleware/Oracle_Home/user_projects/domains/bin/startWebLogic.sh &
else
echo "WARNING! No connection to the Oracle Server" | mail -s OracleServerDown monitoring#accedia.com
fi
else
echo "WARNING! Running WebLogic service was found!" | mail -s "Service Already Running" monitoring#example.com
fi
}
weblogic_stop()
{
pgrep -f startWebLogic.sh > /dev/null
if [ $? -eq 0 ]; then
/home/usr/Oracle/Middleware/Oracle_Home/user_projects/domains/Econt/bin/stopWebLogic.sh & pid=$! ; sleep 5m; pkill -TERM -P $pid
ps -ef| grep $pid
if [ $? -ne 0 ];then
pkill -TERM -P $pid
fi
pid=`ps -ejH|grep "startWebLogic" | grep -iv "grep" | awk '{print $1}'`
pkill -TERM -P $pid
else
echo "WARNING! No running WebLogic service was found" | mail -s "WebLogic Not Found" monitoring#example.com
fi
}
case $1 in
start)
weblogic_start
;;
stop)
weblogic_stop
;;
*) echo "Invalid input"
;;
esac
I've put it properly in init.d and stuff, chmod-ed it properly , put it in rc.local and yet again it doesn't want to start on boot, though if i run it manually , passing an argument to it ,e.g. "service weblogic start" it works fine , both as root and not. Anyone has any suggestions why is it acting like this and is there any solution to it ?
You don't need to put it in init.d and rc.local both places. One is enough.
You may try this after you put it in init.d:
For Debian based OS:
sudo update-rc.d script_name defaults
For CentOS:
chkconfig --add myscript
Found a solution, the problem was with the priorites , I have changed the chkconfig options to 345 98 10 from 345 99 01.

init script process pid not working

So I have red5 streaming server installed and can get it to launch and stay launched when closing ssh terminal window using the following init.d script code:
#!/bin/bash
# For RedHat and cousins:
# chkconfig: 2345 85 85
# description: Red5 flash streaming server
# processname: red5
# Created By: Sohail Riaz (sohaileo#gmail.com)
PROG=red5
RED5_HOME=/usr/local/red5
DAEMON=$RED5_HOME/$PROG.sh
PIDFILE=/var/run/$PROG.pid
# Source function library
. /etc/rc.d/init.d/functions
[ -r /etc/sysconfig/red5 ] && . /etc/sysconfig/red5
RETVAL=0
case "$1" in
start)
echo -n $"Starting $PROG: "
cd $RED5_HOME
$DAEMON >/dev/null 2>/dev/null &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo $! > $PIDFILE
touch /var/lock/subsys/$PROG
fi
[ $RETVAL -eq 0 ] && success $"$PROG startup" || failure $"$PROG startup"
echo
;;
stop)
echo -n $"Shutting down $PROG: "
killproc -p $PIDFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
;;
restart)
$0 stop
$0 start
;;
status)
status $PROG -p $PIDFILE
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL
So I can just type 'red5 start', 'red5 status' and 'red5 stop'.
Now I renamed the red5 folder to red5bak and have been testing a new updated version in a new 'red5' folder which also uses a red5.sh file to launch. I can launch this manually no problem and works as intended however when launched with the above commands and code it says started 'OK' but appears not to and when using 'status' command I get the error red5 dead but pid file exists.
I tried deleting the pid red5 files in var/run and lock folders and restarting server but still get the same issue.
If I delete the newer red5 folder and rename the old folder back then the script works again so how can I get it to work for the newer version/files?
Any help would be appreciated,
Thanks.
The file is listed in your init script at the top:
PROG=red5
PIDFILE=/var/run/$PROG.pid
So try deleting it (as root user):
rm /var/run/red5.pid

Linux: process into a service

I am trying to make a linux executable as a service
I execute my program like this below
java -jar mytestprogram.jar
creates a process that runs continuously and serves REST requests. But I want to run it as a service where I can do
service mytestprogram start
service mytestprogram stop
service mytestprogram status
chkconfig mytestprogram on
etc. What is the most simple way of doing it?
That depends on your system manager
the most common way to do that on debian/ubuntu is to build an initscript and place it in /etc/init.d or /etc/rc/init.d and place a script named mytestprogram in that.
this is an example initscript:
#!/bin/sh
### BEGIN INIT INFO
# Provides: testone
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: Example init script
# Description: Start/stop an example script
### END INIT INFO
DESC="test script"
NAME=testone
#DAEMON=
do_start()
{
echo "starting!";
}
do_stop()
{
echo "stopping!"
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
esac
exit 0
I suggest you to look some scripts in that directory, It's simple if you know bash a little ;)
Here is a sample shell script (make sure you replace the MAT name with the name of the your application):
I create one GitHubGist with the latest version of my script and a brief explanation to help those who need it. GitHub Gist link
#!/bin/bash
### BEGIN INIT INFO
# Provides: MATH
# Required-Start: $java
# Required-Stop: $java
# Short-Description: Start and stop MATH service.
# Description: -
# Date-Creation: -
# Date-Last-Modification: -
# Author: -
### END INIT INFO
# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0
# Start the MATH
start() {
echo "Starting MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "The service is already running"
else
#Run the jar file MATH service
$JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
#sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service was successfully started"
else
echo "Failed to start service"
fi
fi
echo
}
# Stop the MATH
stop() {
echo "Stopping MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
#Kill the pid of java with the service name
kill -9 $($PGREP -f MATH)
#Sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Failed to stop service"
else
echo "Service was successfully stopped"
fi
else
echo "The service is already stopped"
fi
echo
}
# Verify the status of MATH
status() {
echo "Checking status of MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service is running"
else
echo "Service is stopped"
fi
echo
}
# Main logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac
exit 0
In Ubuntu you can create the file as explained above with few lines, play around and see if it works for you.
description "My prog"
start on (net-device-up
and local-filesystems
and runlevel [2345])
stop on runlevel [!2345]
respawn
respawn limit 10 5
setuid root
setgid shnmon
script
/usr/local/bin/my_script -config.path /etc/myprog/yourconfig.xyz -children false >> /var/log/myprog.log 2>&1
end script
The "-config.path /etc/myprog/yourconfig.xyz" is optional incase you have a config file.
You can just edit the script part and test it and keep others default and edit the rest along the way

Resources