使用screen会话来运行Python代码(功能全)

时间有限,在好不容易跑通了代码的前提下,安心去睡觉,起床一看半夜3点自动断开服务器然后代码停下来了啊啊啊啊啊,师兄告诉了我用screen,这就学了记录一下。  

ssh连接上服务器

1.直接在默认状态下安装screen

sudo apt install screen.

2.创建自己的一个screen会话

screen -S mysession
创建一个名为 “mysession” 的新会话。 “mysession” 可改为指定名称。
运行该代码之后,就进入了mysession会话中

3.运行代码

转到我们需要运行的文件的目录
cd /tmp/pycharm_project_880
activate 自己的环境
conda activate Fiona0329
运行代码
python run.py

4. 断开连接(不影响代码运行)

当需要断开连接时,按下以下快捷键:

按下 Ctrl + A + D,这将使 screen 会话在后台继续运行,并返回到本地终端。

使用ctrl+a+d退出screen,然后再×掉cmd窗口了可以关机出去玩耍啦,代码还会继续在服务器中愉快地跑~

5.查看和恢复会话

5.1)首先,需要恢复会话先要知道之前创建会话的名称,即查看screen会话,输入如下命令,可以查看创建的所有screen回话。所有会话的名称由id和name组成。
screen -ls

Detached意思是当前screen没有被打开,相对的是Attached表示当前screen正处在打开状态。 

5.2)恢复会话,输入以下命令即可。
screen -r 113738.myCode
5.3)删除已经运行结束或出现报错代码的会话文件
在会话中运行
exit

或者

screen -S 113778.myCode -X quit
screen -X -S 113778 quit
screen -S myCode -X quit

6.报错

报错内容:There is no screen to be resumed matching.

会话在窗口打开时,但由于网络不稳定,使得xshell直接与服务器断开连接,重新连接上之后,再使用screen -r 命令恢复该会话就报了这个错误;这个错误是因为之前没有好好关闭会话而是突然断开,说明之前的还没关,而一个会话不能恢复两次。

解决办法

6.1)先将该会话离线
screen -d 113778.myCode
6.2)再恢复。
screen -r 113778.myCode
### PyCharm Installation Guide #### Installing PyCharm Community or Professional Edition To install PyCharm, one can start by downloading the desired version from JetBrains' official website. After obtaining the installer file, follow the on-screen instructions to complete the setup process[^1]. For Windows users, this involves running an executable file and following prompts; macOS users will drag the application into their Applications folder. #### Setting Up a Project with Virtual Environment Once installed, launching PyCharm opens up options for creating new projects or opening existing ones. When setting up a project, it is advisable to configure a virtual environment specific to that project within PyCharm settings. This ensures dependencies are managed separately per-project without affecting global Python installations[^3]. ```bash # Example command to create a virtualenv via terminal/command line before configuring in PyCharm python -m venv my_project_env ``` #### Configuring External Tools and Plugins For extended functionality beyond basic coding support—such as integrating external tools like linters or formatters—one may add these through PyCharm’s preferences/setting interface under `Tools` -> `External Tools`. Similarly, enhancing productivity further comes down to installing plugins available directly inside PyCharm's marketplace accessed via `File` -> `Settings` (or `Preferences`) -> `Plugins`. #### Troubleshooting Module Installations In cases where module installation fails despite successful pip operations outside of PyCharm, verifying paths set within PyCharm interpreter configurations becomes crucial. Ensuring correct site-packages directories match those expected when executing scripts helps resolve common issues related to missing packages during runtime[^2][^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值