首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从Pycharm中分离运行python脚本,使其始终附加到文件中?

如何从Pycharm中分离运行python脚本,使其始终附加到文件中?
EN

Stack Overflow用户
提问于 2014-04-15 20:32:48
回答 1查看 5.1K关注 0票数 8

我正在远程系统上工作,我使用Pycharm窗口来编辑和运行我的脚本。

我使用

代码语言:javascript
运行
复制
ssh -Y myName@myMachine

然后我从终端机运行Pycharm。

我想以这样的方式从Pycharm运行我的脚本,如果我关闭它(甚至从ssh会话中注销),进程仍然会运行。

我尝试使用“分离而不终止进程”选项退出Pycharm。这将导致python过程显示在以下列表中:

代码语言:javascript
运行
复制
ps -all

但是,它停止了对文件的写入。当py魅力打开时,进程通常每隔几秒钟写一次文件。当它脱离py魅力时,它会显示在进程列表中(在注销和再次登录之后,它在ps -x中显示未知的tty),但是它停止工作,因为它不再将任何输出附加到它通常应该添加的文件中。

是什么引起的?我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2019-04-05 15:54:11

有几种方法:

  1. 使用终端仿真器作为tmuxscreen

代码语言:javascript
运行
复制
1. `tmux` - The tmux is a terminal multiplexer that enables a number of terminals to be created, accessed, and controlled from a single screen. A tmux session can be detached from a screen and continue running in the background, then later reattached. Like Screen tool, you can also use tmux to detach from an SSH session without quitting the remote jobs.

安装tmux后,使用命令启动tmux会话:

$ tmux

现在,开始你的任务或工作。然后,在不退出远程作业的情况下,安全地从tmux会话中分离出来,方法是按“CTRL”,然后按"d"。这将分离您的tmux会话,但将使您在该会话中所做的工作在后台运行。这意味着即使您与会话断开连接,所有的遥控器也将运行。

若要列出可用的会话,请运行:

$ tmux ls

您可以使用相应的会话ID重新附加到tmux会话,如下所示:

$ tmux附加-t <会话ID >

有关详细信息,请参阅手册页。

$ man tmux

代码语言:javascript
运行
复制
2. `screen` - The screen tool, a full-screen window manager with VT100/ANSI terminal emulation, allows you to safely detach from the SSH session without exiting the remote job. It will be helpful for those who are working with multiple remote servers.

在远程系统上安装屏幕后,启动屏幕会话:

银幕

屏幕会话现在已经启动。现在运行您想在远程系统上执行的任何作业或任务,然后可以通过按“Ctrl”“d”退出屏幕会话,在脱离屏幕会话后,您可以从远程系统注销。远程作业将继续在服务器中运行。

若要列出屏幕会话,请运行:

$ screen -ls

您可以使用相应的会话ID重新附加到屏幕会话,如下所示:

$ screen -r <会话ID >

有关详细信息,请参阅手册页。

$ man屏幕

  1. 使用在后台执行的分离命令(也请阅读):

代码语言:javascript
运行
复制
1. `nohup` - stands for **No h**ang**up**, is yet another command line utility to help you run Linux commands even after you’re disconnected from the SSH sessions.

这个用法是绝对容易的。登录到远程系统后,您所要做的就是:

$ nohup < command >&

现在,您可以退出SSH会话。远程作业将继续运行。

要列出正在运行的作业,请运行:

-l $ jobs

有关详细信息,请参阅手册页。

$ man nohup

代码语言:javascript
运行
复制
2. `disown` - Disown, removes the job from the process job list of the system, so the process is shielded from being killed during session disconnection as it won’t receive SIGHUP by the shell when you logout.

登录到远程系统后,使用“&”运行命令:

$< command >&

然后,使用以下方法列出正在运行的作业:

-l $ jobs

然后运行与进程ID不一致的进程,如下所示:

不承认-h < PID >

现在可以断开与服务器的连接。

有关详细信息,请参阅手册页。

$ man nohup

登录到远程系统后的

代码语言:javascript
运行
复制
3. `setsid` - setsid allocates a new process group to the process being executed and hence, the process created is totally in a newly allocated process group and can execute safely without fear of being killed even after session logout.

,运行:

$ setsid < command >

有关详细信息,请参阅手册页。

$ man nohup

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23093971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档