远程运行文件
Keruone
Contents
1. 使用 nohup
1.1 一般使用
- 假设有文件
test.py,需要参数1 2 3,在远程服务器上运行,可以使用 nohup 命令:nohup python test.py 1 2 3 &其中
&表示在后台运行。
-
如果需要在远程服务器上查看运行日志,可以使用 tail 命令:
tail -f nohup.out其中
nohup.out是 nohup 命令生成的日志文件。nohup.out 保存在当前目录下
1.2 重定向输出
- 如果需要将输出重定向到指定文件,可以使用
>符号:nohup python test.py 1 2 3 > output.log 2>&1 &其中
>表示将输出重定向到指定文件。2>&1表示将标准错误输出重定向到标准输出。output.log是指定的输出文件。
1.3 查看进程
- 查看进程
ps aux | grep test.py其中
ps aux表示查看所有进程。grep test.py表示过滤出包含test.py的进程。
1.4 杀死进程
- 杀死进程
kill -9 <pid>其中
kill -9表示强制杀死进程,也可以不使用-9,使用默认。<pid>是进程的 ID。
- 假设你的文件还会生成多个子进程,你想全部杀死,可以使用
kill $(ps aux | grep test.py | grep -v grep | awk '{print $2}')其中
ps aux表示查看所有进程。grep test.py表示过滤出包含test.py的进程。grep -v grep表示过滤出不包含grep的进程awk '{print $2}'表示打印出进程的 ID。
具体演示示例
假设系统中有以下进程:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 123 0.0 0.1 12345 6789 pts/0 S+ 10:00 0:00 python test.py
user 124 0.0 0.1 12345 6789 pts/1 S+ 10:01 0:00 python test.py
user 125 0.0 0.1 12345 6789 pts/2 S+ 10:02 0:00 python test.py
user 126 0.0 0.1 12345 6789 pts/3 S+ 10:03 0:00 python test.py
user 127 0.0 0.0 12345 6789 pts/4 S+ 10:04 0:00 grep test.py
命令执行过程
1 ps aux
- 列出所有进程,输出如下:
user 123 0.0 0.1 12345 6789 pts/0 S+ 10:00 0:00 python test.py user 124 0.0 0.1 12345 6789 pts/1 S+ 10:01 0:00 python test.py user 125 0.0 0.1 12345 6789 pts/2 S+ 10:02 0:00 python test.py user 126 0.0 0.1 12345 6789 pts/3 S+ 10:03 0:00 python test.py user 127 0.0 0.0 12345 6789 pts/4 S+ 10:04 0:00 grep test.py
2 grep test.py
- 过滤出包含
test.py的进程,输出如下:user 123 0.0 0.1 12345 6789 pts/0 S+ 10:00 0:00 python test.py user 124 0.0 0.1 12345 6789 pts/1 S+ 10:01 0:00 python test.py user 125 0.0 0.1 12345 6789 pts/2 S+ 10:02 0:00 python test.py user 126 0.0 0.1 12345 6789 pts/3 S+ 10:03 0:00 python test.py user 127 0.0 0.0 12345 6789 pts/4 S+ 10:04 0:00 grep test.py
3 grep -v grep
- 排除包含
grep的进程,输出如下:user 123 0.0 0.1 12345 6789 pts/0 S+ 10:00 0:00 python test.py user 124 0.0 0.1 12345 6789 pts/1 S+ 10:01 0:00 python test.py user 125 0.0 0.1 12345 6789 pts/2 S+ 10:02 0:00 python test.py user 126 0.0 0.1 12345 6789 pts/3 S+ 10:03 0:00 python test.py
4 awk '{print $2}'
- 提取进程 ID(
PID),输出如下:123 124 125 126
5 kill $(...)
- 将提取到的
PID作为参数传递给kill,最终执行的命令为:kill 123 124 125 126
最终效果
- 命令会向进程
123、124、125和126发送SIGTERM信号(默认信号),请求它们终止。 - 如果这些进程正确处理了
SIGTERM信号,它们会执行清理操作并退出。 - 如果进程没有响应
SIGTERM,可以使用kill -9强制终止:“`bash
kill -9 123 124 125 126
</li>
</ul><h2>
“`
PS: 我只使用过 nohup,剩下的没使用过,欢迎补充
2. 使用 screen
screen 是一个终端多路复用器,可以让你在一个会话中运行多个终端。使用方法如下:
- 启动
screen:screen - 运行你的 Python 脚本:
python your_script.py - 按
Ctrl + A然后按D键将会话分离,程序将继续在后台运行。 -
你可以通过以下命令重新连接到会话:
screen -r
3. 使用 tmux
tmux 和 screen 类似,也是一个终端复用工具。使用方法如下:
- 启动
tmux:tmux - 运行你的 Python 脚本:
python your_script.py - 按
Ctrl + B然后按D键将会话分离。 -
重新连接到会话:
tmux attach
4. 使用 supervisor
如果你需要更复杂的进程管理,可以考虑使用 supervisor。安装和配置方法如下:
- 安装
supervisor:pip install supervisor - 创建配置文件,例如
your_script.conf:[program:your_script] command=python /path/to/your_script.py autostart=true autorestart=true - 启动
supervisor:supervisord -c /path/to/your_script.conf