1. 創(chuàng)建啟動(dòng)腳本
#!/bin/bash
WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE
mkdir -p log conf
module=
app=fileserver
conf=conf/cfg.json
pidfile=conf/fileserver.pid
logfile=log/fileserver.log
function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}
function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi
nohup ./$app &> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}
function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}
function restart() {
stop
sleep 1
start
}
function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running, pid="
cat $pidfile
else
echo "$app is stoped"
fi
}
function tailf() {
tail -f $logfile
}
function build() {
go build
if [ $? -ne 0 ]; then
exit $?
fi
mv $module $app
./$app -v | grep -v "config"
}
function pack() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
file_list="control cfg.example.json $app"
tar zcf $app-$version.tar.gz gitversion $file_list
}
function packbin() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
tar zcvf $app-bin-$version.tar.gz $app gitversion
}
function help() {
echo "$0 start|stop|restart|status|tail"
}
if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
else
help
fi
2.測(cè)試腳本
cd go-fastdfs
chmod +x control
./control start|stop|status #對(duì)和序進(jìn)行啟動(dòng)兽泣,停止桦沉,查看狀態(tài)等,注意事項(xiàng):確保control與fileserver在同一個(gè)目錄
- 遇到問(wèn)題并解決
[root@localhost go-fastdfs]# vi control
[root@localhost go-fastdfs]# ./control status
-bash: ./control: /bin/bash^M: 壞的解釋器: 沒(méi)有那個(gè)文件或目錄
[root@localhost go-fastdfs]# sed -i 's/\r$//' control
[root@localhost go-fastdfs]# ./control status
fileserver is stoped
3.添加開(kāi)機(jī)啟動(dòng)
在rc.local 添加以下命令
cd /home/go-fastdfs/ && ./control start
[root@localhost go-fastdfs]# vim /etc/rc.d/rc.local
image.png
給rc.local 添加執(zhí)行權(quán)限
[root@localhost go-fastdfs]# chmod +x /etc/rc.d/rc.local