Easy way of daemonizing in CentOS 5.4

2

5

I know there is a program called upstart that can make it easy to make small daemons. I can't get this program to configure on CentOS. I get all sort of errors concerning pkg-congfig, libnih, and dbus.

I am working on a node.ja application and this is a pain to start and stop all the time, so I want to create a deamon for this which makes it easy to start and stop.

Update 1
I will give a small example of what i need for this project, I hope someone can help with this.

To start the node.js application I have to type in SSH:

# node /path-to-file/filename.js

Now when I execute this the terminal freezez, i have to press CTRL + Z (pc) to get input back.

Now when i changed something in the file I have to reload it again

I need to:

# killall -9 node

This kills all the node applications that are running

Next i have to start the script again

# node /path-to-file/filename.js

I want to just type

# myapp restart

And everything is done. This type of setup would save me lots of time

Update 2
I found a program called monit. This works nice, and automatically starts the application in case of a crash, which is good. It also has a nice web interface which is also handy.

I can type

# monit myapp start(start/stop/restart)

This works fine. There is only one downside, and this is a major downside. When i start the myapp application, it does not display the compile errors node.js throws. So when it fails to start I will not know what the reason is. I have to type the whole '# node /path-to-file/filename.js' again to check the error.

Saif Bechan

Posted 2010-03-30T09:42:46.877

Reputation: 2 835

Answers

10

You don't need anything special to make a daemon, really. Any program in any language can "daemonize" itself. Alternatively, you can daemonize an existing program with a small shell script wrapper (for instance the /etc/init.d program launcher can take care of it).

Typically, a daemon has the following properties :

  • working directory must be /
  • STDIN must be /dev/null
  • STDOUT and STDERR must be either /dev/null or log files
  • the parent ID should be init ( 1 ), easily achieved by forking then letting the parent exit.

Update 1

Monit basically takes care of the gory details I gave here. For the details about STDIN/STDOUT, /dev/null etc. these are basic Unix concepts so I guess you'll need to dig into this sooner or later. See for instance this Unix introduction.

Update 2

A shell script that daemonize a program would look like the following. Note that this will not work for programs that stay in the foreground.

#!/bin/sh

DAEMON=/some/program/to/run
PARAMETERS="parameters to my program"
LOGFILE=/var/log/somefile.log

start() {
    echo -n "starting up $DAEMON"
    RUN=`cd / && $DAEMON $PARAMETERS > $LOGFILE 2>&1`

    if [ "$?" -eq 0 ]; then
        echo "Done."
    else
        echo "FAILED."
    fi
}

stop() {
    killall $DAEMON
}

status() {
    killall -0 $DAEMON

    if [ "$?" -eq 0 ]; then
        echo "Running."
    else
        echo "Not Running."
    fi
}

case "$1" in
    start)
    start
    ;;

    restart)
    stop
    sleep 2
    start
    ;;

    stop)
    stop
    ;;

    status)
    status
    ;;

    *)
    echo "usage : $0 start|restart|stop|status"
    ;;
esac

exit 0

wazoox

Posted 2010-03-30T09:42:46.877

Reputation: 1 285

Ok i am new to linux, so I don't know what all those tings are. Can you maybe give me a little example on how to achieve this. I will edit the question a little and maybe you can help me with this particular type of setup, because i have no idea what stdin, etc are. – Saif Bechan – 2010-03-30T10:32:01.747

Ok so basically I just create a file(myapp), and just add #!/bin/sh to the top of the line. And when I type the filename it will run as a program? – Saif Bechan – 2010-03-30T11:27:51.660

Thank you this will get me started on the program. Thank you for the help. Accepted answer – Saif Bechan – 2010-03-30T12:27:56.320

Good answer, though I would add that the parent process should ignore SIGCHLD in order for init to immediately inherit the orphan process and not leave a zombie in the process table. This is easily done in scripts using the NOHUP command, but like you said, tie up all 3 standard IO as well. – kmarsh – 2010-03-30T12:46:04.753

This works great. I now have full control over the start and stop of the execution. This will save me a huge amount of time. – Saif Bechan – 2010-03-30T12:58:46.403

You're welcome :) you can use this script as /etc/init.d/yourapp and use your application as a full-fledged service from now. Error messages will go to the log file ( /var/log/whatever). – wazoox – 2010-03-30T13:13:50.730

1Simply closing stdin and backgrounding works as well: myprog 1<&- &. Now you have a daemon. – w00t – 2012-03-23T13:36:08.597

@woot, chdir to /root is essential to not prevent unmounting something inadvertently. – wazoox – 2012-03-24T22:01:46.513

3

To start a script in non-interacting session, use nohup (it will detach your process in a standalone term).

To make your script executable, use chmod ugoa+x <script_name>.

The last point, do not use #!/bin/bash or #!/bin/sh because you don't know if resides in /bin; try using #!/usr/bin/env bash (or sh) which forces the process to work under bash (sh) environment. Note that env exists all the time in /usr/bin and has all the environments (BASH, SH, TCSH...) paths registered in it.

CGSMCMLXXV

Posted 2010-03-30T09:42:46.877

Reputation: 31

@slhck you didn't actually edit something, you just wrote down the exact same words. I am going to flag this for moderation attention. – Saif Bechan – 2011-10-03T11:01:07.633

1@Saif slhck changed those words in to monospace code items - so #!/bin/bash instead of #!/bin/bash - and added paragraph divisions. The post looks visually easier to parse, I see nothing wrong with this edit. – DMA57361 – 2011-10-03T11:12:28.427

@Saif As DMA said, that was the intention. If you have a problem with this, please explain yourself on [Meta]… – slhck – 2011-10-03T11:47:06.317

2

If you are trying to daemonize a program that doesn't have a daemon mode then you can use daemonize. There are RPM packages for it in the repoforge repository.

HughE

Posted 2010-03-30T09:42:46.877

Reputation: 211

1

The stdout of a daemon does not go to the terminal. If you simply want to automate restarting your program, I would suggest writing a shell script to perform the exact steps you are performing manually now.

Edit:

Here is a simple example shell script:

#!/bin/sh
do-start-stuff () {
    stuff-to-do
}

do-stop-stuff () {
    stuff-to-do
}

case "$1" in
    start)
        do-start-stuff
        ;;
    stop)
        do-stop-stuff
        ;;
    restart)
        do-stop-stuff
        do-start-stuff
        ;;
esac

Paused until further notice.

Posted 2010-03-30T09:42:46.877

Reputation: 86 075

Can you point me in some direction on how to create a shell script. – Saif Bechan – 2010-03-30T11:24:31.713

If i create a file with the exact steps I am making, how will the script know if i want to start or stop it. Or can i work with if statements inside the file. – Saif Bechan – 2010-03-30T11:28:51.313

Yes, you can have if and other statements. See my edit. – Paused until further notice. – 2010-03-30T11:42:53.277

Thank you for the help I will examine the code and I get the basic idea now. Thank you for the help. +1 for the answer.The answer of wazoox was slightly more detailed, thats why I accepted it answer. – Saif Bechan – 2010-03-30T12:26:49.083

0

if you need to run a single app which outputs to the console, but don't want it to monopolise your terminal use screen, then detach it - type in screen, hit enter, start your app normally, then hit f6 to quit. screen -r goes back to the app

Journeyman Geek

Posted 2010-03-30T09:42:46.877

Reputation: 119 122