Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detach python script run from Pycharm so it keeps appending to files?

I am working on a remote system and I use Pycharm remote window to edit and run my scripts.

I login using

ssh -Y myName@myMachine

Then I run Pycharm from terminal.

I want to run my scripts from Pycharm in such a way, that if I close it (and perhaps even logout from ssh session), the processes will still run.

I have tried to exit Pycharm using option "Detach without terminating process". This results in the python process showing on the list of:

ps -all

however, it stops writing to a file. When pycharm is open the process normally writes to a file every few seconds. When detached from pycharm it shows on the list of processes (after logout and login again it shows in ps -x with unknown tty), however it stops working in the sense that it no longer appends any output to files that it normally should.

What can be causing it? How can I fix this?

like image 964
Natalia Zoń Avatar asked Apr 15 '14 20:04

Natalia Zoń


People also ask

How do I stop a script from PyCharm?

Stop a programon the toolbar, or press Ctrl+F2 .

How do I save Python program in PyCharm?

Press Ctrl+S or select File | Save All from the main menu.


1 Answers

There are several approaches:

  1. Use terminal emulators as tmux and screen:

    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.

      After installing tmux, start the tmux session using command:

      $ tmux

      Now, start your task or job. Then safely detach from the tmux session without exiting the remote jobs by pressing "CTRL-b" followed by "d". This will detach your tmux session but will leave you’re doing in that session running in the background. That means all remotes will be running even if you’re disconnected from the session.

      To list the available sessions, run:

      $ tmux ls

      You can re-attach to the tmux session using the respective session ID as shown below:

      $ tmux attach -t < session ID >

      For more details, refer man pages.

      $ man tmux

    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.

      After installing screen on your remote systems, start the screen session:

      $ screen

      The screen session has been started now. Now run whatever job or task you wanted to do on your remote system, Then you can exit from the screen session by pressing “Ctrl-A” followed by “d“, After detaching from the screen session, you can log out from the remote system. The remote job will keep running in the server.

      To list the screen sessions, run:

      $ screen -ls

      You can re-attach to the screen session using the respective session ID as shown below:

      $ screen -r < session ID >

      For more details, refer man pages.

      $ man screen

  2. Use detached commands executed in the background ([read also][6]):

    1. nohup - stands for No hangup, is yet another command line utility to help you run Linux commands even after you’re disconnected from the SSH sessions.

      The usage is absolutely easy. After logging into your remote system, all you have to do is:

      $ nohup < command > &

      Now, you can exit from SSH session. The remote job will keep running.

      To list the running jobs, run:

      $ jobs -l

      For more details, refer man pages.

      $ man nohup

    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.

      After logging into your remote system, run you command with "&":

      $ < command > &

      Then list the running jobs, using:

      $ jobs -l

      Then run disown with the process ID as shown below:

      $ disown -h < PID >

      You can now disconnect from the server

      For more details, refer man pages.

      $ man nohup

    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.

      After logging into your remote system, run:

      $ setsid < command >

      For more details, refer man pages.

      $ man nohup

like image 129
Yogev Neumann Avatar answered Oct 16 '22 03:10

Yogev Neumann