3

I have a python script which seems to be failing to continue running its while True: ... sleep() loop

my code is at http://github.com/xdaimon/xdaimonConky

What happens is I autostart python on system boot by running...

start.sh

#! /bin/bash
sleep 120
cd ${0%/*}
conky -d -c ~/.conkyrc2
python profitability.py &

cd ${0%/*} brings me to the correct directory where I eventually start profitability.py

profitability.py eventually runs

while True:
    ....
    time.sleep(2000)

This works out to be about 33.3 minutes. This python script seems to run correctly as long as my screen does not lock. After a screen lock my script ceases to run, although there is still a python process.

I'm writing this to be at least somewhat portable to other linux computers and can't have disabling screen lock or keeping a terminal open for python as prerequisites :)

There is a lot that I do not understand I am sure, I'm open to many different options here. Any help is appreciated.


I ran this script in the terminal until it quit. I had a urllib error due to poor internet connection. I wrapped urllib2 piece of code in a try: except: statement, which fixed this issue. Although this may have been the cause for my script to fail I have confirmed through much testing that my script still fails if I close my terminal and lock my screen.

I have implemented the code found at a url posted below, which works awesome while I use my computer but if I decide to lock and walk away scheduled events do not happen. http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

I'm using ubuntu GNOME 13.10 with gnome 3.

  • 1
    Have you considered using nohup? See man nohup. – John1024 Jan 9 '14 at 19:43
  • I just read the doc. Thanks, I'm running a final test to weed out the screen lock issue, as I have screen lock and "turn off screen after n minutes of inactivity" turned off. If the script fails then maybe its not the screen lock... After that I'll try the nohup to see how it handles my python script. – xdaimon Jan 9 '14 at 19:55
  • what "scheduled events" are you referring to? In your Python script or generally? Is your computer suspending or hibernating? – Matt Jan 11 '14 at 20:48
1

For your second question, you can use double forking magic to detach child process from parent, resulting in an orphan and unstoppable process (unless killed). It's also called daemonizing. I guess a daemon might be able to solve your problems in your second question.

see more at: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

  • 1
    There are Python libraries that can do the daemon. daemonize python-daemon – Matt Jan 11 '14 at 20:42
  • I'm using python code to daemonize already. The process becomes a child of init, not the top level init but an init that is a child of the gnome display manager. Even screen becomes a child of this same init. This must be a well kept gnome programming secret – xdaimon Jan 14 '14 at 5:06
  • my reddit thread is interesting. the pstree command revealed where my process's have been going when i background and do different forks. everything is a child of gnome-session. how does gnome session handle its process at lock? more specifically the python process and shell scripts, as I've tried implementing my python code in shell (the sleep timing) to no avail. I went to ubuntu-gnome IRC to see if anybody had a response for me. One person said my process's shouldnt be killd by gdm. Well he is right, but my process's arnt being killed they are simply not executing. like being paused – xdaimon Jan 14 '14 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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