This script is used to start, stop, restart, and check the status of the wasp-agent.
Here is a breakdown of the script:
Set the initial variables for the name, base directory path, app path, log path, pid file path, and port number.
Check the user running the script. It should be run as the "apps" user.
Verify that the base directory is a real directory, not a symbolic link.
BASEDIR/proc/
if [ "USER"!="apps"];thenecho"pleaserunbyapps"exit1elsesc=(stat /apps/svr/wasp | head -2 | tail -1 | grep symbolic)
if [ -n "$sc" ]; then
echo "Unsupport a symbolic link on /apps/svr/wasp, it must be a real directory!!!"
exit 1
fi
# chown -R apps:apps "/apps/logs/wasp"
# chown -R apps:apps "$BASE_DIR"
fi
if [ ! -f LOGPATH];thentouch{LOG_PATH}
fi
start() {
[ -x ${exec} ] || exit 5
# 判断进程文件是否存在
# if [ -f "$pidfile" ];then
# echo $"${pidfile} is exists, please check whether the process is running"
# exit 1
# fi
# 判断进程是否已存在
pidof_exsit=$(pidof "${name}")
if [[ -n "$pidof_exsit" ]]; then
echo "wasp-agent is running"
exit 0
fi
# 检查文件执行权限
if [ ! -x "$APP_PATH" ]; then
echo "${APP_PATH} is not allowed to execute"
exit 1
fi
# 开始启动
echo $"starting ${name}... "
(nohup ${APP_PATH} >>${LOG_PATH} 2>&1 &) && echo $! >"${pidfile}"
retval=$?
if [ ${retval} -eq 0 ]; then
pidof "${name}" >"${pidfile}"
else
echo "fail to start"
exit 1
fi
# 3秒查看端口是否监听
MAX_EXECUTION_TIME=20 # 设置最长执行时间为20秒
start_time=$(date +%s) # 记录脚本开始时间
while [[ $retval -eq 0 ]]; do
# 验证进程是否运行
pid_start=$(pidof "${name}")
# 检查wasp是否监听了10039端口
uid=$(grep '2737' /proc/${pid_start}/net/tcp | awk '{print $10}')
if [ -z "$uid" ]; then
check_port_start=""
else
check_port_start=$(ls -l /proc/${pid_start}/fd | grep socket | grep "$uid")
fi
# 同时成立表示已经成功启动
if [ -n "$pid_start" ] && [ -n "$check_port_start" ]; then
echo "start up successfully"
echo "wasp-agent is running, pid" $pid_start
break
fi
# 在循环内执行任务
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [[ $elapsed_time -gt $MAX_EXECUTION_TIME ]]; then
echo "start timeout"
echo "Fail to start wasp-agent"
echo "Check the run log for more information"
stop
retval=1
fi
sleep 0.2
done
return ${retval}
status() {
pid=(pidof"{name}")
uid=(grep′2737′/proc/{pid}/net/tcp | awk '{print 10}')
check_port_status=(ls -l /proc/pid/fd∣grepsocket∣grep"uid")
if [[ -n "pid" ]] && [[ -n "check_port_status" ]]; then
return 0
else
return 1
fi
}
ps_status() {
status
if [[ ?−eq0]];thenif[!−f{pidfile} ]; then
touch pidfilepidof"{name}" >"pidfile"fips−ef∣grep(pidof "name")∣grep{name} | grep -v 'grep'
else
echo "wasp-agent is not running"
fi
}
case "1"instart)statusif[[? -eq 0 ]]; then
echo "wasp-agent is already running"
ps_status
exit 0
fi
1;;stop)pidof"{name}"
if [[ ?−eq0]];thenstopelseecho"1
;;
status)
ps_status
;;
*)
echo "Usage:0 {start|stop|status|restart}"
exit 2
;;
esac
exit $?
Check the log file path and create it if it doesn't exist.
Define the start() function to start the wasp-agent.
Check if the process is already running. If yes, exit.
Check the executable permissions of the app path. If not allowed, exit.
Start the wasp-agent using nohup in the background.
Write the process ID to the pidfile.
Verify if the process is running and listening on the specified port.
After the maximum execution time (20 seconds), exit if the process is not fully started.
Define the stop() function to stop the wasp-agent.
Stop the wasp-agent using the killproc function provided by the /etc/init.d/functions script.
Define the restart() function to restart the wasp-agent.
Stop the wasp-agent.
Wait for 1 second.
Start the wasp-agent.
Define the reload() function. It is currently set to false and can be implemented if needed.
Define the status() function to check the status of the wasp-agent.
Get the process ID of the wasp-agent.
Check if the process ID exists and if the process is listening on the specified port.
Define the ps_status() function to display the process status.
Check the status of the wasp-agent.
If it is running, check the process using ps and output the result.
The case statement handles the command line arguments:
start: Check if the wasp-agent is already running, start it if not.