LinuxLinux【Linux】DotNet运行命令Think.Wang2024-07-012024-12-19设置系统级环境变量1234567891011121314# /etc/profile.d/{文件名.sh} 新建并打开配置文件vi /etc/profile.d/dotnet.sh# 文件末尾加变量定义# 此处我的dotnet文件在 这个位置 /home/sys_wh/dotnetexport DOTNET_ROOT=/home/sys_wh/dotnetexport PATH=$PATH:/home/sys_wh/dotnet# 重载配置文件source /etc/profile.d/dotnet.sh# 终端使用变量,执行以下命令查看是否有版本输出,最好切换下用户测试是否所有用户都可以读到变量dotnet info 启动脚本12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879#!/bin/bash #这里可替换为你自己的执行程序,其他代码无需更改APP_NAME=CATSTI.YZ.dll#这里修改程序运行的端口号APP_PORT=8181 #使用说明,用来提示输入参数usage() { echo "Usage: sh YZServiceStart.sh [start|stop|restart|status]" exit 1} #检查程序是否在运行is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi} #启动方法start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup dotnet $APP_NAME --urls=http://*:$APP_PORT > /dev/null 2>&1 & echo "${APP_NAME} start success" fi} #停止方法stop(){ is_exist if [ $? -eq "0" ]; then kill -9 $pid else echo "${APP_NAME} is not running" fi } #输出运行状态status(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. Pid is ${pid}" else echo "${APP_NAME} is NOT running." fi} #重启restart(){ stop start} #根据输入参数,选择执行对应方法,不输入则执行使用说明case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;;esac 批量启动脚本1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162#!/bin/bash # 应用程序的路径APP_PATH="/home/sys_wh/yz/30-NewYZ/CATSTI.YZ.dll" # 启动的端口列表PORTS=( "5000" "5001" "5002" ) start() { # 循环启动每个端口的应用程序 for PORT in "${PORTS[@]}"; do nohup dotnet $APP_PATH --urls http://*:$PORT > "app_$PORT.log" 2>&1 & echo "端口 $PORT 已启动" done echo "应用程序已批量启动。"} stop() { # 循环启动每个端口的应用程序 for PORT in "${PORTS[@]}"; do # 停止端口为5000的应用程序 kill $(lsof -i :$PORT -t) echo "端口 $PORT 已停止" done echo "............应用程序已批量停止。............"}status() { for PORT in "${PORTS[@]}"; do # 检查端口是否在使用中 if [ -n "$(netstat -tuln | grep :$PORT)" ] || [ -n "$(ss -tuln | grep :$PORT)" ]; then echo "端口 $PORT 正在使用中" else echo "端口 $PORT 未被使用" fi done} case "$1" in start) start "$2" sleep 5s status "$2" ;; stop) stop "$2" ;; status) status "$2" ;; restart) stop "$2" sleep 3s start "$2" sleep 5s status "$2" ;; *) echo "第一个参数请输入:start|stop|restart|status" exit 1 ;;esac 注册系统服务1234567891011121314[Unit]Description=Dotnet Searver[Service]LimitMEMLOCK=infinityLimitNOFILE=65535WorkingDirectory=/appRestart=alwaysType=forkingExecStart=/app/startup.shExecStop=/usr/bin/kill -15 $MAINPID[Install]WantedBy=multi-user.target