user1902346
user1902346

Reputation: 819

Execute python from bash and see its log on terminal

I have the following:

  1. main.py python script which while running writes to main.log.
  2. I am writing a sh file.

I want to execute the python script from sh file and to see in the terminal window all the log which is written to the main.log file while executing.

Currently I am doing it by open a terminal and execute the script and open another terminal and writing

tail -f main.log.

Thanks.

Upvotes: 0

Views: 4586

Answers (2)

clipsett
clipsett

Reputation: 13

I've used the nohup command in the past to essentially disconnect from a process and let it run in the background. The syntax would be something along the lines of nohup <program execution and options> > main.log &

Then you can tail -f main.log to watch it update in real time.

I've had issues with nohup where a .sh either runs infinitely or has a syntax error, so definitely check for correctness before running. You probably won't catch this unless you change the end of the statement to 2>&1 & to get stderr to redirect to stdout, but since you're running a python script, I don't think this would help you.

Lastly, you can kill the process created by nohup by either killing the PID given to you by the command, or using ps -ef | grep <main.log>

Upvotes: 0

DainDwarf
DainDwarf

Reputation: 1669

Simply use & after the command you use for launching your python script to launch it as a background process. Then, you can tail -f in the same terminal.

Upvotes: 1

Related Questions