asdfg_rocks
asdfg_rocks

Reputation: 115

process does not log when run as background

I want to run this command inside a docker container ( ubuntu:18.04 image ):

(cd inse/; sh start.sh > log.txt 2>&1 ;) &

but when I run it, it does not log it to log.txt. When I run it this way:

(cd inse/; sh start.sh > log.txt 2>&1 ;)

It locks the forground (as it should do) and when I kill it I see that the log.txt file is filled with log stuff, which means It works correctly.

Why is this behaviour happening?

The contents of start.sh is:

#!/usr/bin/env sh
. venv/bin/activate;
python3 main.py;

UPDATE:

Actually this command is not the entry point of container and I run it inside another shell but inside a long running container (testing container).

Using with nohup, no success:

(cd inse/; nohup sh start.sh | tee log.txt;) &

I think this problem refers to using () the subshell concept inside sh. It seems it does not let output go anywhere when ran in background.

UPDATE 2:

Even this does not work:

sh -c "cd inse/; sh start.sh > log.txt 2>&1 &"

UPDATE 3:

Not even this:

sh -c "cd inse/; sh start.sh > log.txt 2>&1;" &

Upvotes: 1

Views: 1860

Answers (2)

asdfg_rocks
asdfg_rocks

Reputation: 115

I found what was causing the problem.
It was buffered python output. This problem is caused by python.

I should have used python unbuffered output:

python -u blahblah

Upvotes: 2

Kirti Azad
Kirti Azad

Reputation: 31

Try to use this command and please check that have full access to that folder where log.txt is created.use CMD/RUN step in Dockerfile to run start.sh.

CMD /inse/start.sh > log.txt 2>&1 ;

OR

RUN /inse/start.sh > log.txt 2>&1 ;

Upvotes: 0

Related Questions