0

OS: ubuntu 20.0 I write a simple python with print

import time
a = 0
while 1:
    a +=1
    print(a)
    time.sleep(1) 

and I run command

nohup python3 test2.py > 3.log 2>&1 &

then next

cat 3.log 

only got

nohup: ignoring input

if I remove last & run

nohup python3 test2.py > 3.log 2>&1 

get 3.log output

1
2
3
4

but nohup will be kill after terminor close .how can I get output with &

django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$ nohup python3 test2.py > 3.log 2>&1
^C
django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$ cat 3.log
nohup: ignoring input
1
2
3
4
5
6
7
8
Traceback (most recent call last):
  File "test2.py", line 6, in <module>
    time.sleep(1)
KeyboardInterrupt
django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$ ^C
django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$ nohup python3 test2.py > 3.log 2>&1 &
[3] 83030
django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$ cat 3.log
nohup: ignoring input
django@django-s-1vcpu-1gb-sgp1-01:~/swap_hb$

    

hardy
  • 1
  • 1
  • Why are you using `nohup`? Bash has a built-in `disown -h` that does the actual "no HUP" part, and the only other things it does is redirecting stdin from `/dev/null` and stdout and stderr to `nohup.out` if they aren't already going to a file. – Charles Duffy Sep 23 '20 at 03:23
  • That said, redirect ` – Charles Duffy Sep 23 '20 at 03:24
  • 1
    ...btw, for the future, [unix.se] is more appropriate for questions about using command-line tools; this is the right place for questions that are specific to developing software (in the context of shells, writing scripts). – Charles Duffy Sep 23 '20 at 03:25
  • You could add `-u` to unbuffer `sys.stdout` like: `nohup python3 -u test2.py > 3.log 2>&1 &` – jia Jimmy Sep 23 '20 at 03:36
  • nohup python3 -u test2.py > 3.log 2>&1 is work thanks – hardy Sep 23 '20 at 03:40

0 Answers0