How to write init script - java

Hi I am using 64bit Fedora 10 linux. I have created a sample java application. Now I want to write init script for that application. so that my application should start on bootup.
How to write init script to start on bootup.
Thanks
Sunil Kumar Sahoo

There's quite a good guide here:
http://www.novell.com/coolsolutions/feature/15380.html
I'd suggest taking a look at the tomcat startup.sh and shutdown.sh scripts, and then modifying the following init.d script:
#!/bin/bash
#
# tomcat
#
# chkconfig:
# description: Start up the Tomcat servlet engine.
# Source function library.
. /etc/init.d/functions
RETVAL=$?
CATALINA_HOME="/usr/apps/apache/tomcat/jakarta-tomcat-4.0.4"
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
/bin/su tomcat $CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
/bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
The above script is missing much of the stuff to make it fully Linux Standard Base compliant. You may want to copy an existing init.d script from your distro. A slightly better script can be found here: http://blog.valotas.com/2011/05/tomcat-initd-script.html

I usually just take one of the smaller init scripts from /etc/init.d and modify it.
Edit
The easiest thing to do is just add your program to the /etc/rc.local file. It will be the last start script executed. You won't have to mess around with the "start" and "stop" stuff.
However, if you're interested in being able to start and stop your program at will, you'll need to write a script.
Some of the other answers here will get you started.

Many distributions come with a skeleton script you can use as a template for your own init script, in /etc/init.d/skeleton or /etc/init.d/skel.

Some of the best java applications that I have seen tend to use the tanuki wrapper for this.
It standardises startup scripts across different OS's, i.e. can be used to configure a *nix daemon or a windows service.
It provides a standard command line interface for stopping, starting, restarting and checking the status - running or not.
I appreciate seeing it used more and more, as I don't have to learn it again, learn it once and reuse it again and again.
By using this service library, your application can benefit from future enhancements.

Related

Why would my spring boot application will shut down automatically?

update:
I have followed spring boot deployment for Installation as an init.d Service (System V). I start my application successfully.But, after one day.My application closed again......Is there any ways to help me ?
I hava one spring boot application which names my.jar. I put it into my ubuntu server(20.04) and use the command of nohup java -jar my.jar &.In the first few hours, my application is in good condition.But,after one or two days,it will shut down automatically.
I have seen the log of my application which don't recorded any error and saved the last correct log before exiting
The current situation is my application is very simple and have only a small number of visits.
According to my guess,it is seems that ubuntu kill my process for inactivity?
My scripts are as follow,
stop.sh:
#!/bin/bash
PID=$(ps -ef | grep centre-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
run.sh:
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh
start.sh:
#!/bin/bash
nohup java -jar centre-0.0.1-SNAPSHOT.jar &
I need my application will run all the time.
Is there any ways to resolve the issue?
nohup is more suited for running processes that are expected to end after a while. For example running a lengthy batch script.
As of why it is killed, there may be a number of reasons: memory leak, server security policy .... The server probably decided your program wasn't behaving correctly. Logs of the server, like dmesg or /var/log/ contents may have some clues about it.
What you said about your app receiving visits feels like it is more a service rather than a script.
You may want to
daemonize your program
. this will make your program tied to the server availability.
Here is an explanation of the differences between nohup and daemons.
Also check this link at baeldung for help on setting up the daemon
After I check the memory usage, so I realized that my spring boot have used a lot of memory abnormally.
I solved it in the following way:
I updated my start.sh with the command of "nohup java -Xms100m -Xmx300m -jar ./target/centre-0.0.1-SNAPSHOT.jar &"
But, I did not figure out why did my spring boot application use a lot of memory?
Is there anyone can explain this phenomenon?

Bash script to start java if not running, with other java programs running

i usually use this bash script to check if my java application is running and start it again if not. I use crontab to check it
#!/bin/bash
if [ "$(pidof java)" ]
then
# process was found
echo "application running"
else
# process not found
cd /home/assist/emanager
setsid java -jar emanager-1.0.0.jar </dev/zero &>/dev/null &
fi
Now the problem is that there are other java applications running on the server, so the script does not start my app because the if condition is true. Is there a way to check if a specific java application is running?
Thanks
Your question is very similar to this one:
Start a Java application using Bat file if not already started
The main difference is you are asking for bash while the other one is in Windows. Hence replace jcmd with jps, the rest is still applicable.

How to start the weblogic server using global variables

My startup script for weblogic 10.3.3 is startWeblogic.sh which resides under mydomain. i do not want to disclose startup script path to my peers. so i would like define the global variable in sunOS and use userdefined commands like
server start -> ./startWeblogic.sh this should start the server.
server stop ->/stopWeblogic.sh this should stop the server.
from anywhere. so that people do not know where actually the start and stop scripts reside
Please help me out.
You can use rc scripts but for solaris better use SMF
First create a XML containing the scripts location and its dependencies (basically we can take the example template which comes with Solaris 10 and edit a few lines since all NM requires is a mounted filesystem, network and multiuser run-level).
Create file as shown below Admin.xml and place it under /var/svc/manifest/application/management/ .
#! /bin/bash
case "$1" in
start)
DOMAIN_HOME = /opt/Oracle/Middleware/user_projetcs/domains/basedomain
$DOMAIN_HOME/bin/startWebLogic.sh
;;
stop)
$WL_HOME/server/bin/stopWebLogic.sh
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit_status
Lets place this script under
/lib/svc/method/ so it can be imported in our service database.
Now, lets import it and activate:
svccfg import /var/svc/manifest/application/management/Admin
svcadm enable svc:/application/management/Admin:default
Also you can check below link
http://philtheitmonkey.blogspot.in/2011/08/starting-weblogic-managed-servers-using.html

