参考链接

基本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash

__FILE__="$0"
REAL_FILE=`readlink "${__FILE__}"`
if [ ! -z "${REAL_FILE}" ]; then
__FILE__="${REAL_FILE}"
fi

__DIR__=`cd "$(dirname "${__FILE__}")"; pwd`

RUNDIR="${__DIR__}"
PHPFILE="${RUNDIR}/xxxx.php"
PIDFILE="${RUNDIR}/xxxx.pid"
LOGFILE="${RUNDIR}/xxxx.log"
TASKNAME="XXXXXXXX"

if [ ! -f "${PHPFILE}" ]; then
echo 'Missing the task file'
exit
fi

get_pid() {
if [ -f ${PIDFILE} ]; then
cat ${PIDFILE}
fi
}

start() {
local PID=$(get_pid)
if [ ! -z ${PID} ]; then
echo "The ${TASKNAME}(${PID}) is running."
echo "You should stop it before you start."
return
fi

touch ${PIDFILE}

echo "Starting ${TASKNAME}..."
nohup php ${PHPFILE} >>${LOGFILE} 2>&1 &
echo $! > ${PIDFILE}
}

stop() {
local PID=$(get_pid)
if [ -z ${PID} ]; then
echo "${TASKNAME} is not running."
return
fi

echo "Stopping ${TASKNAME}..."
get_pid | xargs kill -9
rm -f ${PIDFILE}
}

status() {
local PID=$(get_pid)
if [ ! -z ${PID} ]; then
echo "${TASKNAME}(${PID}) is running."
else
echo "${TASKNAME} is not running."
fi
}

case "$1" in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

status)
status
;;

*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0