I have a sample file with this content:
#!/bin/bash
# Setting environment
CLASSPATH="."
CLASSPATH="$CLASSPATH:props"
CLASSPATH="$CLASSPATH:cfg"
CLASSPATH="$CLASSPATH:./bin/*"
CLASSPATH="$CLASSPATH:./wslib/*"
CLASSPATH="$CLASSPATH:./oalib/*"
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8"
# RAM usage settings
JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx1024m"
# Settings of GC
JAVA_OPTS="$JAVA_OPTS -XX:ParallelGCThreads=2"
# Print of memory usage
JAVA_OPTS="$JAVA_OPTS -Xloggc:./log/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution"
# setting config
JAVA_OPTS="$JAVA_OPTS se.highex.ebe.adaptor.Adapter -config cfg/config_gw_ws.xml"
echo $CLASSPATH
echo $JAVA_OPTS
export CLASSPATH
export JAVA_OPTS
java $JAVA_OPTS
file is locating in /app/somepath/stpa.sh
how I can create an init.d file for start/stop/restart it?
I found this tutorial, but after service stpa start command there is no any messages in terminal and I think, that my script is not working. But can't find the reason.
P.S. Sorry for my bad English!
EDIT:
this is my stpa file in /etc/init.d
#!/bin/sh
#
# chkconfig: 2345 90 60
name="STPA_WS"
command="/app/STPAdapterPG/stpa_ws.sh"
command_args=""
daemon="/usr/local/bin/daemon"
[ -x "$daemon" ] || exit 0
[ -x "$command" ] || exit 0
daemon_start_args=""
pidfiles="/var/run"
user=""
chroot=""
chdir=""
umask=""
stdout="daemon.info"
stderr="daemon.err"
case "$1" in
start)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo "$name is already running."
else
echo -n "Starting $name..."
"$daemon" --respawn $daemon_start_args \
--name "$name" --pidfiles "$pidfiles" \
${user:+--user $user} ${chroot:+--chroot $chroot} \
${chdir:+--chdir $chdir} ${umask:+--umask $umask} \
${stdout:+--stdout $stdout} ${stderr:+--stderr $stderr} \
-- \
"$command" $command_args
echo done.
fi
;;
stop)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo -n "Stopping $name..."
"$daemon" --stop --name "$name" --pidfiles "$pidfiles"
echo done.
else
echo "$name is not running."
fi
;;
restart|reload)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo -n "Restarting $name..."
"$daemon" --restart --name "$name" --pidfiles "$pidfiles"
echo done.
else
echo "$name is not running."
exit 1
fi
;;
status)
"$daemon" --running --name "$name" --pidfiles "$pidfiles" --verbose
;;
*)
echo "usage: $0 <start|stop|restart|reload|status>" >&2
exit 1
esac
exit 0
I think you did not complete the init.d script format, you need to add the content just like the tutorial you mentioned:
case "$1" in
start)
#here do something you need when start
;;
stop)
#here do something you need when stop
;;
restart|reload)
;;
status)
;;
*)
echo "usage: $0 <start|stop|restart|reload|status>" >&2
exit 1
esac
Related
I am trying to log time_firstbyte and handling in a file via varnishncsa.
My /etc/init.d/varnishncsa looks like below:-
. /lib/lsb/init-functions
NAME=varnishncsa
DESC="HTTP accelerator log deamon"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/$NAME
PIDFILE=/run/$NAME/$NAME.pid
LOGFILE=/var/log/varnish/varnishncsa.log
USER=root
LOG_FORMAT="%t %r %s %b %{Varnish:time_firstbyte}x %{Varnish:handling}x"
#VARNISHNCSA_ENABLED=1
DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE} -F '${LOG_FORMAT}'"
# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
. /etc/default/$NAME
fi
# If unset, or set to "0" or "no", exit
if [ -z "${VARNISHNCSA_ENABLED}" ] || \
[ "${VARNISHNCSA_ENABLED}" = "0" ] || \
[ "${VARNISHNCSA_ENABLED}" = "no" ]; then
exit 0;
fi
test -x $DAEMON || exit 0
start_varnishncsa() {
output=$(/bin/tempfile -s.varnish)
log_daemon_msg "Starting $DESC" "$NAME"
create_pid_directory
if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
--chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \
> ${output} 2>&1; then
log_end_msg 0
else
log_end_msg 1
cat $output
exit 1
fi
rm $output
}
stop_varnishncsa(){
log_daemon_msg "Stopping $DESC" "$NAME"
if start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--retry 10 --exec $DAEMON; then
log_end_msg 0
else
log_end_msg 1
fi
}
reload_varnishncsa(){
log_daemon_msg "Reloading $DESC" "$NAME"
if kill -HUP $(cat $PIDFILE) >/dev/null 2>&1; then
log_end_msg 0
else
log_end_msg 1
exit 1
fi
}
status_varnishncsa(){
status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}"
exit $?
}
create_pid_directory() {
install -o $USER -g $USER -d $(dirname $PIDFILE)
}
case "$1" in
start)
start_varnishncsa
;;
stop)
stop_varnishncsa
;;
reload)
reload_varnishncsa
;;
status)
status_varnishncsa
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|reload}"
exit 1
;;
esac
My /etc/default/varnishncsa looks like below:-
VARNISHNCSA_ENABLED=1
But while trying to do restart varnishncsa it is failing.
I am on ubuntu 12.04 with varnish 4.1.
As per this link updating my /etc/init.d/varnishncsa as below solved the issue:-
LOG_FORMAT="%h|%u|%{%Y-%m-%d}t|%{%H:%M:%S}t|%{%z}t|%m|%{Host}i|%U|%q|%s|%b|%{Referer}i|%{User-agent}i|%{Varnish:time_firstbyte}x|%T|%D|%{Varnish:handling}x|%{X-FE-Varnish-Obj-TTL}o|%{X-FE-Varnish-Backend}o|%{X-FE-Varnish-Obj-Stat}o"
DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE} -F '${LOG_FORMAT}'"
The %T format specifier can be tricky if you are also using ntp. Because the response time can be very small - you may end up with negative values in your log file if the clock skew changes in the middle of the request. If you are post-processing your varnishncsa logs you may end up with results you were not expecting.
I have the following code for a service that I'm trying to have automatically start on boot.
#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
prog='foo'
exec="/usr/sbin/$prog"
pidfile="/var/run/$prog.pid"
lock_file="/var/lock/subsys/$prog"
logfile="/var/log/$prog"
if [ -f /etc/default/foo ]; then
. /etc/default/foo
fi
if [ -z $QUEUE_TYPE ]; then
echo 'ENV variable QUEUE_TYPE has not been set, please set it in /etc/default/foo'
exit 1
fi
get_pid() {
cat "$pidfile"
}
is_running() {
[ -f "$pidfile" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
echo -n "Starting Consul daemon: "
#
daemon --pidfile $pidfile --check foo --user my-user "my app stuff here"
echo
;;
stop)
echo -n 'Stopping Consul daemon: '
killproc foo
echo
;;
status)
status $pidfile
RETVAL=$?
#status -p $pidfile -l $prog
#[ $RETVAL -eq 0 ] && RETVAL=$?
#RETVAL=$?
#if is_running; then
# echo 'Running'
#else
# echo 'Not Running'
#fi
#status foo
#RETVAL=$?
;;
restart)
$0 stop
$0 start
RETVAL=$?
;;
*)
echo 'Usage: foo {start|stop|status|restart}'
exit 1
esac
exit $RETVAL
When I run sudo service foo status it says that it hasn't been started which is correct. After running sudo service foo start and then running the status command, it tells me that the service hasn't been started. I'm not sure what is causing this to happen. I looked at the configurations for other init.d scripts to see how they were handling this and tried to follow their lead. Is there something obvious here that I'm doing wrong or something else that I may be unaware of that's causing this problem?
I try to Nginx with /etc/init.d/nginx start. It exits with value 0 and otherwise does absolutely nothing because /usr/sbin/nginx doesn't exist.
Here is /etc/init.d/nginx:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
# We made the awk expression more precise:
# https://code.google.com/p/phusion-passenger/issues/detail?id=1060
PID=$(awk -F'[ \t;]+' '/[^#]\<pid/ {print $2}' /etc/nginx/nginx.conf)
if [ -z "$PID" ]
then
PID=/var/run/nginx.pid
fi
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
test_nginx_config() {
$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
retvar=$?
if [ $retvar -ne 0 ]
then
exit $retvar
fi
}
start() {
start-stop-daemon --start --quiet --pidfile $PID \
--retry 5 --exec $DAEMON --oknodo -- $DAEMON_OPTS
}
stop() {
start-stop-daemon --stop --quiet --pidfile $PID \
--retry 5 --oknodo --exec $DAEMON
}
case "$1" in
start)
test_nginx_config
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg $?
;;
restart|force-reload)
test_nginx_config
log_daemon_msg "Restarting $DESC" "$NAME"
stop
sleep 1
start
log_end_msg $?
;;
reload)
test_nginx_config
log_daemon_msg "Reloading $DESC configuration" "$NAME"
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID \
--oknodo --exec $DAEMON
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
if test_nginx_config; then
log_daemon_msg "$NAME"
else
exit $?
fi
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" nginx
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
I see that it exits with value 0 because of the test -x $DAEMON || exit 0 line. If I comment that line out then the script will stop suddenly inside of the test_nginx_config() function when it goes to run the daemon (/usr/sbin/nginx) which doesn't exist. There is no /usr/local/nginx/ directory either, for what it's worth.
I'm a total newbie at this stuff so there might be something dumb simple going on but I just need to get this started up. Any help would be greatly appreciated.
Well, if this is your start up script and nginx used to work with it then, Nginx has been removed by something or someone.
I wrote a little program that I want to start as a service on Opensuse 11.3
This is from the init.d script, it starts my processes as I want but I don't get the right PID.
What am I missing?
echo "Starting DHCPALERT"
for (( i = 1; i <= $DHCP_AL_DAEMONS; i++ ))
do
var="DHCP_AL_$i"
START_CMD="exec /sbin/startproc -p "$DHCP_AL_PID_DIR"p_"$i".pid -l "$DHCP_AL_LOG_DIR"log_"$i".log "$DHCPALERT_BIN" "${!var}" &"
eval $START_CMD
echo "PID: "$!
echo "Command: "$START_CMD
done
results in
PID: 47347
Command: (/sbin/startproc -p /var/log/sthserver/dhcpalert/p_2.pid -l /var/log/sthserver/dhcpalert/log_2.log /usr/sbin/dhcpalert -i eth1 -c ./test.sh -a 00:15:5D:0A:16:07 -v )&
but pidof returns some othe pid.
If I try to execute it directly:
exec /sbin/startproc -p "$DHCP_AL_PID_DIR"p_"$i".pid -l "$DHCP_AL_LOG_DIR"log_"$i".log "$DHCPALERT_BIN" "${!var}" &
Then I get errors:
startproc: exit status of parent of /usr/sbin/dhcpalert: 1
I suppose because I don't escape the variables the right way?
This is the whole script:
#!/bin/sh
# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
DHCPALERT_BIN=/usr/sbin/dhcpalert
#-x FILE exists and is executable
test -x $DHCPALERT_BIN || { echo "$DHCPALERT_BIN not installed";
if [ "$1" = "stop" ]; then exit 0;
else exit 5; fi; }
# Check for existence of needed config file and read it
DHCPALERT_CONFIG=/etc/sysconfig/dhcpalert
#-r FILE exists and is readable
test -r $DHCPALERT_CONFIG || { echo "$DHCPALERT_CONFIG not existing";
if [ "$1" = "stop" ]; then exit 0;
else exit 6; fi; }
# Read config to system VARs for this shell session only same as "source FILE"
. $DHCPALERT_CONFIG
#check for exitstence of the log dir
if [ -d "$DHCP_AL_LOG_DIR" ]; then
echo "exists 1"
echo "exists 2"
echo "exists 3"
if [ "$1" = "start" ]; then
echo "Deleting all old log files from: "
echo "Dir:... "$DHCP_AL_LOG_DIR
rm -R $DHCP_AL_LOG_DIR
mkdir $DHCP_AL_LOG_DIR
fi
else
echo "does not exist 1"
echo "does not exist 2"
echo "does not exist 3"
echo "Directory for Logfiles does not exist."
echo "Dir:... "$DHCP_AL_LOG_DIR
echo "Createing dir..."
mkdir $DHCP_AL_LOG_DIR
fi
. /etc/rc.status
# Reset status of this service
rc_reset
case "$1" in
start)
echo "Starting DHCPALERT"
for (( i = 1; i <= $DHCP_AL_DAEMONS; i++ ))
do
var="DHCP_AL_$i"
exec /sbin/startproc -p "$DHCP_AL_PID_DIR"p_"$i".pid -l "$DHCP_AL_LOG_DIR"log_"$i".log "$DHCPALERT_BIN" "${!var}" &
# START_CMD="exec /sbin/startproc -p "$DHCP_AL_PID_DIR"p_"$i".pid -l "$DHCP_AL_LOG_DIR"log_"$i".log "$DHCPALERT_BIN" "${!var}" &"
# eval $START_CMD
echo "PID: "$!
# echo "Command: "$START_CMD
done
rc_status -v
;;
stop)
echo -n "Shutting down DHCPALERT "
/sbin/killproc -TERM $DHCPALERT_BIN
rc_status -v
;;
try-restart|condrestart)
if test "$1" = "condrestart"; then
echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
fi
$0 status
if test $? = 0; then
$0 restart
else
rc_reset # Not running is not a failure.
fi
rc_status
;;
restart)
$0 stop
$0 start
rc_status
;;
force-reload)
echo -n "Reload service DHCPALERT "
/sbin/killproc -HUP $DHCPALERT_BIN
rc_status -v
;;
reload)
echo -n "Reload service DHCPALERT "
/sbin/killproc -HUP $DHCPALERT_BIN
rc_status -v
;;
status)
echo -n "Checking for service DHCPALERT "
/sbin/checkproc $DHCPALERT_BIN
rc_status -v
;;
probe)
test /etc/DHCPALERT/DHCPALERT.conf -nt /var/run/DHCPALERT.pid && echo reload
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
exit 1
;;
esac
rc_exit
The configfile:
## Specifiy where to store the Pid files
DHCP_AL_PID_DIR="/var/log/sthserver/dhcpalert/"
##
## Specifiy where to store the Log file
DHCP_AL_LOG_DIR="/var/log/sthserver/dhcpalert/"
##
## is needed to determine how many vars should be read and started!
DHCP_AL_DAEMONS="2"
##
## Then DHCP_AL_<number> to specify the command that one instance of
## dhcpalert should be started
DHCP_AL_1="-i eth0 -c ./test.sh -a 00:15:5D:0A:16:06 -v"
DHCP_AL_2="-i eth1 -c ./test.sh -a 00:15:5D:0A:16:07 -v"
Add exec to it to prevent forking:
START_CMD="(exec /sbin/startproc -p "$DHCP_AL_PID_DIR"p_"$i".pid -l "$DHCP_AL_LOG_DIR"log_"$i".log "$DHCPALERT_BIN" "${!var}") &"
Update. Please try this script:
#!/bin/bash
# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
DHCPALERT_BIN=/usr/sbin/dhcpalert
#-x FILE exists and is executable
[[ -x $DHCPALERT_BIN ]] || {
echo "$DHCPALERT_BIN not installed"
if [ "$1" = "stop" ]; then
exit 0
else
exit 5
fi
}
# Check for existence of needed config file and read it
DHCPALERT_CONFIG=/etc/sysconfig/dhcpalert
#-r FILE exists and is readable
[[ -r $DHCPALERT_CONFIG ]] || {
echo "$DHCPALERT_CONFIG not existing"
if [[ $1 == stop ]]; then
exit 0
else
exit 6
fi
}
# Read config to system VARs for this shell session only same as "source FILE"
. "$DHCPALERT_CONFIG"
#check for exitstence of the log dir
CREATE_DIR=false
if [[ -d $DHCP_AL_LOG_DIR ]]; then
echo "exists 1"
echo "exists 2"
echo "exists 3"
if [[ $1 == start ]]; then
echo "Deleting all old log files from: "
echo "Dir:... $DHCP_AL_LOG_DIR"
rm -R "$DHCP_AL_LOG_DIR"
CREATE_DIR=true
fi
else
echo "does not exist 1"
echo "does not exist 2"
echo "does not exist 3"
echo "Directory for Logfiles does not exist."
CREATE_DIR=true
fi
if [[ $CREATE_DIR == true ]]; then
echo "Dir:... $DHCP_AL_LOG_DIR"
echo "Createing dir..."
mkdir "$DHCP_AL_LOG_DIR" || {
echo "Failed to create directory $DHCP_AL_LOG_DIR"
exit 1
}
fi
. /etc/rc.status
# Reset status of this service
rc_reset
case "$1" in
start)
echo "Starting DHCPALERT"
for (( I = 1; I <= DHCP_AL_DAEMONS; ++I )); do
REF="DHCP_AL_${I}[#]"
COMMAND=(/sbin/startproc -p "${DHCP_AL_PID_DIR}p_${I}.pid" -l "${DHCP_AL_LOG_DIR}log_${I}.log" "$DHCPALERT_BIN" "${!REF}")
echo "COMMAND: ${COMMAND[*]}"
"${COMMAND[#]}" &
PID=$!
echo "PID: $PID"
done
rc_status -v
;;
stop)
echo -n "Shutting down DHCPALERT "
/sbin/killproc -TERM "$DHCPALERT_BIN"
rc_status -v
;;
try-restart|condrestart)
[[ $1 == condrestart ]] && echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
"$0" status ## ??
if [[ $? -eq 0 ]]; then
"$0" restart
else
rc_reset # Not running is not a failure.
fi
rc_status
;;
restart)
"$0" stop
"$0" start
rc_status
;;
force-reload)
echo -n "Reload service DHCPALERT "
/sbin/killproc -HUP "$DHCPALERT_BIN"
rc_status -v
;;
reload)
echo -n "Reload service DHCPALERT "
/sbin/killproc -HUP "$DHCPALERT_BIN"
rc_status -v
;;
status)
echo -n "Checking for service DHCPALERT "
/sbin/checkproc "$DHCPALERT_BIN"
rc_status -v
;;
probe)
[[ /etc/DHCPALERT/DHCPALERT.conf -nt /var/run/DHCPALERT.pid ]] & echo reload
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
exit 1
;;
esac
rc_exit
Config file:
## Specifiy where to store the Pid files
DHCP_AL_PID_DIR="/var/log/sthserver/dhcpalert/"
##
## Specifiy where to store the Log file
DHCP_AL_LOG_DIR="/var/log/sthserver/dhcpalert/"
##
## is needed to determine how many vars should be read and started!
DHCP_AL_DAEMONS="2"
##
## Then DHCP_AL_<number> to specify the command that one instance of
## dhcpalert should be started
DHCP_AL_1=(-i eth0 -c ./test.sh -a 00:15:5D:0A:16:06 -v)
DHCP_AL_2=(-i eth1 -c ./test.sh -a 00:15:5D:0A:16:07 -v)
When you have a shell-command inside parentheses you start a new sub-shell. You run this sub-shell in the background and it's that sub-shells process id you get with $!.
There are two solutions: The first is to not run the /sbin/startproc command in a subshell, but directly and put that in the background. The secomd solution is to monitor the pid file created by /sbin/startproc.
I have this script which I would like to switch to the user "terraria" before starting the daemon. I can't figure out how to do it. My research brings me to bash scripts using su my_user -c, but I don't think that works in this case.
#!/bin/bash
# Terraria daemon
# chkconfig: 345 20 80
# description: Terraria Server
# processname: TerrariaServer.exe
DAEMON_PATH="/usr/Terraria"
DAEMON=TerrariaServer.exe
DAEMONOPTS="-world This_Land.wld -port 7777 "
NAME=TerrariaServer
DESC="Terraria Server"
PIDFILE=/var/run/TerrariaServer.pid
SCRIPTNAME=/etc/init.d/Terraria-Server
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`mono $DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s\n" "Fail"
else
echo $PID > $PIDFILE
printf "%s\n" "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -HUP $PID
printf "%s\n" "Ok"
rm -f $PIDFILE
else
printf "%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
Check out the following link for the 'DJB' way of starting up processes as other users:
http://thedjbway.b0llix.net/daemontools/uidgid.html
Also, see:
How to run a command as a specific user in an init script?