Sponsored Content
Operating Systems Linux Ubuntu How can I automatically start a daemon at boot time. Post 302269555 by s93366 on Thursday 18th of December 2008 03:32:52 AM
Old 12-18-2008
a basic unix daemon does the following:

fork
close all filedescriptors (stdout,stderr, etc)
chdir /
signal handeling (sighup, sigterm etc)
while
do stuff
sleep(xx)
done

(example in C: daemon.c)


Red Hat example on how to install startup scripts:

to start a deamon at system startup in redhat you need a init script.
it should be placed in /etc/init.d

example of init script :

Code:
# chkconfig: 3 99 1
# description: my daemon

case "$1" in
'start')
/usr/local/bin/mydaemon
;;

'stop')
pkill mydaemon
;;

'restart')
pkill -HUP mydaemon
;;

esac

the first line will tell chkconfig to start the daemon in run level 3 with priority 99 and kill it as priority 1 when server shutdowns.

to install the startup script use the following: chkconfig --add ./scriptabove
Now it will start when the server boots.

to start it right away use: service <name_of_initfile> start


Hope this helps somewhat!

Last edited by s93366; 12-18-2008 at 04:38 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Disable routed daemon at boot time????

I'm new to UNIX.... I'm running SCO UNIX and would like to disable routed daemon from being start at boot time. How should I do this????? Please help. (2 Replies)
Discussion started by: rrivas
2 Replies

2. UNIX for Advanced & Expert Users

start a process at boot up time

Hi, I have a program that check the IP address and automatic update it to the DNS server. I would like to run this program when the computer bootup after pppd get a connection. How do I add it to the init file. Does any one have any information of how to do it. I run a Linux Mandrake as a... (1 Reply)
Discussion started by: vtran4270
1 Replies

3. Solaris

Database can't start automatically on solaris

Hi, I have installed oracle 9i on solaris 10 sucessfully. I have configured listener & tnsname.ora file sucessfuly. Even i can start database manully by starting listener first and from sqlplus promt data base sucessfully. Now i want to start the database automatically after system reboots. I... (2 Replies)
Discussion started by: Sachin Vaidya
2 Replies

4. HP-UX

HP UX start process at boot time

When I get start program at boot I read which run level /sbin/rcx.d runlevel=0.....x only read directory which directory name has UpperCase 'S' is not enough someone says that I need to reference another file which file I need to reference 1)/etc/rc.config.d/all file which parameter... (4 Replies)
Discussion started by: alert0919
4 Replies

5. Shell Programming and Scripting

Start program in background (or start crontab ahead of time)

Hey! I'm working on a script that will add a user, create some configfiles, and add a crontab for the user. The crontab looks like the following: @reboot /home/user/program config.conf & I would like for this process to start at the end of my script under the corresponding username by... (0 Replies)
Discussion started by: noratx
0 Replies

6. AIX

How to start rstatd Daemon

guy's How to start rstatd Daemon on AIX 6.1 as I enable it from etc/inetd.conf then I refreshed it by this command refresh -s inetd but stil is not Active ! can some one advice in this ! (7 Replies)
Discussion started by: ITHelper
7 Replies

7. Shell Programming and Scripting

How do I Start JBoss automatically when boot my linux machine.

I have created a script file named 'start-jboss' ------------------------------------------------- #!/bin/sh # For starting Jboss JAVA_HOME=/home/argole/jdk/jdk1.6.0_10 export JAVA_HOME cd /home/argole/server/jboss-4.2.0.GA/bin echo "Starting JBOSS server" nohup ./run.sh -c... (25 Replies)
Discussion started by: shiraz
25 Replies

8. UNIX and Linux Applications

How do I Start postgre SQL automatically when boot my linux machine

Can any body give me the script to start postgre SQL 8.3.1 when boot linux? (2 Replies)
Discussion started by: shiraz
2 Replies

9. UNIX for Advanced & Expert Users

start some services automatically

Hello All i have a question related to some services,i want to start some services with server linux centos. i make "chkconfig httpd on" and "chkconfig asterisk on",but when i verify with chkconfig --list i found httpd 0: off 1: off 2: on 3: on 4: on 5: on 6:... (6 Replies)
Discussion started by: bernard12
6 Replies

