启动程序的脚本怎么写

时间:2025-01-27 15:52:38 单机游戏

启动程序的脚本可以根据不同的操作系统和需求有不同的写法。以下是一些常见的启动脚本示例:

Linux下的Shell脚本

示例1:简单的启动脚本

```bash

!/bin/bash

start_program.sh

检查程序是否已经在运行

count=$(ps -ef | grep './bin/testMain' | grep -v grep | wc -l)

如果程序已经在运行,则输出提示信息

if [ $count -gt 0 ]; then

echo "testMain server is running!"

else

否则,启动程序

./shell/start_test.sh

sleep 1s

fi

```

示例2:带状态检查的启动脚本

```bash

!/bin/bash

start_program_with_status.sh

检查程序是否已经在运行

count=$(ps -ef | grep './bin/testMain' | grep -v grep | wc -l)

如果程序已经在运行,则输出提示信息

if [ $count -gt 0 ]; then

echo "testMain server is already running."

else

否则,启动程序并检查是否成功

./shell/start_test.sh

sleep 1s

if [ $? -eq 0 ]; then

echo "testMain server started successfully."

else

echo "Failed to start testMain server."

fi

fi

```

Windows下的批处理脚本

示例1:简单的启动脚本

```batch

@echo off

REM start_program.bat

REM 检查程序是否已经在运行

tasklist | findstr /i "testMain.exe" >nul

REM 如果程序已经在运行,则输出提示信息

if %errorlevel% neq 0

echo testMain is already running.

else

REM 否则,启动程序

start "" "C:\path\to\testMain.exe"

fi

```

示例2:带状态检查的启动脚本

```batch

@echo off

REM start_program_with_status.bat

REM 检查程序是否已经在运行

tasklist | findstr /i "testMain.exe" >nul

REM 如果程序已经在运行,则输出提示信息

if %errorlevel% neq 0

echo testMain is already running.

else

REM 否则,启动程序并检查是否成功

start "" "C:\path\to\testMain.exe"

if %errorlevel% neq 0

echo Failed to start testMain.

else

echo testMain started successfully.

fi

fi

```

Linux下的Systemd服务脚本

示例1:简单的Systemd服务文件

```ini

[Unit]

Description=testMain Server

After=network.target

[Service]

ExecStart=/path/to/start_test.sh

Restart=always

User=your_user

Group=your_group

Environment=LD_LIBRARY_PATH=/path/to/module/lib

[Install]

WantedBy=multi-user.target

```

示例2:带状态检查的Systemd服务文件

```ini

[Unit]

Description=testMain Server

After=network.target

[Service]

ExecStart=/path/to/start_test.sh

Restart=always

User=your_user

Group=your_group

Environment=LD_LIBRARY_PATH=/path/to/module/lib

[Install]

WantedBy=multi-user.target

```

Windows下的任务计划程序脚本

示例1:简单的任务计划程序脚本

```vbscript

Set WshShell = WScript.CreateObject("WScript.Shell")

RegPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\"

KeyName = "testMain"

RegData = """C:\path\to\testMain.exe"""

WshShell.RegWrite RegPath & KeyName, RegData, "REG_SZ"

```

示例2:带状态检查的任务计划程序脚本