目录

在Linux上启动程序后台运行时,往往需要输入一堆复杂的命令,为了能快速编写一个完善的启动脚本,整理一个通用的启停脚本模板如下。

脚本支持从任意位置执行,不存在路径问题,启动脚本和停止脚本最好一起配合使用。

启动脚本

#!/bin/bash

current_path=$(cd `dirname $0`; pwd)
parent_path=$(cd ${current_path} ; cd ..; pwd)
conf_path=$parent_path/conf
log_path=$parent_path/log
#echo $current_path
#echo $parent_path pid_file=$current_path/xxx.pid
arg1=$1
arg2=$2 function print_usage() {
echo ""
echo "Usage:"
echo " sh $0 <arg1> <arg2>"
echo ""
echo " e.g:"
echo " sh $0 yyy zzz"
echo ""
} if [[ ! "${arg1}" ]]; then
print_usage
exit 1
fi if [[ ! "${arg2}" ]]; then
print_usage
exit 1
fi #print_usage if [[ -f "${pid_file}" ]]; then
pid=`cat ${pid_file}`
pid_exist=$(ps aux | awk '{print $2}'| grep -w $pid)
if [[ "$pid_exist" ]]; then
echo ""
echo "process is running: ${pid}, Please stop it first!"
echo ""
exit 1
fi
fi echo "startup..."
echo "arg1: ${arg1}, arg2: ${arg2}" ## 启动Python进程示例
#nohup python ${parent_path}/test_tasks.py -s ${arg1} -c ${arg2} > /dev/null 2>&1 & echo $! > $pid_file ## 启动Java进程示例
## set java path
#if [ -z "$JAVA" ]; then
# JAVA=$(which java)
#fi #if [ -z "$JAVA" ]; then
# echo "java not install, Please install java first" 2>&2
# exit 1
#fi ## set classpath
#CLASSPATH=$parent_path/conf
#for i in $parent_path/libs/*;
#do
# CLASSPATH="$CLASSPATH":$i
#done #OPTS_APP="-Dlogback.configurationFile=$conf_path/logback.xml -Dlog.dir=$log_path"
#OPTS_MEM="-Xms1024m -Xmx1024m"
#OPTS_JAVA_EXT="-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"
#OPTS_JAVA="$OPTS_JAVA_EXT $OPTS_MEM $OPTS_APP" ## 方式1: 明确指定main方法所在的入口类
#nohup $JAVA $OPTS_JAVA -classpath $CLASSPATH xxx.yyy.zzz.BootstrapClass > /dev/null 2>&1 & echo $! > $pid_file ## 方式2: 直接指定可执行jar包
#nohup $JAVA $OPTS_JAVA -jar xxx.jar > /dev/null 2>&1 & echo $! > $pid_file sleep 1
echo "started."
echo "process id: `cat ${pid_file}`"
echo ""

根据实际情况,将脚本模板中的xxxarg1arg2修改为对应的名称即可。

停止脚本

#!/bin/bash

current_path=$(cd `dirname $0`; pwd)
#echo $current_path pid_file=$current_path/xxx.pid if [[ ! -f "${pid_file}" ]]; then
echo "pid file not exists"
echo ""
exit 1
fi echo "stopping..."
pid=`cat ${pid_file}`
echo "process id: ${pid}"
kill -15 $pid
sleep 1 # check process exists
pid_exist=$(ps aux | awk '{print $2}'| grep -w $pid)
if [[ ! "${pid_exist}" ]]; then
# echo the process $pid is not exist
rm -rf ${pid_file}
else
# echo the process $pid exist
kill -9 $pid
sleep 1
rm -rf ${pid_file}
fi echo "stopped."

根据实际情况,将脚本模板中xxx修改为对应名称即可。

【参考】

https://blog.csdn.net/baidu_38558076/article/details/88191567 shell 判断pid是否真实存在

https://blog.csdn.net/sunt2018/article/details/102523112 后台启动 nohup,并记录pid号

