# Easy way of daemonizing in CentOS 5.4

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.

-

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

-
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 Mar 30 '10 at 10:32
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 Mar 30 '10 at 11:27
Thank you this will get me started on the program. Thank you for the help. Accepted answer –  Saif Bechan Mar 30 '10 at 12:27
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 Mar 30 '10 at 12:46
Simply closing stdin and backgrounding works as well: myprog 1<&- &. Now you have a daemon. –  w00t Mar 23 '12 at 13:36

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.

-
@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 Oct 3 '11 at 11:01
@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 Oct 3 '11 at 11:12
@Saif As DMA said, that was the intention. If you have a problem with this, please explain yourself on Meta Super User –  slhck Oct 3 '11 at 11:47

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.

-

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

-
Can you point me in some direction on how to create a shell script. –  Saif Bechan Mar 30 '10 at 11:24
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 Mar 30 '10 at 11:28
Yes, you can have if and other statements. See my edit. –  Dennis Williamson Mar 30 '10 at 11:42
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 Mar 30 '10 at 12:26

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

-
Thank you i will try this. –  Saif Bechan Mar 30 '10 at 11:25