10. Red Hat

Can't start NTOP service/daemon

I have installed version of ntop 4.0.3 by guide. But I can't start ntop daemon/service. I didn't find a service file for starting. During the installation there was no problem only want to RRDTool so I installed that. Now there is no necessary package required. I didn't find in /etc/init.d/... (9 Replies)
Discussion started by: getrue
9 Replies
Daemon::Control(3pm)					User Contributed Perl Documentation				      Daemon::Control(3pm)

NAME
Daemon::Control - Create init scripts in Perl DESCRIPTION
Daemon::Control provides a library for creating init scripts in perl. Your perl script just needs to set the accessors for what and how you want something to run and the library takes care of the rest. You can launch programs through the shell (/usr/sbin/my_program) or launch Perl code itself into a daemon mode. Single and double fork methods are supported and in double-fork mode all the things you would expect like reopening STDOUT/STDERR, switching UID/GID are supported. SYNOPSIS
Write a program that describes the daemon: #!/usr/bin/perl use warnings; use strict; use Daemon::Control; Daemon::Control->new({ name => "My Daemon", lsb_start => '$syslog $remote_fs', lsb_stop => '$syslog', lsb_sdesc => 'My Daemon Short', lsb_desc => 'My Daemon controls the My Daemon daemon.', path => '/home/symkat/etc/init.d/program', program => '/home/symkat/bin/program', program_args => [ '-a', 'orange', '--verbose' ], pid_file => '/tmp/mydaemon.pid', stderr_file => '/tmp/mydaemon.out', stdout_file => '/tmp/mydaemon.out', fork => 2, })->run; You can then call the program: /home/symkat/etc/init.d/program start You can also make an LSB compatible init script: /home/symkat/etc/init.d/program get_init_file > /etc/init.d/program CONSTRUCTOR
The constuctor takes the following arguments. name The name of the program the daemon is controlling. This will be used in status messages "name [Started]" and the name for the LSB init script that is generated. program This can be a coderef or the path to a shell program that is to be run. $daemon->program( sub { ... } ); $daemon->program( "/usr/sbin/http" ); program_args This is an array ref of the arguments for the program. In the context of a coderef being executed this will be given to the coderef as @_, the Daemon::Control instance that called the coderef will be passed as the first arguments. Your arguments start at $_[1]. In the context of a shell program, it will be given as arguments to be executed. $daemon->program_args( [ 'foo', 'bar' ] ); $daemon->program_args( [ '--switch', 'argument' ] ); user When set, the username supplied to this accessor will be used to set the UID attribute. When this is used, "uid" will be changed from its inital settings if you set it (which you shouldn't, since you're using usernames instead of UIDs). See "uid" for setting numerical user ids. $daemon->user('www-data'); group When set, the groupname supplied to this accessor will be used to set the GID attribute. When this is used, "gid" will be changed from its inital settings if you set it (which you shouldn't, since you're using groupnames instead of GIDs). See "gid" for setting numerical group ids. $daemon->group('www-data'); uid If provided, the UID that the program will drop to when forked. This is ONLY supported in double-fork mode and will only work if you are running as root. Accepts numeric UID. For usernames please see "user". $daemon->uid( 1001 ); gid If provided, the GID that the program will drop to when forked. This is ONLY supported in double-fork mode and will only work if you are running as root. Accepts numeric GID, for groupnames please see "group". $daemon->gid( 1001 ); directory If provided, chdir to this directory before execution. path The path of the script you are using Daemon::Control in. This will be used in the LSB file genration to point it to the location of the script. If this is not provided $0 will be used, which is likely to work only if you use the full path to execute it when asking for the init script. init_config The name of the init config file to load. When provided your init script will source this file to include the environment variables. This is useful for setting a PERL5LIB and such things. $daemon->init_config( "/etc/default/my_program" ); redirect_before_fork By default this is set true. STDOUT will be redirected to stdout_file, STDERR will be redirected to stderr_file. Setting this to 0 will disable redriecting before a double fork. This is useful when you are using a code ref and would like to leave the file handles alone until you're in control. Call ->redirect_filehandles on the Daemon::Control instance your coderef is passed to redirect the filehandles. stdout_file If provided stdout will be redirected to the given file. This is only supported in double fork more. $daemon->stdout_file( "/tmp/mydaemon.stdout" ); stderr_file If provided stderr will be redirected to the given file. This is only supported in double fork more. $daemon->stderr_file( "/tmp/mydaemon.stderr" ); pid_file The location of the PID file to use. Warning: if using single-fork mode, it is recommended to set this to the file which the daemon launching in single-fork mode will put it's PID. Failure to follow this will most likely result in status, stop, and restart not working. $daemon->pid_file( "/tmp/mydaemon.pid" ); fork The mode to use for fork. By default a double-fork will be used. In double-fork, uid, gid, std*_file, and a number of other things are supported. A traditional double-fork is used and setsid is called. In single-fork none of the above are called, and it is the responsiblity of whatever you're forking to reopen files, associate with the init process and do all that fun stuff. This mode is recommended when the program you want to control has it's own daemonizing code. It is importand to note that the PID file should be set to whatever PID file is used by the daemon. $daemon->fork( 1 ); $daemon->fork( 2 ); # Default scan_name This provides an extra check to see if the program is running. Normally we only check that the PID listed in the PID file is running. When given a regular expression, we will also match the name of the program as shown in ps. $daemon->scan_name( qr|mydaemon| ); lsb_start The value of this string is used for the 'Required-Start' value of the generated LSB init script. See <http://wiki.debian.org/LSBInitScripts> for more information. $daemon->lsb_start( '$remote_fs $syslog' ); lsb_stop The value of this string is used for the 'Required-Stop' value of the generated LSB init script. See <http://wiki.debian.org/LSBInitScripts> for more information. $daemon->lsb_stop( '$remote_fs $syslog' ); lsb_sdesc The value of this string is used for the 'Short-Description' value of the generated LSB init script. See <http://wiki.debian.org/LSBInitScripts> for more information. $daemon->lsb_sdesc( 'Mah program...' ); lsb_desc The value of this string is used for the 'Description' value of the generated LSB init script. See <http://wiki.debian.org/LSBInitScripts> for more information. $daemon->lsb_desc( 'My program controls a thing that does a thing.' ); METHODS
run This will make your program act as an init file, accepting input from the command line. Run will exit either 1 or 0, following LSB files on exiting. As such no code should be used after ->run is called. Any code in your file should be before this. do_start Is called when start is given as an argument. Starts the forking, and exits. /usr/bin/my_program_launcher.pl start do_stop Is called when stop is given as an argument. Stops the running program if it can. /usr/bin/my_program_launcher.pl stop do_restart Is called when restart is given as an argument. Calls do_stop and do_start. /usr/bin/my_program_launcher.pl restart do_status Is called when status is given as an argument. Displays the status of the program, basic on the PID file. /usr/bin/my_program_launcher.pl status do_get_init_file Is called when get_init_file is given as an argument. Dumps an LSB compatible init file, for use in /etc/init.d/ /usr/bin/my_program_launcher.pl get_init_file pretty_print This is used to display status to the user. It accepts a message, and a color. It will default to green text, if no color is explicitly given. Only supports red and green. $daemon->pretty_print( "My Status", "red" ); write_pid This will write the PID to the file in pid_file. read_pid This will read the PID from the file in pid_file and set it in pid. pid An accessor for the PID. Set by read_pid, or when the program is started. dump_init_script A function to dump the LSB compatible init script. Used by do_get_init_file. AUTHOR
SymKat <symkat@symkat.com> ( Blog: <http://symkat.com/> ) CONTRIBUTORS o Matt S. Trout (mst) <mst@shadowcat.co.uk> o Mike Doherty (doherty) <doherty@cpan.org> COPYRIGHT
Copyright (c) 2012 the Daemon::Control "AUTHOR" and "CONTRIBUTORS" as listed above. LICENSE
This library is free software and may be distributed under the same terms as perl itself. AVAILABILITY The most current version of Daemon::Control can be found at https://github.com/symkat/Daemon-Control <https://github.com/symkat/Daemon- Control> perl v5.14.2 2012-06-06 Daemon::Control(3pm)
All times are GMT -4. The time now is 11:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy