Linux – set any program to start as daemon/service

This post explains how to add any program to start a service/daemon on linux (as long as the program supports this). It was created while attempting to start TeamViewer as a daemon while my home server was booting up.

daemon startup

To start any program as a daemon, a link to the daemon script should be placed in /etc/rc*.d/ where * is the number of the boot mode the machine is set to start. In order to find what is your current default mode, please check in file /etc/inittab. Usually these types of daemon scripts are placed in /etc/init.d/ directory.

The below script is a generic script that will start any application/service defined by the user. Simply copy the code below into a file, e.g. /etc/init.d/newdaemon and then create a symbolic link to it from within the directory your machine is booting e.g. # ln -s /etc/init.d/newdaemon /etc/rc3.d/S90daemon

Note that this script was only tested using bash shell. Feel free to use and modify as you like. To test the script is working you can use it like this:

# /etc/rc3.d/S90daemon start

# /etc/rc3.d/S90daemon status

# /etc/rc3.d/S90daemon stop

# /etc/rc3.d/S90daemon status

 


#!/bin/bash
#
#Generic daemon script
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
USER=root
PROG=sshd			# MODIFY THIS
PATHPROG=/usr/sbin/$PROG	# MODIFY THIS

start () {
        echo -n $PROG" starting up "
        $PATHPROG & > /dev/null 2> /dev/null
        sleep 5
	if [ `ps -ef | grep $PROG | grep -v grep | grep -v bash | wc -l` -gt 0 ]
	then
        	success $""
	else
		failure $""
	fi
	echo ""
}

status() {
	if [ `ps -ef | grep $PROG | grep -v grep | grep -v bash | wc -l` -gt 0 ]
	then
		echo -n $PROG" is running..."
		echo ""
	else
		echo -n $PROG" is stopped"
		echo ""
	fi
}

stop () {
        echo -n $PROG" shutting down"
        > /tmp/pid
	chmod 777 /tmp/pid > /dev/null 2> /dev/null
        ps -ef | awk '!/bash/' | awk '/'$PROG'/ {system("echo " $2 " >> /tmp/pid")}'
        cat /tmp/pid | awk '{system("kill -9 " $1 " > /dev/null 2> /dev/null") }'

        sleep 5
        if [ `ps -ef | grep $PROG | grep -v grep | grep -v bash | wc -l` -gt 0 ]
        then
                failure $""
        else
                success $""
        fi
	echo ""
}

case "$1" in
start)
start
;;
status)
status
;;
stop)
stop
;;
*)
echo $"Usage: $0 (start|status|stop)"
RETVAL=1
;;
esac
exit $RETVAL

 

Similar Posts

  • PuTTY SSH Tunneling

    This post explains how PuTTy SSH Tunneling can be easily used to bypass firewall rules blocking ports. Let’s say that we have the following setup where only communication via port 22 is allowed between two hosts: Step 1: Open profile and load server’s profile Step 2: Navigate to Connection->SSH->Tunnels and add new port as the…

  • Crack WEP using KisMAC

    Things you need: An Intel based macbook (other may work) Time! First thing you need to do is to download and install KisMAC. You can do this from http://kismac-ng.org/. Then launch the program and open Preferences. Go to Driver tab, select Apple Airport Extreme card, passive mode and click Add. Then Select All channels and…

  • Auction System

    This is an online auction system (like eBay) that lets users add items for sale and purchase items. Users are only allowed to enter bids if they have sufficient funds and when an auction has ended the money are being transfered from the buyer to the seller automatically. An extra feature supported by this auction…

  • Proxy Server

    Build a proxy server in C using threads and sockets. Every server can have up to two connections on the proxy. Client requests must be satisfied. New servers must be added to a list/array upon a request and must be removed if no request comes within 10 seconds. [Source]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.