jgoat
Posts: 24
Joined: Wed Oct 02, 2013 12:11 pm

What is the easiest way to create a daemon? [obsolete]

Sat Oct 05, 2013 8:29 pm

Hey y'all, I posted about the relay a couple days back - I've got my device working on the breadboard as a command line app that simple activates the octocoupler. Now, I want to wrap that little bit of functionality into a daemon.

Short version of the question: What is the simplest way to create a daemon in the Pi?

Relevant info: I mainly have used Python up to now, but trying to make my Python script in my last project became a nightmare. It occurs to me, though, that I can simply "black box" the Python code and write the daemon in PHP, or whatever other language "daemonizes" more easily than Python. But then I just figured, surely someone has made some Python code into a daemon on the Pi by now, so... I'm not sure which order of ignorance I'm at, but it's annoying.

Events on which I want the daemon to activate its pin:

-check the internet connection every five minutes - if no connection found, activate.
-eventually I'm going to add a GSM shield to the Pi, and have it send out a text message before activating the relay, to give me the option to shut off the daemon if I know that something else is going on.

-listen on a port (or a http server, whatever is most easy/efficient - or is that the same thing?) for a signal to do the same.

-listen on another IO pin (a switch on my desk) to do the same.

So, to recap: I want to make a daemon that manages turning on Pin 17, that I can activate from elsewhere on the network, and that automatically monitors my internet connection. I know Python and PHP, but am confident that I can learn any high-level language you can throw at me, if it results in my being able to daemonize my little snippet of Python code, which so far as I know is the only way to work with the GPIO.

cheers!

WickedPenguin
Posts: 33
Joined: Wed Sep 04, 2013 7:11 pm

Re: What is the easiest way to create a daemon?

Sun Oct 06, 2013 4:10 am

Assuming your daemon has some way of continually running (some event loop, twisted, whatever), you can try to use upstart.

Here's an example upstart config for a hypothetical Python service:

description "My service"
author "Some Dude <[email protected]>"

start on runlevel [234]
stop on runlevel [0156]

chdir /some/dir
exec /some/dir/script.py
respawn

If you save this as script.conf to /etc/init you simple do a one-time

$ sudo initctl reload-configuration
$ sudo start script

You can stop it with stop script. What the above upstart conf says is to start this service on reboots and also restart it if it dies.

As for signal handling - your process should naturally respond to SIGTERM. By default this should be handled unless you've specifically installed your own signal handler.


Source: http://stackoverflow.com/questions/4705 ... ice-daemon

jgoat
Posts: 24
Joined: Wed Oct 02, 2013 12:11 pm

Re: What is the easiest way to create a daemon?

Fri Oct 11, 2013 4:18 pm

Cheers, I'll give this a go. to clarify, is upstart installed on raspbian by default, or do I need to install it? I'm mostly 2nd order ignorant now - I've learned a lot about a lot of things, but I don't have an overall framework yet for all this stuff.

By the way, here's the state of this project:

https://github.com/jimmyyeo/Yeodem

soroushjp
Posts: 1
Joined: Wed Dec 04, 2019 7:37 am

Re: What is the easiest way to create a daemon?

Wed Dec 04, 2019 7:53 am

**PLEASE READ**

I highly recommend not following this advice, and saving yourself some time. I just wasted my last hour trying to use this method to setup a daemon my RPi 4.

Upstart has never been the default init system in Raspbian/Debian and it is not recommended to move your RPi to using it. Look into ways of doing this in SystemD (example) instead.

hippy
Posts: 6281
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: What is the easiest way to create a daemon?

Wed Dec 04, 2019 5:53 pm

Can't you just run your Python code as a background task by adding an & to the end of the command which launches it ...

python /path/myfile.py &

Put that in /etc/rc.local and it will run whenever your Pi boots.

User avatar
rpdom
Posts: 15600
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: What is the easiest way to create a daemon?

Wed Dec 04, 2019 6:27 pm

soroushjp wrote:
Wed Dec 04, 2019 7:53 am
**PLEASE READ**

I highly recommend not following this advice, and saving yourself some time. I just wasted my last hour trying to use this method to setup a daemon my RPi 4.

Upstart has never been the default init system in Raspbian/Debian and it is not recommended to move your RPi to using it. Look into ways of doing this in SystemD (example) instead.
Well, if you will follow a guide that is over six years old and outdated....

User avatar
thagrol
Posts: 2001
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: What is the easiest way to create a daemon?

Wed Dec 04, 2019 7:15 pm

hippy wrote:
Wed Dec 04, 2019 5:53 pm
Can't you just run your Python code as a background task by adding an & to the end of the command which launches it ...

python /path/myfile.py &

Put that in /etc/rc.local and it will run whenever your Pi boots.
Technically that ain't a daemon. True daemons do things like dropping privileges, forking, detatching from terminal sessions, etc (see, for example, here: http://www.netzmafia.de/skripten/unix/l ... howto.html)

That said, you'll probably get away with running a python script from rc.local subject to all the usual caveats about not having a full shell environment, not having the working directory you code expects, correct permissions on files used by your code, etc. And you're happy with the security implications of running code as root.

I've used this article in the past with python 2: http://code.activestate.com/recipes/278 ... ython-way/
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Return to “Beginners”