Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a python script which I would like to run in the background. So I do this:

$ nohup python script.py &

If I am frequently making changes to the script, I need to terminate the process and run the script again.

$ kill -9 <pid_of_bg_process>
$ nohup python script.py &

Is is possible to reload the process without terminating it?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Short answer - no.

Long answer: You are actually calling a python interpreter. That interpreter loads the script.py and parses it.

If you change the script it has to reload the file and start from the beginning, since the interpreter has no way to know which part was changed.

Now if your goal is to simply signal python to reload/restart the script, you can wrap it into a shell-script:

#!/bin/sh
LINE="python script.py"
stop()
{
  pkill -f "$LINE"
}
clean()
{
  stop
  exit 0
}
trap stop 1
trap clean 9 15
while true
  do
    $LINE &
    wait
done

You can now start that shell-script (in background, with nohup, if you like). If you send a HUP signal to it, it will restart your python process.

If you kill the wrapper process the python script will terminate, too.

I did not test my script - but the idea should be clear.

share|improve this answer
    
I had also planned to create a wrapper script too, since I had a feeling that what I asked might not be possible. But my thought was vague and your script puts me in a good direction. Thank you so much. –  Nanda Apr 27 '12 at 0:43
add comment

you can use kill -USR1 PID or pidof script.py also -9 is brutal to terminate the process , try -15 for graceful exit .

share|improve this answer
    
I thought 9 was for SIGTERM and I was wrong. Thank you for pointing it out. 15 was what I intended to use as well. –  Nanda Apr 27 '12 at 0:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.