For the past 2 weeks I've been busy trying to figure out how to setup my minecraft server onto my freenas server.
I was able to get it up and running stably when going into the jail manually typing in my startup command:
cd /root/Minecraft_Server
java -Xmx4096M -Xms4096M -jar forge-1.12.2-14.23.4.2757-universal.jar
And then just close the shell.
I tried looking to automate this command and put it into and sh file in crontab and everything, that didn't work so i decided to upgrade to 11.2 to see if that has any solutions.
Now the main problem already is that if I try to run my command manually in the shell, and i leave the webui, it will just close the server down to unlike in the 11.1 freenas.
Does anyone have any more ideas here?
In the same location as the server I have a minecraft.sh script with this command.
If I manually run the script it works, but if I use crontab it won't start it either.
The corntab command that i've used is:
#reboot /root/Minecraft_Server/minecraft.sh
I also tried putting in the command directly but this also was useless.
I even tried the exec.poststart but when i direct it to /root/minecraft_Server/minecraft.sh it won't start either, it won't even run the jail anymore
use “screen java ...”
on relog to shell do screen -x to get on the server shell
You can configure your Java command as a service that starts whenever the jail starts. That way, the Java server doesn’t depend on the shell or webui.
Basically, create a usr/local/etc/rc.d/minecraftd file that includes the following script:
#!/bin/sh
#
# PROVIDE: minecraftd
# REQUIRE: LOGIN DAEMON NETWORKING mountcritlocal
# KEYWORD: shutdown
#
# Use the following variables to configure the minecraft server. For example, to
# configure the ON/OFF knob variable:
# sysrc minecraftd_enable="YES"
#
# minecraftd_enable="YES"
# minecraftd_user_dir="/root/minecraft"
# minecraftd_jar_path="/root/minecraft/server.jar"
# minecraftd_java_opts="-Xms512M -Xmx1024M"
. /etc/rc.subr
name=minecraftd
rcvar=`set_rcvar`
pidfile=/var/run/minecraftd.pid
load_rc_config $name
start_cmd="${name}_start"
: ${minecraftd_enable="NO"}
: ${minecraftd_user_dir="/root/minecraft"}
: ${minecraftd_jar_path="/root/minecraft/server.jar"}
: ${minecraftd_java_opts="-Xms512M -Xmx1024M"}
minecraftd_start() {
if [ -e $pidfile ]; then
echo "$name already running."
else
echo "Starting $name..."
/usr/sbin/daemon -f -p $pidfile \
/usr/local/bin/java -Duser.dir=$minecraftd_user_dir \
$minecraftd_java_opts \
-jar $minecraftd_jar_path nogui
echo "$name started."
fi
}
run_rc_command $1
Then configure the service to start on boot:
sysrc minecraftd_enable="YES"
And restart your jail.
For more information, check Installing a Minecraft server on FreeNAS.
Disclaimer: My team published that article.
Hope this helps somebody.
I have a java app on my (Ubuntu) server. If I do this, then it starts correctly:
/usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main
but I don't know how to get its PID. I don't know how to stop it with an init.d script.
I have a different app, written in Clojure, and for it I was able to write an init.d script that works great. So I tried to refashion that init.d script for my Java app, and this is what I got:
WORK_DIR="/home/jenkins/veta"
NAME="lily"
JAR="lily.jar"
USER="jenkins"
DAEMON="/usr/bin/java"
DAEMON_ARGS=" -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main"
start () {
echo "Starting lily..."
if [ ! -f $WORK_DIR/lily.pid ]; then
/sbin/start-stop-daemon --start --verbose --background --chdir $WORK_DIR --exec $DAEMON --pidfile $WORK_DIR/lily.pid --chuid "$USER" --make-pidfile -- $DAEMON_ARGS
else
echo "lily is already running..."
fi
}
stop () {
echo "Stopping lily..."
/sbin/start-stop-daemon --stop --exec $DAEMON --pidfile $WORK_DIR/lily.pid
rm $WORK_DIR/lily.pid
}
But this doesn't work. Although the PID in $WORK_DIR/lily.pid changes every time I run the script, no process with that PID ever seems to run. If I try:
ps aux | grep java
I don't see this app, nor if I try using the PID.
So is there a way I can use the first command, but somehow capture the PID, so I can store it for later?
I just want a reliable way to stop and start this app. That can be by PID or some other factor. I'm open to suggestions.
UPDATE:
Maybe my question is unclear? Something like jps or ps will give me too many answers. If I do something like "ps aux | grep java" I'll see that there are 5 different java apps running on the server. The start-stop-daemon won't know which PID belongs to this particular app, nor can I figure out what I should feed into my init.d script.
If your system has jdk installed there is an utility called jps which resides in jdk/bin. It will display the list of running java process. Make use of it.
If jdk is not installed in your machine then you have to grep the java process from ps -eaf command.
If you want the pid from the command line, this might work:
myCommand & echo $!
Which I copied from the accepted response to a very similar topic in ServerFault: https://serverfault.com/a/205504
At the moment I have to go to /usr/java/apache-solr-1.4.0/example and then do:
java -jar start.jar
How do I get this to start automatically on boot?
I'm on a shared Linux server.
As you're on a shared Linux box, you'll have to ask the system administrator to do the following, probably.
Create a startup script in /etc/init.d/solr.
Copy this code, my Solr startup script, into that file:
#!/bin/sh
# Prerequisites:
# 1. Solr needs to be installed at /usr/local/solr/example
# 2. daemon needs to be installed
# 3. Script needs to be executed by root
# This script will launch Solr in a mode that will automatically respawn if it
# crashes. Output will be sent to /var/log/solr/solr.log. A PID file will be
# created in the standard location.
start () {
echo -n "Starting solr..."
# Start daemon
daemon --chdir='/usr/local/solr/example' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
stop () {
# Stop daemon
echo -n "Stopping solr..."
daemon --stop --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "Done."
else
echo "Failed. See error code for more information."
fi
return $RETVAL
}
restart () {
daemon --restart --name=solr --verbose
}
status () {
# Report on the status of the daemon
daemon --running --verbose --name=solr
return $?
}
case "$1" in
start)
start
;;
status)
status
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: solr {start|status|stop|restart}"
exit 3
;;
esac
exit $RETVAL
Then run:
chkconfig --add solr
Or (on Ubuntu):
update-rc.d solr defaults
... or, if your Linux distribution doesn't have chkconfig (or update-rc.d), link /etc/init.d/solr to /etc/rc3.d/S99solr and /etc/rc5.d/S99solr and /etc/rc3.d/K01solr and /etc/rc5.d/K01solr:
% ln -s /etc/init.d/solr /etc/rc3.d/S99solr
% ln -s /etc/init.d/solr /etc/rc5.d/S99solr
% ln -s /etc/init.d/solr /etc/rc3.d/K01solr
% ln -s /etc/init.d/solr /etc/rc5.d/K01solr
Now on reboot Solr will startup in run levels 3 and 5 (console with network & full GUI).
To start solr manually run:
% /etc/init.d/solr start
If you have root access to your machine, there are a number of ways to do this based on your system's initialization flow (init scripts, systemd, etc.)
But if you don't have root, cron has a clean and consistent way to execute programs upon reboot.
First, find out where java is located on your machine. The command below will tell you where it is:
$ which java
Then, stick the following code into a shell script, replacing the java path below (/usr/bin) with the path you got from the above command.
#!/bin/bash
cd /usr/java/apache-solr-1.4.0/example
/usr/bin/java -jar start.jar
You can save this script in some location (e.g., $HOME) as start.sh. Give it world execute permission (to simplify) by running the following command:
$ chmod og+x start.sh
Now, test the script and ensure that it works correctly from the command line.
$ ./start.sh
If all works well, you need to add it to one of your machine's startup scripts. The simplest way to do this is to add the following line to the end of /etc/rc.local.
# ... snip contents of rc.local ...
# Start Solr upon boot.
/home/somedir/start.sh
Alternatively, if you don't have permission to edit rc.local, then you can add it to your user crontab as so. First type the following on the commandline:
$ crontab -e
This will bring up an editor. Add the following line to it:
#reboot /home/somedir/start.sh
If your Linux system supports it (which it usually does), this will ensure that your script is run upon startup.
If I don't have any typos above, it should work out well for you. Let me know how it goes.
Adding the following lines to my /etc/init.d/solr file works to support Red Hat Linux (I copied them from /etc/init.d/mysql after reading comments by others here).
# Comments to support chkconfig on Red Hat Linux
# chkconfig: 2345 64 36
# Description: A very fast and reliable search engine.
There are three steps that you need to do here:
Create the script, make it executable, and put it in the right place.
Make the script start up properly on reboot.
Bonus: Set up a logrotation script so logs don't get out of control.
For number one, I've tuned supermagic's script from above. It was OK, but had a number of typos, lacked some functionality (status, restart), didn't use the daemon utility very effectively.
Here's my version of the script (make sure you have daemon installed for it to work):
#!/bin/sh
# Prerequisites:
# 1. Solr needs to be installed at /usr/local/solr/example
# 2. daemon needs to be installed
# 3. Script needs to be executed by root
# This script will launch Solr in a mode that will automatically respawn if it
# crashes. Output will be sent to /var/log/solr/solr.log. A pid file will be
# created in the standard location.
start () {
echo -n "Starting solr..."
# start daemon
daemon --chdir='/usr/local/solr/example' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
stop () {
# stop daemon
echo -n "Stopping solr..."
daemon --stop --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
restart () {
daemon --restart --name=solr --verbose
}
status () {
# report on the status of the daemon
daemon --running --verbose --name=solr
return $?
}
case "$1" in
start)
start
;;
status)
status
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: solr {start|status|stop|restart}"
exit 3
;;
esac
exit $RETVAL
Place this script at /etc/init.d/solr, make it executable, and you should be good with step one. You can now start/stop/restart/status a solr daemon with /etc/init.d/solr start|stop|restart|status
For step two, run the following on an Ubuntu machine (don't know about Redhat):
update-rc.d solr defaults
Once this is done, you're in pretty good shape, but you probably want to rotate the logs properly at some point, so here's a good config for the logs:
/var/log/solr/*.log {
weekly
rotate 12
compress
delaycompress
create 640 root root
postrotate
/etc/init.d/solr restart
endscript
}
Place that file in /etc/logrotate.d/solr, and you should be good to go, assuming logrotate is running (it usually is).
I answered this question once already, but that answer was for operating systems that used SysV and this one is for newer operating systems that are increasingly using systemd.
As in my other answer, there are three things you'll need to do here:
Create the script and put it in the right place.
Make the script start up properly on reboot.
Bonus: Make systemd logs persistent.
1. Creating the script and putting it in the right place
Here's a systemd unit file that you can use (these replace the SysV init files). Name it solr.service.
[Unit]
Description=Apache Solr
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
Environment="XMX=2G"
WorkingDirectory=/usr/local/solr/example
ExecStart=/usr/bin/java -jar -server -Xmx${XMX} start.jar
Restart=on-failure
LimitNOFILE=10000
[Install]
WantedBy=multi-user.target
Note that there's a configuration in there for Solr's memory. You'll probably want to tweak that to your own purposes. If you have a number of variables you're passing to systemd, you can do that with the EnvironmentFile directive.
More documentation for these files is here.
2. Make the script startup properly at boot
This is fairly simple, but there are rules. To make it start at boot, put the file in /etc/systemd/system/solr.service. You cannot use a symlink in this directory, do not try.
Once that's in there, you can enable the daemon to run at boot with:
sudo systemctl enable solr
And you can start, stop, status it with:
sudo systemctl {start|stop|status} solr
3. Making systemd logs persistent
By default, systemd logs are not persistent and they are lost whenever you reboot the system. If that's not what you desire, you can make them persistent by creating a directory:
sudo mkdir -p /var/log/journal/
And then restarting the systemd journaling daemon:
sudo systemctl restart systemd-journald
Once that's complete, systemd's journaling daemon will receive all the stdout and stderr that Solr creates and it will get stored in a binary format under /var/log/journal/.
The way systemd handles logging is pretty neat and is worth studying if you're not familiar with it. In the meantime, just know that to read your log entries you'll need to use a new tool called journalctl. For example, this will follow your solr logs:
journalctl -u solr -f
And there are also flags for doing date-based filtering and things like that.
3.1 Tweaking the log files
Bonus section! If you want to tweak the log files, you can read all about it in the documentation here, but the defaults are actually very safe and sane (logs are compressed by default, can't grow too large, are rate limited, and are written to disk in batches).
Follow supermagic's comments, then follow this
http://codingrecipes.com/service-x-does-not-support-chkconfig
He says,
1 – Copy your script into /etc/init.d folder
2 – cd /etc/init.d
3 – chmod +x myscript
4 – Add these lines, including #, right after #!/bin/bash or #!/bin/sh:
# chkconfig: 2345 95 20
# description: Some description
# What your script does (not sure if this is necessary though)
# processname: myscript
Then you can do
chkconfig --add myscript
init.d/solr script that's tested on Centos 6/Amazon Linux. It wraps solr's native CLI.
#!/bin/bash
# description: Starts and stops Solr production
PIDFILE=/var/run/solr.pid
SOLR_HOME=/usr/share/solr
START_COMMAND="$SOLR_HOME/bin/solr start -p 8984 -noprompt -m 1g"
NAME="Solr"
start() {
echo "Starting $NAME"
if [ -f $PIDFILE ]; then
echo -n "$PIDFILE exists. $NAME may be running."
else
str=`$START_COMMAND`
pid=`echo $str | grep -o "pid=[0-9]*" | grep -o "[0-9]*"`
if [ "$pid" == "" ];
then
echo "[FATAL ERROR] Failed to extract pid. Exiting!"
exit 1
fi
echo $pid > $PIDFILE
fi
return 0
}
case "$1" in
start)
start
;;
stop)
echo "Stopping $NAME .."
$SOLR_HOME/bin/solr stop
rm -f $PIDFILE
;;
status)
$SOLR_HOME/bin/solr status
;;
restart)
$0 stop
#sleep 2
$0 start
;;
*)
echo "Usage: $0 (start | stop | restart | status)"
exit 1
;;
esac
exit $?
Check
man 5 crontab
See if #reboot is supported on the Linux system you are using.
I'm trying to run a java program as a service.
My requirements are:
1) start a java program on machine startup
2) restart if the java program crashes
3) execute it in a special directory as a special user
sidenotes: I CANNOT assume this being the only java process running and it would be dangerous to run the service twice by accident.
So far, I've tried implementing it with start-stop-daemon. However, the application is not automatically restarted when it crashes (i.e., terminates with a non-zero exit code). I guess it has something to do, that I need to use the --background and, thus, start-stop-daemon cannot determine the exit code? Am I correct? How do I resolve this issue properly? (I would prefer a solution with system functionality only, it would be much easier without third party tools due to security restrictions)
My current script (Dummy is, as the same says, a dummy java application which sleeps forever)
#!/bin/sh
### BEGIN INIT INFO
# Provides: CI Master
# Required-Start: $all
# Required-Stop: $all
# Should-Start: $portmap
# Should-Stop: $portmap
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: CI Master
# Description: CI Master
### END INIT INFO
SERVICE_NAME="CI Master"
PIDFILE=/var/run/CI_master.pid
USER=ci
DIRECTORY=./master/
EXECUTABLE=/usr/bin/java
ARGUMENTS="Dummy"
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting $SERVICE_NAME" "$SERVICE_NAME"
start-stop-daemon --pidfile $PIDFILE --make-pidfile --background --chuid $USER --chdir /home/$USER/$DIRECTORY/ --startas $EXECUTABLE --start -- $ARGUMENTS
log_daemon_msg "$SERVICE_NAME started" "$SERVICE_NAME"
;;
stop)
log_daemon_msg "Stopping $SERVICE_NAME" "$SERVICE_NAME"
start-stop-daemon --pidfile $PIDFILE --remove-pidfile --stop
log_daemon_msg "$SERVICE_NAME stopped" "$SERVICE_NAME"
;;
restart|reload|force-reload)
$0 stop
sleep 1
$0 start
;;
status)
start-stop-daemon --pidfile $PIDFILE --status
case $! in
0)
log_daemon_msg "$SERVICE_NAME is running" "$SERVICE_NAME"
;;
1)
log_daemon_msg "$SERVICE_NAME is not running (pid file exists)" "$SERVICE_NAME"
;;
2)
log_daemon_msg "$SERVICE_NAME is not running" "$SERVICE_NAME"
;;
3)
log_daemon_msg "unable to determine status of $SERVICE_NAME" "$SERVICE_NAME"
;;
esac
;;
esac
exit 0
Thanks in advance!
I suggest DaemonTools + service/chkconfig.
DaemonTools maybe already installed on your platform, or you can try apt-get. It will restart your daemon automatically in at most 5 seconds.
You can look up linux user manual 8 for more information about service/chkconfig.
Hope this helpful.
I use Apache Commons Daemon Tools for this...
JSvc will respawn your Java service process for you...
Jsvc uses 3 processes: a launcher process, a controller process and a controlled process. The controlled process is also the main java thread, if the JVM crashes the controller will restart it in the next minute. Jsvc is a daemon process so it should be started as root and the -user parameter allows to downgrade to an unprivilegded user. When the -wait parameter is used, the launcher process waits until the controller says "I am ready", otherwise it returns after creating the controller process.
JSvc will write a PID File. So the -stop command can also stop your running service.
JSvc -user will allow you to demote your service to a less privileged user after startup.
It is also compatible with ProcRun.exe which works well on Windows.
I can recommend another solution - Java Service Wrapper http://wrapper.tanukisoftware.com/doc/english/introduction.html
I'm trying to create a service that will run in Ubuntu Linux written in Java. My executable Jar file is a big fat Jar file that has all the dependencies packaged inside with it. I get the following error when I try to run it using JSVC.
Java VM created successfully
Class org/apache/commons/daemon/support/DaemonLoader found
Cannot register native methods
java_init failed
Service exit with a return value of 1
What's killing me is the "cannot register native methods" line. What does this mean and how do I fix it?
I'm writing my app in Java using the Eclipse IDE, I installed the latest version of JSVC using apt-get (1.0.10-3). My commons.daemon library version is 1.0.1. My startup script is below.
#!/bin/sh
# Setup variables
EXEC=jsvc
JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-amd64
CLASS_PATH="/usr/hadoop-1.1.1/lib/commons-daemon-1.0.1.jar":"/usr/devel/Hadoop_LCS/"
CLASS=com.foo.hadoop.lcs.Program
USER=hduser
PID=/tmp/lcs_process.pid
LOG_OUT=/tmp/lcs_log.out
LOG_ERR=/tmp/lcs_log.err
do_exec()
{
$EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -debug -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $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
I got it. I just upgraded my version of the Apache commons.daemon library and that did the trick. I got 1.0.15.