重新启动时,无论我们尝试什么,我们的服务脚本始终以root身份启动。我们需要脚本作为指定用户运行。如果我们尝试使用例如sudo -u用户名或su用户名,那么PID会与sudo绑定吗?
#! /bin/sh
### BEGIN INIT INFO
# Provides: programer
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: programer supervisor process v2.5
# Description: This file starts stops and monitors the programer process
### END INIT INFO
JAVA_PATH=/usr/bin/programer-java/bin/java
JAVA_OPTS="-Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
AGENT_PATH=/etc/programer
AGENT=agents.jar
SCRIPT="sudo -u username $JAVA_PATH $JAVA_OPTS -jar $AGENT_PATH/$AGENT"
LOGFILE=$AGENT_PATH/programer.log
PIDFILE=$AGENT_PATH/programer.pid
start() {
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
printf " programer service already running...\n"
return 1
fi
printf " Starting programer service...\n"
local CMD="$SCRIPT"
cd $AGENT_PATH
$CMD > $LOGFILE 2>&1 &
PID=$!
echo $PID > $PIDFILE
chown programer:programer $PIDFILE
printf " programer service started...\n"
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
printf " programer service not running...\n"
#return 1
else
printf " Stopping programer service...\n"
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
printf " programer service stopped...\n"
fi
# Wait for known process to stop, then look for rogue agents
sleep 5
procs=(`ps -ef | grep -e $AGENT | grep -v -e 'grep' -e 'sudo' | awk '{print $1","$2}'`)
for proc in ${procs[@]}
do
pid=`echo $proc | awk -F, '{print $2}'`
owner=`echo $proc | awk -F, '{print $1}'`
if [ $owner != "programer" ]; then
printf " $0 - WARNING: PID=$pid is owned by $owner!!!! Please use \"sudo kill -9 $pid\" to kill this rogue instance of the programer service\n"
else
printf " $0 - WARNING: Stopping rogue programer service PID=$pid\n"
kill -15 $pid
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo "Usage: basename $0 {start|stop|restart}"
esac
exit 0
答案 0 :(得分:0)
查看start-stop-daemon
(dpkg
软件包的一部分,已安装在您的系统上),这是一个完全针对您的问题编写的实用程序。
它负责