supernoob
Posts: 8
Joined: Tue Jun 19, 2012 4:25 pm

Python process is killed

Tue Jun 17, 2014 7:53 pm

Hi guys,
strange behaviours here... I'm going crazy so, please, help.

I have three python process a.py , b.py, c.py
All of these continue to run at the same time in background (sudo python a_b_c.py & ) only if ssh session is up.
When I close the session, at least one of these process ends.

I try to understand if process is killed by OS but "var/log/kern.log" does not help.

This is the TOP report...
10193 root 20 0 12976 7200 2804 R 49.4 1.6 5:50.50 python
10377 root 20 0 60720 5884 2888 R 24.2 1.3 2:06.24 python
10456 root 20 0 12484 6764 2984 R 24.2 1.5 1:52.00 python

Someone could help me please ?

Many, many thanks.

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python process is killed

Tue Jun 17, 2014 7:57 pm

man nohup

supernoob
Posts: 8
Joined: Tue Jun 19, 2012 4:25 pm

Re: Python process is killed

Wed Jun 18, 2014 5:16 am

It works!

Why:

Code: Select all

sudo  python a.py &
sudo  python b.py &
sudo  python c.py &
does not work ?

Is there a limit about background process runned by session?

Thanks!

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python process is killed

Wed Jun 18, 2014 7:54 am

supernoob wrote: ...
Is there a limit about background process runned by session?
...
I don't think Linux (or *nix in general) impose any limits on the number of processes. The amount of available memory/swap is probably the practical limit.

User avatar
rpdom
Posts: 17274
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python process is killed

Wed Jun 18, 2014 8:14 am

When you end a session all processes started in that session are sent the "HUP" signal (HangUp, which refers to the old days of dial-up modems), which will usually end the processes.

Some ways around this are to trap the HUP signal in the process and ignore it, or run the process using "nohup" which blocks the HUP signal from reaching the process, or start a "screen" session which keeps your login session active when you disconnect (but not if you log out) you can later reconnect to the screen session and see what has happened while you were away. Some people prefer tmux to screen.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Python process is killed

Wed Jun 18, 2014 10:00 am

joan wrote:I don't think Linux (or *nix in general) impose any limits on the number of processes. The amount of available memory/swap is probably the practical limit.
It depends on the user. The user pi on my Raspberry Pi is limited to 1404 processes.

Code: Select all

pi@rpi ~ $ ulimit -u
1404
On my Raspberry Pi root has a soft limit of 1404 processes. But root can change that to unlimited.

Code: Select all

root@rpi:~# ulimit -u
1404
root@rpi:~# ulimit -u unlimited
root@rpi:~# ulimit -u
unlimited
Last edited by AndyD on Wed Jun 18, 2014 11:21 am, edited 1 time in total.

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python process is killed

Wed Jun 18, 2014 10:33 am

Thanks.

My laptop ((3GB) reports 23978 . My soft float Pi (256MB) reports 1785. My hard float Pi (512MB) reports 3434.

It seems like free / ulimit -u tends to 128.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: Python process is killed

Wed Jun 18, 2014 1:17 pm

rpdom wrote:When you end a session all processes started in that session are sent the "HUP" signal (HangUp, which refers to the old days of dial-up modems), which will usually end the processes.
If you end a session cleanly (with "exit", "logout", or Ctrl+D), and you have not explicitly set "shopt huponexit", then all background jobs are left running.

If you end a session uncleanly (by hangup or window close) the kernel sends a SIGHUP to the foreground process group only. bash does attempt to forward HUP signals to background jobs, but most other shells do not. Even bash often fails, because there is no guarantee it receives the HUP signal before it notices the EOF condition on the terminal.

In other words, you should not rely on background jobs being HUPped, because usually they are not.

The nohup command does two different things. It sets SIGHUP to be ignored at process start (equivalent to "trap '' HUP" in the shell), and it disconnects stdin from the terminal, and redirects stdout and stderr to the file "nohup.out" unless they are both already redirected.

If you just want to ensure that a background job is not killed by the shell, "disown %1" is arguably better than nohup. If you want to redirect output, do so manually so you can choose a sensible file name.

Return to “Python”