Commit 2fa52818 authored by Bess严根旺's avatar Bess严根旺

脚本

parent 6aadbc2a
package main
import (
"fmt"
"os"
"os/exec"
)
func startScript(scriptName string) {
cmd := exec.Command("nohup", "go", "run", scriptName, "&")
err := cmd.Start()
if err != nil {
fmt.Printf("Error starting %s: %v\n", scriptName, err)
}
}
func restartScript(scriptName string) {
// Stop the script if it's already running
stopScript(scriptName)
// Start the script again
startScript(scriptName)
}
func stopScript(scriptName string) {
cmd := exec.Command("pkill", "-f", scriptName)
err := cmd.Run()
if err != nil {
fmt.Printf("Error stopping %s: %v\n", scriptName, err)
}
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: control_script <command> <script_name>")
return
}
command := os.Args[1]
scriptName := os.Args[2]
switch command {
case "start":
startScript(scriptName)
case "restart":
restartScript(scriptName)
case "stop":
stopScript(scriptName)
default:
fmt.Println("Unknown command. Use 'start', 'restart', or 'stop'.")
}
}
package main
import (
"fmt"
"time"
)
func main() {
for {
// 数据刷写逻辑
fmt.Println("Script 1 is running...")
time.Sleep(time.Second * 5) // 每隔 5 秒执行一次
}
}
package main
import (
"fmt"
"time"
)
func main() {
for {
// 数据刷写逻辑
fmt.Println("Script 2 is running...")
time.Sleep(time.Second * 5) // 每隔 5 秒执行一次
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment