linux C++ 中如何使用stop来终止正在运行的程序?

linux C++ 中如何使用stop来终止正在运行的程序?,第1张

不一定写在程序里,可以写成脚本 http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

除非你的程序在退出前要处理一些事务,那么用kill给一个信号,在信号里处理退出前的工作

Linux系统为Ubuntu 一、Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start or $ sudo /etc/init.d/apache2 start 二、 Restart Apache 2 Server /重启apache服务 # /etc/init.d/apache2 restart or $ sudo /etc/init.d/apache

# resolve links - $0 may be a softlink

PRG="$0" <--- 执行程序名

# 以下循环判断可执行文件是否是一个符号连接(symbolic link,也叫软连接),如果是符号连接,找到实际指向的文件位置

while [ -h "$PRG" ] do

ls=`ls -ld "$PRG"`

link=`expr "$ls" : '.*->\(.*\)$'`

if expr "$link" : '/.*' >/dev/nullthen

PRG="$link"

else

PRG=`dirname "$PRG"`/"$link"

fi

done

# 此时,$PRG 指向实际的文件

PRGDIR=`dirname "$PRG"`

EXECUTABLE=catalina.sh

# Check that target executable exists

# 判断这个实际的目标文件是否存在且是可执行文件

if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]then

echo "Cannot find $PRGDIR/$EXECUTABLE"

echo "This file is needed to run this program"

exit 1

fi

# 执行目标文件

exec "$PRGDIR"/"$EXECUTABLE" stop "$@"

这个脚本的目的是解决通过符号连接去执行一个程序的问题。如果是符号连接,需要找到实际指向的目标文件,然后再执行它的 stop 命令。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/7354828.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-04
下一篇2023-04-04

发表评论

登录后才能评论

评论列表(0条)

    保存