0

I have a problem with deamonizing of my Python application.

As basic daemon on Python, I have used this example. It works, but only for some simple actions. But when i've tried to deamonize my app - nothing was happen.

This is my crawler code, (it's too big for posting it on SO) and Recursion()._recurse(url, 1) - is the instance what starts crawler (it works independently).

How can I deamonize my application? Thanks to all replies in advance!

Max L
  • 1,453
  • 2
  • 17
  • 32
  • If you have large chunks of code like in your crawler code, please try to find a minimal working example of your problem. In this case start by removing all crawling code and just log something every few seconds. You can use `time.sleep(seconds)` to hold process execution similar to the IO wait of network interaction. Having less code to look through makes it easier to answer and more useful for others having the problem as you have. – Perseids Aug 15 '13 at 12:36
  • i've tried to log some message every second, and it was working. For now, I don't understand, why it doesn't working for all crawler... – Max L Aug 15 '13 at 12:39
  • Your code does not include the attempt to demonize your script with the Daemon class you are referring to. Please include that. Also specify what "nothing was happen" means exactly. Does your process exit immediately or does it continue to exist but logs nothing and you return to shell immediately or does it just hang? – Perseids Aug 15 '13 at 12:49
  • When I start the daemon, it must create PID file in /tmp/ dir. So in this case, when I want to start Crawler, inserting `Recursion()._recurse(url, 1)` in run() function of deamon - it exit immediately, and there is no any PID files of my app – Max L Aug 15 '13 at 12:56
  • Sorry, but there are too many possible reasons. Try to slowly expand some code that properly works with `Daemon` until something unexpected happens and try to find the smallest change that breaks functionality. Then include the experiment and its result in the question. – Perseids Aug 15 '13 at 13:17
  • ok, maybe you right. Will try that advice. Thank you! – Max L Aug 15 '13 at 13:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35510/discussion-between-yakudza-m-and-perseids) – Max L Aug 15 '13 at 13:28

2 Answers2

1

If you just want to run the script in the backround you can use nohup, just add an '&' to the end of the script. For example: python myscript.py &. However this is different from daemonizing

Another option would be to create a cron job

Community
  • 1
  • 1
Martyn
  • 806
  • 1
  • 8
  • 20
  • I've tried that, but then process can be closed, when I close terminal. I need only start-restart-stop actions...(( – Max L Aug 15 '13 at 12:07
1

Note: Is not actually daemonizing, but is an approach I've had used and had made me happy when I've needed it

If you're on Unix you can use screen command. I've used that for a while to "daemonize" Django's Server with a lot of success.

Here are some usage examples:

  1. In Unix, what is screen, and how do I use it?
  2. Screen Command Examples: Get Control of Linux / Unix Terminal
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • it works with start-restart-stop actions? I use FreeBSD and Python 2.6.8 – Max L Aug 15 '13 at 12:17
  • It basically creates a new terminal, where you can start-restart-stop every command (like a normal terminal) and later _detach_ from it. So it will keep running in the background as long as the computer is turned on. – Paulo Bu Aug 15 '13 at 12:21
  • Thanks, I will try it now. PS: it's not me placed you downvote – Max L Aug 15 '13 at 12:23
  • I also didn't downvote you, but - as much as I like screen - yakudza_m asked for a way to implement a daemon, not for a way to put any program in the background. – Perseids Aug 15 '13 at 13:08
  • 1
    @Perseids I see that, just made this suggestion because when I needed to _daemonize_ something time ago, just learned that `screen` will make my life easier according to **the given task**. Sometimes one doesn't know what it needs until it finds it, that's why I suggested this approach and made a notice above the answer. – Paulo Bu Aug 15 '13 at 17:18