How to restart service when it get stopped in Linux [duplicate]

This question already has answers here:
How to Daemonize a Java Program?
(11 answers)
Closed 7 years ago.
I have built a little daemon in Java and I would like to run it as a service under Unix (e.g. Debian 5). I have read that there is a possibility of using a Java wrapper, but isn't there any other option which is easier to implement? Can't I just use a Unix command such as xxx java -jar program.jar?
Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:
$nohup java -jar program.jar &
You need to create an appropriate script in /etc/init.d and link it to /etc/rcX.d directories. The script should support at least start, stop, and status parameters. During start it should run java command with appropriate arguments, probably via nohup java <arguments> &. Then you should save PID of your newly-started process to file /var/run/yourservice.pid. stop command should read this PID file and kill this service.
The details vary from distribution to distribution, most distributions provide some macros to make whole job easier. It's best to look at examples of other services in /etc/init.d for your distribution.
Additionally:
If your service isn't accessed from other computers from the network, but it opens some port, make it unavailable with firewall.
If your service processes some 'delicate' data, it's good to add another user and invoke an appropriate sudo command in your /etc/init.d file.
You can start it as:
java -jar program.jar
Unix daemons are normally started by init or started by a script in /etc/init.d or /etc/rc.d, and started at specific runlevels - normally by soft links in /etc/rcX.d. (where X is the intended "runlevel" which is normally 3.
I think debian are moving to using "upstart", a init-replacement. It uses config files in /etc/init to define jobs, and they are quite easy to write. Check that out.
Daemons traditionally closes stdin, sdtout and stderr, and does a "double fork" when starting, in order to detach from the session and also to signal that they are ready to handle whatever they should handle. This is not really necessary, as long as the daemon is not started from the terminal.
If you want a simple shell wrapper to start you program; you just need to write a small shell script:
#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar
... and make it executable (chmod 755 )
This article contains a few useful tricks for running a Java application as a daemon:
http://barelyenough.org/blog/2005/03/java-daemon/
Alternatively, you can have a look at the Apache Commons Daemon project, although this requires native code (Unix and Win32 supported):
http://commons.apache.org/daemon/
You can use a cron job to schedule your program. You can also check out this article for details on how to run scripts on startup. You can write a script that runs your java program and run it on startup as mentioned in the article.

Background process in linux

I have developed a Java socket server connection which is working fine.
When started from a terminal, it starts from listening from client. But when I close the terminal it stops listening.
I need to continue even though the terminal closed by user from where jar file was started.
How can I run Java server socket application in Linux as background process?
There are several ways you can achieve such a thing:
nohup java -server myApplication.jar > /log.txt - this is pretty straight forward. It will just put the application in the background. This will work but it's just not a very good way to do so.
Use a shell wrapper and the above OR daemon app. This approach is used by many open source projects and it's quite good for most of the scenarios. Additionally it can be included in init.d and required run level with regular start, stop and status commands. I can provide an example if needed.
Build your own daemon server using either Java Service Wrapper or Apache Jakarta Commons Daemon. Again - both are extremely popular, well tested and reliable. And available for both Linux and Windows! The one from Apache Commons is used by Tomcat server! Additionally there is Akuma.
Personally I would go with solution 2 or 3 if you need to use this server in the future and/or distribute it to clients, end users, etc. nohup is good if you need to run something and have no time to develop more complex solution for the problem.
Ad 2:
The best scripts, used by many projects, can be found here.
For Debian/Ubuntu one can use a very simple script based on start-stop-daemon. If in doubt there is /etc/init.d/skeleton one can modify.
#!/bin/sh
DESC="Description"
NAME=YOUR_NAME
PIDFILE=/var/run/$NAME.pid
RUN_AS=USER_TO_RUN
COMMAND=/usr/bin/java -- -jar YOUR_JAR
d_start() {
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case $1 in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
There's one crucial thing you need to do after adding a & at the end of the command. The process is still linked to the terminal. You need to run disown after running the java command.
java -jar yourApp.jar > log.txt &
disown
Now, you can close the terminal.
The key phrase you need here is "daemonizing a process". Ever wondered why system server processes often end in 'd' on Linux / Unix? The 'd' stands for "daemon", for historical reasons.
So, the process of detaching and becoming a true server process is called "daemonization".
It's completely general, and not limited to just Java processes.
There are several tasks that you need to do in order to become a truly independent daemon process. They're listed on the Wikipedia page.
The two main things you need to worry about are:
Detach from parent process
Detach from the tty that created the process
If you google the phrase "daemonizing a process", you'll find a bunch of ways to accomplish this, and some more detail as to why it's necessary.
Most people would just use a little shell script to start up the java process, and then finish the java command with an '&' to start up in background mode. Then, when the startup script process exits, the java process is still running and will be detached from the now-dead script process.
try,
java -jar yourApp.jar &
& will start new process thread,I have not tested this, but if still it not works then twite it in script file and start i with &
Did you try putting & at the end of the command line?
For example:
java -jar mySocketApp.jar &
You can also use bg and fg commands to send a process to background and foreground. You can pause the running process by CTRL+Z.
Check it out this article: http://lowfatlinux.com/linux-processes.html
Step 1.
To create new screen
screen -RD screenname
Step 2.
To enter into screen terminal
press Enter
Step 3.
Run your command or script (to run in the background) in the newly opened terminal
Step 4.
To come out of screen terminal
ctrl + A + D
Step 5.
To list screen terminals
screen -ls
that will print something like below
There is a screen on:
994.screenname (12/10/2018 09:24:31 AM) (Detached)
1 Socket in /run/screen/S-contact.
Step 6.
To login to the background process
screen -rd 994.screenname
for quite terminal and this process still working background. for me, the simple and fast way to run the process in the background is using the &! at end of the command:
if this app is built for X server: (eg: Firefox,Zathura,Gimp...)
$ java -jar yourApp.jar &!
if this app is cli (work on the terminal)
# st is my terminal like kitty alacritty
$ st -e bash -c "lookatme --style one-dark --one $1" &!

Categories