情况描述:现在我在vps上运行着一个xxx.py(是个多线程的)程序。
现在关闭它的办法是:登录vps,然后ps -aux | grep xxx找到运行的id(这个id每次都变的),然后kill 9 id。
每次结束都需要执行上述步骤,比较麻烦。
能不能远程结束它?
重庆网友:比如
- package main
- import (
- "fmt"
- "log"
- "net/http"
- "os/exec"
- )
- const action = `kill -9 $(ps -ef | grep [x]xx.py | awk ‘{print $2}’)`
- func main(){
- http.HandleFunc("/call",RemoteCall)
- log.Println("Start Server http://0.0.0.0:18080"😉
- log.Fatal(http.ListenAndServe("0.0.0.0:18080",nil))
- }
- func RemoteCall(w http.ResponseWriter, r *http.Request){
- cmd := exec.Command(action)
- err := cmd.Start()
- if err != nil{
- fmt.Fprintln(w,err)
- return
- }
- fmt.Fprintln(w,"ok")
- }
复制代码
浏览器访问 http://ip:18080/call
香港网友:楼下来说,
吉林网友:撸个api远程调用
陕西网友:这是个什么情况
福建网友:手机上下载个ssh客户端?
不知道你的结束判断条件是啥,为啥不写到py里,让其自己结束
海南网友:Go语言好评
海南网友:装个面板,里面有进程管理~
青海网友:
- #!/bin/bash
- username=xxxxxx
- host=x.x.x.x
- passwd=xxxxxxx
- pname=xxx.py
- /usr/bin/expect <<EOF
- spawn ssh $username@$host
- expect {
- "yes/no" {send "yes\r"; exp_continue }
- "password:" { send "$passwd\r" }
- }
- expect "#"
- send "killall -9 $pname\r"
- expect "#"
- send "exit\r"
- expect eof
- EOF
- echo "—- end — "
复制代码
江西网友:要装个expect,
apt-get install expect
yum install expect
天津网友:要装个expect,
apt-get install expect
yum install expect
河南网友:装个screen多简单
黑龙江网友:谢谢!
吉林网友:灵活点
你只要换成 pkill 命令就行了, xxx是你的名字
- pkill -9 xxx
复制代码
未经允许不得转载:美国VPS_搬瓦工CN2 GIA VPS » 能不能远程结束vps上运行着的程序?