0

I'm unsure if this actually is the problem, but let me explain: I have a python script that gets started by a bash script. The bash script's job is done then, but when I grep the ps aux the call is still present.

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 

If I grep for ps aux | grep python I get the python -u /home/user/folder/myscript.py -some Parameters as a result. According to the logfile the python script closed properly. (Code to end the script is within the script itself.) The script gets started every hour and I still see all the calls from the hours before.

Thanks in advance for your help, tips or advice!

3
  • 1
    So apparently your Python script doesn't terminate. We can't tell you why it doesn't without seeing the code of the script. Commented May 22, 2014 at 10:31
  • 1
    Are you running an infinite loop or something. Without knowing what the script does, its hard to diagnose the problem. Commented May 22, 2014 at 10:31
  • I used the answer from gaoithe and made it work for me. So, in my python script I check what time it is and when the hardcoded time is reached the script is stopped - with a cleanup function that uses atexit. Meanwhile the Bash script that started the python script sleeps for the same time and then kills off its child and itself. Tested it and it works.. Not very beautiful, but works. Commented May 23, 2014 at 14:33

1 Answer 1

2

The parent bash script will remain as long as the child (python script) is running. If you start the python script running in background (add & at end of python line) then the parent will exit.

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &

If you examine the process list (e.g. 'ps -elf'). It will show the child (if still running). The child PPID (parent PID) will be 1(root PID) instead of the parent PID because the parent doesn't exist any more.

It could eventually be a problem if your python script never exits. You could make the parent script wait and kill the child, e.g. wait 30 secs and kill child if it is still present:

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &
sleep 30
jobs
kill %1
kill -9 %1
jobs
Sign up to request clarification or add additional context in comments.

4 Comments

ah, the little & at the end.. I'm testing that right now. I'll report back!
it still doesn't work. When I kill it with the sleep command as you suggested it works of course, but I want to kill the script from within itself. I just want to make sure the parent dies along or if possible directly after it (the bash script/parent) started the script.
If you want the python script to end itself then that is a matter for the python script. If the python script ends itself then the parent will end immediately after. So you don't need any of the sleep and kill stuff.
What do you mean 'kill the script from within itself'? For that we need to be talking about the python script contents and behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.