[Linux] Display the output of the program to the screen and write it to the log file at the same time

Output Log Dafa

 nohup python -u my_test.py 2>&1 | tee -a me_test.log &
  • Nohup is placed at the beginning of the command, which means no hang up (no hang up), that is, when the terminal is closed or an account is exited, the process will continue to run, and it is generally used together with the & symbol. Such as nohup command &

  • After adding the -u (unbuffered) parameter, it means that when python is executed, its standard output will be forced to print directly to the screen without caching, just like standard error . If you do not add u, you may find that the content of print cannot be displayed on the screen.

  • 2>&1 means to redirect errors to standard output. 2 means standard error, 1 means standard output. The & here is equivalent to an escape character. If you do not add &, it will output the standard error 2 to the file named 1.

  • tee -a means to append at the end of the file without overwriting the original content.

  • & is placed at the end of the command, which means running in the background, preventing the terminal from being occupied by a certain process all the time, so that the terminal can perform other tasks.

Continually updated…

Guess you like

Origin blog.csdn.net/weixin_43693967/article/details/129866980