ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.