Linux里startup.sh 和 shutdown.sh
最近用socket编写了一个服务端程序,监听1024端口,检测客户端发来的请求,所在Linux里写启动和停止的脚本:
在Eclipse里java写好程序,右击导出生成 Runnable JAR file 然后socketServer.jar上传到服务器即可。
1、启动 startup.sh
# ----------------------------------------------------
# Start script for the Socekt Server
# ----------------------------------------------------
#!/bin/sh
ps -ef |grep socketServer.jar |grep -v grep
if [ $? -eq 0 ];then
echo 'Socket server is running!'
else
netstat -apn |grep 1024
if [ $? -eq 0 ];then
PID_1024 = $(echo `netstat -apn |grep 1024 | awk '{print $NF}'|awk -F '/' '{print $1}'`)
kill $PID_1024
fi
echo "startup socket server:"
nohup java -jar socketServer.jar &
fi
2、启动 shutdown.sh
# ----------------------------------------------------
# Stop script for the Socekt Server
# ----------------------------------------------------
#!/bin/sh
ps -ef | grep socketServer | grep -v grep
if [ $? -eq 0 ];then
PID_1024=$(echo `netstat -apn |grep 1024 | awk '{print $NF}'|awk -F '/' '{print $1}'`)
kill $PID_1024
echo 'Socket server has shutdown!'
else
echo 'Not found socket PID!'
fi
同时,设定文件操作权限:chmod 755 startup.sh , chmod 755 shutdown.sh
最后执行 ./startup.sh 即可完成启动!