Linux后台进程启停脚本模板的更多相关文章

  1. Linux 程序启停脚本

    start.sh #!/bin/sh java -jar ./program.jar & echo $! > /var/run/program.pid stop.sh #!/bin/sh ...

  2. linux下的启停脚本

    linux下的根据项目名称,进行进程的启停脚本 #!/bin/bash JAVA=/usr/bin/java APP_HOME=/opt/program/qa/wechat APP_NAME=prog ...

  3. case编写的httpd简单启停脚本

    case编写的httpd简单启停脚本 #!/bin/bash HTTPD="/etc/init.d/httpd" . /etc/init.d/functions case &quo ...

  4. Oracle BIEE启停脚本

    作为BI的开发人员,经常启停BI服务在所难免,启动的过程又比较长,命令需要不同目录切换,简直烦死人呢, 特意整理了linux中的启动脚本,将以下脚本存成biee.sh,后面的过程就相当简单了, 启动: ...

  5. nginx启停脚本

    安装nginx时,源码包中未带官方的启动脚本,也就无法使用service nginxd start这种启动方式,查了下资料自己写了一个: #!/bin/bash #@version: #@author ...

  6. Oracle EBS R12的启停脚本

    以下脚本用root用户登录执行: 一.DB启停使用EBS提供的脚本ebs_start.shsu - oraprod -c "/d01/oracle/PROD/db/tech_st/10.2. ...

  7. python启停脚本的编写

    先占个位置,打算记录一下启停python 的脚本start.sh,stop.sh的写法. 内容包括启动python脚本的方式,日志的重定向,获取进程id,以及杀掉进程. 参考文章: http://bl ...

  8. mysql自己编写启停脚本

    一.场景 在实际生产环境中要求相对较高的企业对于root权限管控相对比较高,故而很多软件并非安装在root用户下(当然root用户权限也可管理,但正常情况下root权限并不会给业务使用方,而会给一个普 ...

  9. Linux weblogic启停

    一般weblogic启停在windows下很方便使用图标方式.但是在linux下需要杀掉weblogic进程才能真正关掉weblogic. 1.查询weblogic进程 ps -ef | grep & ...

  10. liunx weblogic服务启停脚本

    #!/bin/bash #sh xx.sh start xx项目 例如:sh autoWeblogic.sh start bius #经测试发现weblogic 启动大概需要完全启动成功35秒左右 停 ...

随机推荐

  1. [转帖]tiup cluster scale-in

    https://docs.pingcap.com/zh/tidb/stable/tiup-component-cluster-scale-in tiup cluster scale-in 命令用于集群 ...

  2. [转帖]Ceph简单搭建

    https://cloud.tencent.com/developer/article/1643322 Ceph基础介绍 ​ Ceph是一个可靠地.自动重均衡.自动恢复的分布式存储系统,根据场景划分可 ...

  3. [转帖]CertUtil: -hashfile 失败: 0xd00000bb (-805306181)

    https://www.cnblogs.com/heenhui2016/p/de.html 使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile ...

  4. [转帖] Linux命令拾遗-查看系统信息

    https://www.cnblogs.com/codelogs/p/16060714.html 简介# 作为一名程序员,有时需要关注自己的进程运行在什么样的软硬件环境里,比如几核cpu.固态硬盘还是 ...

  5. [转帖]【Python】计算程序运行时间的方法总结

    一.第一种方法 利用time包: import time def test(): start_time = time.time() # 记录程序开始运行时间 s = 0 for i in range( ...

  6. [转帖]关于F5负载均衡你认识多少?

    https://www.cnblogs.com/xiexun/p/10718348.html 网络负载均衡(load balance),就是将负载(工作任务)进行平衡.分摊到多个操作单元上进行执行,例 ...

  7. 是否开启超线程对CPU不同命令的影响情况

    背景 最近公司购买了一台服务器, 要进行一次性能测试. 基于此, 我这边进行了一下超线程与否的测试验证 使用stress-ng的命令,对所有的 CPU 方法进行测试 然后只分析 bogo ops/s ...

  8. kettle系统列文章03---如何建立一个作业

    上篇文章我们建立好了转换,我们希望这个转换可以做成定时任务,每一分钟执行一次 第一步:创建作业开始节点:文件---->新建---->作业----核心对象---->通用-----> ...

  9. 【三】强化学习之PaddlePaddlle-Notebook、&pdb、ipdb 调试---及PARL框架

    相关文章: [一]飞桨paddle[GPU.CPU]安装以及环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简 ...

  10. 构建LVS负载均衡集群

    LVS即Linux虚拟服务器,目前 LVS 已经被集成到 Linux 内核模块中,该项目在 Linux 内核实现了基于 IP 的数据请求负载均衡调度方案,LVS集群采用IP负载均衡技术和基于内容请求分 ...