LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2012, 12:58 AM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
making program daemon


how can I make my program a daemon..
Actually i have a program whose binary "./Update" i want to execute as daemon..
Any idea!
please post in here with simple coading mechanism.
 
Old 07-31-2012, 01:22 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

basically you need to detach from the console where the program is launched. This usually means forking twice and redirecting stdin/stdout/stderr, changing the current directory to /, set umask to 0... I've seen ready made tools/recipes for doing this in a number of languages including C, python and even shell script! What language is your code written in?

Evo2.
 
Old 07-31-2012, 02:43 AM   #3
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
it's in C...
can you provide me one of the sample in c.
 
Old 07-31-2012, 03:15 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

my search engine returned the following tutorial:

http://www.systhread.net/texts/200506cdaemon1.php

Part 2 of the tutorial shows what to do with stdin/stdout/stderr.

Evo2.
 
Old 07-31-2012, 04:02 AM   #5
okcomputer44
Member
 
Registered: Jun 2008
Location: /home/laz
Distribution: CentOS/Debian
Posts: 246

Rep: Reputation: 53
Hi,

Quote:
nohup COMMAND &
The COMMAND will be sent to the background.
If you want to stop it, you have to use
Quote:
kill PROCESSID
Man page about nohup:

Quote:
NAME
nohup - run a command immune to hangups, with output to a non-tty

SYNOPSIS
nohup COMMAND [ARG]...
nohup OPTION

DESCRIPTION
Run COMMAND, ignoring hangup signals.

--help display this help and exit

--version
output version information and exit

If standard input is a terminal, redirect it from /dev/null. If standard output is a
terminal, append output to ‘nohup.out’ if possible, ‘$HOME/nohup.out’ otherwise. If
standard error is a terminal, redirect it to standard output. To save output to FILE,
use ‘nohup COMMAND > FILE’.

NOTE: your shell may have its own version of nohup, which usually supersedes the version
described here. Please refer to your shell’s documentation for details about the options
it supports.
I hope it helps.

Laz
 
Old 07-31-2012, 04:34 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Actual (if minimal) example script:

Code:
#!/bin/sh

trap '' SIGHUP

cd /home/projects/hol
exec >>log/f_giro.log 2>&1

bin/f_giro &

echo "$!">/var/run/f_giro.pid
 
Old 07-31-2012, 08:08 AM   #7
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
The link was really Awesome...
within those two pages it described all with good understanding..
Thanks a lot
 
Old 07-31-2012, 07:05 PM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

since others have made suggestions on how to do this with a shell script wrapper instead of actually coding your program as a daemon, I thought it worthwhile posting the shell script wrapper I have used in the past for daemonizing programs. This script also includes start/stop/restart mechanisms.
Credit for the first three functions goes to http://blog.n01se.net/?p=145
Code:
#!/bin/bash
redirect-std() {
    [[ -t 0 ]] && exec </dev/null
    [[ -t 1 ]] && exec >/dev/null
    [[ -t 2 ]] && exec 2>/dev/null
}
 
# close all non-std* fds
close-fds() {
    eval exec {3..255}\>\&-
}
 
# full daemonization of external command with setsid
daemonize() {
    (                   # 1. fork
        redirect-std    # 2.1. redirect stdin/stdout/stderr before setsid
        cd /            # 3. ensure cwd isn't a mounted fs
        # umask 0       # 4. umask (leave this to caller)
        close-fds       # 5. close unneeded fds
        exec setsid "$@"
    ) &
}
###########################################################################

DAEMON="someprogram"
EXE="/path/to/somwhere/${DAEMON}"
KILL="/bin/kill"
if [ ! -x $EXE ] ; then
    echo "$EXE not found"
    exit 1
fi

SELF="$(basename $0)"
USER="$(whoami)"
LOCKFILE="/tmp/${DAEMON}-${USER}.lock"

dostart() {
    echo -n "Starting $DAEMON ... " 2>&1

    if [ -e ${LOCKFILE} ] ; then
        echo "Failed."
        echo "Lockfile ${LOCKFILE} exists." 2>&1
        echo "Appears ${DAEMON} already running. To restart try:" 2>&1
        echo "${SELF} restart" 2>&1
        echo "Failing that, manually kill $DAEMON and remove ${LOCKFILE}" 2>&1
        return
    fi

    daemonize ${EXE}
    PID="$!"
    echo ${PID} > ${LOCKFILE}
    echo "Ok." 2>&1
}

dostop() {
    echo -n "Stopping ${DAEMON} ... " 2>&1
    if [ ! -f ${LOCKFILE} ] ; then
        echo "${DAEMON} appears not to be running." 2>&1
        return
    fi
    PID=$(cat ${LOCKFILE})
    NAME_OF_PID=$(ps --pid ${PID} -o comm h)
    if [ "x${NAME_OF_PID}" != "x${DAEMON}" ] ; then
        echo "" 2>&1
        echo "Can't find $DAEMON (pid $PID)." 2>&1
        echo "Removing lockfile: ${LOCKFILE}" 2>&1
        rm -f ${LOCKFILE}
        return
    fi

    # Kill the daemon
    echo -n "(kill $PID) ... " 2>&1
    $KILL $PID
    if [ "$?" != "0" ] ; then
        echo "" 2>&1
        echo "Failed to kill $DAEMON (pid $PID)" 2>&1
    else
        echo "Ok." 2>&1
        echo -n "Removing lock file ... " 2>&1
        rm -f ${LOCKFILE}
        echo "Ok." 2>&1
    fi
}

dorestart() {
    dostop
    dostart
}

case "$1" in
    start)
        dostart
        ;;
    stop)
        dostop
        ;;
    restart|reload|force-reload)
        dorestart
        ;;
    *)
        echo "Usage: $SELF {start|stop|reload|force-reload|restart}" 2>&1
        exit 1
esac
You just need to edit
Code:
DAEMON="someprogram"
EXE="/path/to/somwhere/${DAEMON}"
as desired.

There are places where it could us a bit of a cleanup (eg the dostop() function could probably do with a rewrite using pkill), but it should work as is.

HTH,

EVo2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How create daemon from program? monolit Linux - Software 3 09-13-2009 01:33 PM
java program as daemon dsubbarao Linux - Newbie 9 03-01-2007 11:17 PM
how to run a program as a daemon? iclinux Linux - Newbie 2 04-20-2005 12:11 AM
Need help making FTP daemon accessible through firewall Electrode Linux - Networking 1 08-12-2003 05:35 PM
Making a Daemon User Road Linux - General 4 07-10-2002 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration