Ampersand chạy tập lệnh trong lớp con, vì vậy nếu bạn đăng xuất khỏi lớp vỏ hiện tại, lớp con sẽ bị chấm dứt. Ngoài ra, điều này sẽ không chuyển tiếp bất kỳ đầu ra nào nên chúng tôi cần thực hiện thủ công
Ví dụ
// this does not collect any output, but still print output to stdout [on console]. // it just does not receive ctrl+c python test.py & // this writes output to specified file, does not write anything to console. python test.py > log.txt & // this writes error to specified file, but still print normal output to stdout. python test.py 2> log.txt &
nohup python test.py &
3
nohup python test.py &
3 chạy tập lệnh ở chế độ nền và sẽ không bị hủy khi bạn đăng xuất khỏi trình bao hiện tại. Quá trình sẽ bị hủy khi gửi tín hiệu nohup python test.py &
5 [_______16] hoặc nohup python test.py &
7 PID trực tiếp. Đầu ra sẽ được chuyển tiếp đến nohup python test.py &
8 trong thư mục đang chạy lệnh tôi đang chạy csh. Tôi đã thử đợt Nohup /bin/sh. sh & và một số biến thể khác và nó cũng không hoạt động
Tôi đã sử dụng Nohup thành công trên các máy chủ này hoạt động với các chương trình khác. Tôi thường xuyên sử dụng Nohup. /Chủ yếu. out > /dev/null & và sẽ chạy chương trình trong nhiều ngày
Tôi không chắc liệu tập lệnh Shell của mình không hoạt động hay không vì tôi đang chuyển đầu ra của nhiều lần chạy python sang những thứ khác nhau hay cái gì
Giải quyết vấn đề nhỏ nhất ở đây là một tập lệnh python đơn giản mà tôi muốn chạy bằng Nohup trên linux. Tôi chạy nó bằng cách sau [trên linux]
nohup python test.py &
Lệnh dường như không làm gì cả, không có gì được thêm vào Nohup. ngoài. Nếu tôi chạy nó mà không có & thì đầu ra hiển thị chính xác trên cửa sổ đầu cuối. Tôi đang thiếu gì?
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
Giải pháp tốt nhất
Truyền cho python cờ -u để bỏ bộ đệm thiết bị xuất chuẩn
nohup python -u test.py &
Python sẽ đệm thiết bị xuất chuẩn nếu không. Điều này không yêu cầu thay đổi mã
Từ trang người đàn ông
________số 8Giải pháp liên quan
Python – Cách chuẩn để kiểm tra loại trong Python là gì
Để kiểm tra xem
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
3 có phải là phiên bản của import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4 hay bất kỳ lớp con nào của import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4 hay không, hãy sử dụng isinstance [đây sẽ là cách "chính tắc"]nohup python test.py &
2Để kiểm tra xem loại
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
3 có chính xác là import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4 hay không [loại trừ các phân lớp]nohup python test.py &
5Những điều sau đây cũng hoạt động và có thể hữu ích trong một số trường hợp
nohup python test.py &
6Xem Các hàm tích hợp trong Tham khảo thư viện Python để biết thông tin liên quan
Một lưu ý nữa. trong trường hợp này, nếu bạn đang sử dụng Python 2, bạn có thể thực sự muốn sử dụng
nohup python test.py &
7bởi vì điều này cũng sẽ bắt các chuỗi Unicode [
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
8 không phải là lớp con của import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4; cả import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4 và import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
8 đều là lớp con của nohup python -u test.py &
2]. Lưu ý rằng nohup python -u test.py &
2 không còn tồn tại trong Python 3, nơi có sự phân tách nghiêm ngặt giữa chuỗi [import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
4] và dữ liệu nhị phân [nohup python -u test.py &
5]Ngoài ra,
nohup python -u test.py &
6 chấp nhận một bộ lớp. Điều này sẽ trả về nohup python -u test.py &
7 nếu import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
3 là một thể hiện của bất kỳ phân lớp nào của bất kỳ nohup python -u test.py &
9 nàonohup python test.py &
0Python – Cách kiểm tra phiên bản Python nào đang chạy tập lệnh
Thông tin này có sẵn trong chuỗi
-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
0 trong mô-đun -u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
1nohup python test.py &
3Con người có thể đọc được
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
0Để xử lý thêm, hãy sử dụng
-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
2 hoặc -u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
3import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
1Để đảm bảo tập lệnh chạy với yêu cầu phiên bản tối thiểu của trình thông dịch Python, hãy thêm phần này vào mã của bạn
import time
def test[]:
while[True]:
print "Woke up!"
time.sleep[5]
if __name__ == "__main__":
test[]
2Điều này so sánh thông tin phiên bản chính và phụ. Thêm vi mô [=
-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
4, -u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
5, v.v.] và thậm chí cả cấp độ phát hành [= -u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
6, -u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines[], readlines[] and file-object
iterators ["for line in sys.stdin"] which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline[]" inside a "while 1:" loop.
7, v.v.] vào bộ dữ liệu theo ý muốn của bạn. Tuy nhiên, xin lưu ý rằng tốt hơn hết là bạn nên "lẩn tránh" kiểm tra xem có một tính năng nào đó hay không và nếu không, hãy giải quyết [hoặc cứu trợ]. Đôi khi các tính năng biến mất trong các bản phát hành mới hơn, được thay thế bằng các tính năng khác