1、制作程序启动脚本
scenarioDaemon.sh
#!/bin/sh
#set env
export ORACLE_HOME=/opt/oracle/product/11.2.0
export TNS_ADMIN=$ORACLE_CLIENT_HOME/network/admin
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
export CPLUS_INCLUDE_PATH=/usr/local/boost_1_64_0:/opt/oracle/product/11.2.0/rdbms/public
export LIBRARY_PATH=/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:$ORACLE_HOME/lib:/usr/local/boost_1_64_0/stage/lib
export LD_LIBRARY_PATH=/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:$ORACLE_HOME/lib:/usr/local/boost_1_64_0/stage/lib
export PATH=$PATH:$ORACLE_HOME/bin
currentpath=$(pwd)
basepath=$(cd `dirname $0`; pwd)
case $1 in
start)
path="$basepath/scenarioserver.pid"
if [ -f $path ]; then
pid=`cat $basepath/scenarioserver.pid`
echo "进程$pid正在运行..."
else
cd $basepath
nohup $basepath/scenarioserver run &
echo $! > $basepath/scenarioserver.pid
echo "运行进程$!..."
cd $currentpath
fi
;;
stop)
pid=`cat $basepath/scenarioserver.pid`
kill -9 $pid
echo "结束进程$pid..."
rm -rf $basepath/scenarioserver.pid
;;
pid=`cat $basepath/scenarioserver.pid`
kill -9 $pid
echo "结束进程$pid..."
rm -rf $basepath/scenarioserver.pid
cd $basepath
nohup $basepath/scenarioserver run &
echo $! > $basepath/scenarioserver.pid
echo "运行进程$!..."
cd $currentpath
else
echo "进程没运行..."
cd $basepath
nohup $basepath/scenarioserver run &
echo $! > $basepath/scenarioserver.pid
echo "运行进程$!..."
cd $currentpath
fi
;;
status)
path="$basepath/scenarioserver.pid"
if [ -f $path ];then
pid=`cat $basepath/scenarioserver.pid`
echo "进程$pid正在运行..."
else
echo "进程没运行..."
fi
;;
*)
echo "Usage:{start|stop|restart|status}"
;;
esac
2、添加启动服务
vim /usr/lib/systemd/system/scenarioserver.service
[Unit]
Description=scenarioserver
After=network.target
[Service]
Type=forking
ExecStart=xxxx/scenarioDaemon.sh start
ExecReload=xxxx/scenarioDaemon.sh restart
ExecStop=xxxx/scenarioDaemon.sh stop
PrivateTmp=true
User=指定启动用户名
Group=指定启动用户组
[Install]
WantedBy=multi-user.target
3、执行启动命令
启动服务
systemctl start scenarioserver
停止服务
systemctl stop scenarioserver
重启服务
systemctl restart scenarioserver
查看服务状态
systemctl status scenarioserver -l
4、注意
制作程序启动脚本时,必须设置环境变量。我当时没有设置环境变量,手动执行shell时没问题,服务调用时oracle连接报错。