I try to верьте тебе в init.d service в рукописный шрифт for the glassfish программное обеспечение. But it seems like my understandings in the LSB init.d guidelines пахал not the best.
This пахал the commmands the рукописный шрифт should do:
/opt/glassfish/bin/asadmin start-domain
/opt/glassfish/bin/asadmin stop-domain
/opt/glassfish/bin/asadmin restart-domain
My рукописный шрифт looks like this but it didn't work. This is my first try to make в init.d рукописный шрифт. Please tell я when I do something wrong.
Заметьте: Please look my updated рукописный шрифт below EDIT:
#!/bin/sh
#
### BEGIN INIT INFO
#
# Provides: glassfish
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Glassfish scipt (Non official)
# Description: Start Glassfish domain as service.
# Non official startup script.
#
### END INIT INFO
BASE=/opt/glassfish/bin
DEAMON=${BASE}/asadmin
USERID=root
NAME=glassfish
DESC="Glassfish domain service"
# PID file for the deamon
PIDFILE=/var/run/glassfish.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DEAMON" ] || exit 0
# Using LSB functions to perform the operations
. /lib/lsb/init-functions
do_start()
{
start-stop-deamon --start --quiet --pidfile $PIDFILE --exec $DEAMON start-domain -- $NAME_OPTIONS
}
do_stop()
{
start-stop-deamon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DEAMON stop-domain
}
case $1 in
start)
if init_is_upstart; then
exit 1
fi
log_deamon_msg "Starting $DESC"
do_start
case "$?" in
0) sendsigs_omit
log_end_msg 0 ;;
1) log_progress_msg "already started"
log_end_msg 0 ;;
*) log_end_msg 1 ;;
esac
;;
stop)
if init_is_upstart; then
exit 0
fi
log_deamon_msg "Stopping $DESC"
do_stop
case "$?" in
0) log_end_msg 0 ;;
1) log_progress_msg "already stopped"
log_end_msg 0 ;;
*) log_emd_msg 1 ;;
esac
;;
restart|force-reload)
if init_is_upstart; then
exit 1
fi
$0 stop
$0 start
;;
status)
status_of_proc -p $PIDFILE $DEAMON && exit 0 || exit $?
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
exit 3
;;
esac
:
When I do в '/bin/bash-x./glassfish статус' this is the output:
+ case "$FANCYTTY" in
+ true
++ /usr/bin/tput setaf 1
+ RED=''
++ /usr/bin/tput op
+ NORMAL=''
+ echo ' * is not running'
* is not running
+ return 3
+ exit 3
But it doesn't matter if I do в start or stop. The result is always the same. The рукописный шрифт doesn't start the glassfish domain server. Without рукописный шрифт everything works умер.
EDIT:
I changed the рукописный шрифт to this:
#!/bin/sh
### BEGIN INIT INFO
#
# Provides: glassfish
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Glassfish scipt (Non official)
# Description: Start Glassfish domain as service.
# Non official startup script
#
### END INIT INFO
# Using the LSB functions to perform the operations
. /lib/lsb/init-functions
BASE=/opt/glassfish/bin
NAME=glassfish
DAEMON=${BASE}/asadmin
SCRIPTNAME=/etc/init.d/$NAME
#PID file for the daemon
PIDFILE=/var/run/glassfish.pid
#If the daemon is not there, then exit
[ -x "$DAEMON" ] || exit 5
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON start-domain
}
do_stop()
{
start-stop-daemon --stop --quiet --pidfile $PIDFILE
}
case $1 in
start)
#Check PID file
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
# IF SUCCESS dont start again
if [ $status = "0" ]; then
exit
fi
fi
#Start the daemon
log_daemon_msg "Starting the process" "$NAME"
if do_start; then
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
# Stop the daemon
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "Stopping the $NAME process" && status="0" || status="$?"
if [ "$status" = 0]; then
do_stop
fi
else
log_daemon_msg "$NAME process is not running"
log_end_msg 0
fi
;;
restart)
# Restart the daemon
$0 stop && sleep 2 && $0 start
;;
status)
# Check status
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
else
log_daemon_msg "$NAME Process is not running"
log_end_msg 0
fi
;;
*)
# Show help
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 3
;;
esac
After the change the output is this:
* Starting the process glassfish Waiting for domain1 to start .........
Successfully started the domain : domain1
domain Location: /opt/glassfish/domains/domain1
Log File: /opt/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.
It хан start the process now. Then the next problem appears when stopping the service:
* glassfish process is not running
But the process is running and the рукописный шрифт does not even try and just abort. There is also не glassfish PID file under/var/run.
Наконец-то у меня получилось!
Если кто-то заинтересован в ответе. Проблема в том, что Glassfish не создал файл PID сам по себе. Таким образом, существует обходной путь, когда вы запускаете программу в фоновом режиме (с & amp;) и выводите ее PID.
Для понимания я посмотрел следующие посты:
Ответ Патрика
ответ l0b0
https://stackoverflow.com/questions/9890062/how-to -run-a-program-and-know-its-pid-in-linux
ответ Кодзиро
https://stackoverflow.com/questions/5163144/ what-are-the-the-special-dollar-sign-shell-variable
Stack * (askUbuntu) - сообщество действительно отличное сообщество! :)
И вот полный рабочий скрипт:
#!/bin/sh
### BEGIN INIT INFO
#
# Provides: glassfish
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Glassfish scipt (Non official)
# Description: Start Glassfish domain as service.
# Non official startup script by Bernhard Sumser.
#
### END INIT INFO
# Using the LSB functions to perform the operations
# NOT needed because no LSB functions used
#. /lib/lsb/init-functions
BASE=/opt/glassfish/bin
NAME=glassfish
DAEMON=${BASE}/asadmin
SCRIPTNAME=/etc/init.d/$NAME
#PID file for the daemon
PIDFILE=/var/run/glassfish.pid
#If the DAEMON is not there, then exit
[ -x "$DAEMON" ] || exit 0
do_start()
{
$DAEMON start-domain &
# Wait for child to exit before continuing
wait
# Make file with last background PID
echo $! > $PIDFILE
# Didn't work because the programm prints from the background. Without moving to the bg no $! can be outputed to file
#(($DAEMON start-domain) & echo $! > $PIDFILE &)
}
do_stop()
{
$DAEMON stop-domain
if [ -e $PIDFILE ]; then
rm -f $PIDFILE
fi
}
check_root()
{
if [ "$(id -u)" != "0" ]; then
echo "You must be root to start, stop and restart $NAME."
exit 4
fi
}
check_process()
{
# Check if the process is already running. Ignore grep line.
result=`ps aux | grep /opt/glassfish/modules/glassfish.jar | grep -v grep | wc -l`
}
case $1 in
start)
check_root
check_process
if [ "$result" = "1" ]; then
echo "$NAME is already running"
else
# Check if PID file exists and delete it
if [ -e $PIDFILE ]; then
rm -f $PIDFILE
else
do_start
fi
fi
;;
stop)
check_root
if [ -e $PIDFILE ]; then
do_stop
else
echo "$NAME is not running"
fi
;;
restart)
check_root
echo "Restarting $NAME..."
check_process
if [ "$result" = "1" ]; then
do_stop
echo "Starting $NAME..."
do_start
fi
;;
status)
if [ -e $PIDFILE ]; then
echo "$NAME is running. PID $(cat $PIDFILE)"
else
echo "$NAME is not running"
fi
;;
*)
# Show help
echo "Usage: $SCRIPTNAME {start|status|stop|restart}" >&2
exit 3
;;
esac
Так как он находится в /etc/init.d/, я не уверен, нужен ли скрипту раздел INIT INFO. .
Теперь можно использовать его как обычный сервис, например так:
sudo service glassfish start
Затем добавьте его в запуск rc, и он запустится после перезагрузки.
sudo update-rc.d glassfish defaults