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 that periodically generates a png image, and puts it on a server that I can access through a browser. The image displays the progress of a job I am running on the server. I'm using matplotlib to generate the images.

The python script goes to sleep after generating each image. After an hour, it wakes up, generates a new image, and then goes back to sleep.

I start the script over an ssh session. When I log off, the script gets killed. I've read the other threads covering this problem, but the proposed solutions do not work. The script is killed after I log off, even if I run it with

nohup script.py &> job-monitor.log &

or if I run it like this

script.py
[Ctrl-Z]
disown -h %1
bg 1

The strange thing is, when I log off and log back in, the script is no longer running, and job-monitor.log is empty.

What could be happening, and how do I get the script to continue running after I log off?

Additional information: I think the problem has to do with the plotting library I'm using, matplotlib. I think it may require an X session to produce a png, even though the graphic is just being saved to disk.

share|improve this question
    
Take a look at this thread: unix.stackexchange.com/questions/4004/… –  slm Sep 22 '13 at 13:24
    
Also if you search through these Q's on the site you'll find additional examples and guidance: unix.stackexchange.com/search?tab=votes&q=nohup –  slm Sep 22 '13 at 13:24
    
Is your job-monitor.log not empty when you do a nohup and then empty after you logout and back in? Does script.py finish normally (with a filled log file) if you let it run long enough without login out? Apart from finding the reason for why this happens, you can try tmux or screen –  Anthon Sep 22 '13 at 13:30
1  
@slm: This question is not a duplicate of How can I close a terminal without killing the command running in it?. The accepted answer to that question gives two methods that I explicitly stated in my question do not work in this case. Users with the same problem as I have should not be redirected to an answer that doesn't solve their problem. –  Thucydides411 Sep 22 '13 at 17:58
    
Based on the information you've provided that Q&A is the most related and has all the information regarding the topic, from what I know of Unix & Linux. I understand your frustration, but we need more info to go on in order to assist you more than what's here. –  slm Sep 22 '13 at 18:11

4 Answers 4

up vote 3 down vote accepted

If the problem is with matplotlib (that is, your script never provides an answer if you stay connected, or it works because ssh forwards your Xwindow connection), you have to put in your matplotlibrc file:

backend : AGG

This way, the script doesn't need Xwindow to work.

share|improve this answer
    
Importing matplotlib first, and invoking matplotlib.use('Agg') before importing matplotlib.pyplot also works. –  Thucydides411 Sep 23 '13 at 14:54

Instead of IO/redirecting use tmux or screen command for session monitoring.

share|improve this answer

May I humbly suggest that you throw away the time.sleep() part from your script and use either cron or at for periodic and/or delayed invocations?

This simplifies your code, safely removes dependencies on ssh session detachment, and separates the concerns of image generation and scheduling.

Regarding the script dying: I suspect that the script after nohup might not have write access to the log file, either because it lacks write access to the directory where you start it, or because an existing log file is owned by e.g. root and thus inaccessible.

I don't know if disowning a job and then trying to call bg against it should work. I always do the opposite: foo & disown %1; it works exactly in circumstances like yours.

share|improve this answer

Try redirecting STDIN as well. It's possible the script is reading from stdin, so when you close the shell, stdin goes away, and it dies.

script.py </dev/null &>/dev/null &
disown
share|improve this answer

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.