Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I have been struggling with this for 2 days and finally I decided to ask for your help. I created a daemon using perl. When I run the perl script the daemon is created and work perfectly. The source code of the daemon is below:
#!/usr/bin/perl use POSIX qw(setsid); chdir '/' or die "cannot change to /:$!"; open STDIN,'/dev/null' or die "cannot read stdin:$!"; #open STDOUT ,'>>/dev/null' or die "cannot write to stdout:$!"; open STDERR ,'>>/dev/null' or die "cannot write to stderr:$!"; defined(my $pid=fork) or die "cannot fork process:$!"; exit if $pid; setsid; umask 0; while(1) { #system("perl Test.pl") or die "Can't exec: $!\n"; ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOf +Week, $dayOfYear, $daylightSavings) = localtime(); if ($hour < 19) { $waitSeconds = (19-$hour) * 3600 - 60 * $minute - $second; } $date = `date`; print "The date is $date \n"; print "The number of seconds $waitSeconds\n"; sleep($waitSeconds); &loadData; }
I have a Fedora 5 system installed on my computer and I want this daemon to be created at boot time. I read different posts on internet on how to do that and I understand that you have to put a script in etc/init.d which I did :
#!/bin/sh # Source function library. . /etc/rc.d/init.d/functions # See how we were called. case "$1" in start) # Start daemons. echo -n "Start load data daemon" perl home/magda/WorkArea/MyPerlScripts/DaemonScripts/TestDaemo +n.pl ;; stop) # Stop daemons. ;; restart) $0 stop $0 start ;; esac
But still when I restart the server and then check the process list wit ps axj my daemon is not there. I will greatly appreciate your help.

Replies are listed 'Best First'.
Re: How do I start a perl daemon at boot time
by Fletch (Bishop) on Feb 01, 2007 at 16:13 UTC

    Not that this is a Perl problem per se . . .

    Just having the startup/shutdown script in /etc/init.d isn't enough; it also needs to be symlink'd into the appropriate rc(runlevel).d directory (e.g. /etc/init.d/rc5.d for a typical X11-running machine). The easiest way to do this is provide suitable comments in your init.d script such that chkconfig knows how to handle it. See man chkconfig and specifically its section on "Runlevel Files" for more details.

Re: How do I start a perl daemon at boot time
by Joost (Canon) on Feb 01, 2007 at 16:14 UTC
      Hi, Thanks for your promt reply. Is it me who posted the questions but I do not know why my name was not registered. To be honest the interface it is a little hard to use. I must admit I am a little confused. Could you be more specific. I came from a Windows background and in Widows I can perform this operation with 3 - 4 mouse clicks. I had to switch to Fedora for secutiry reasons. I did spent 2 days on this. I know it is not hard, I just do not know why it is not working.

        Just like windows has the differentiation between normal operations and 'safe mode', unix-y operating systems have a concept of 'run level' ... and you need to specify which run level should start the process.

        The Unix System Administration Handbook is a good introductory tome, and the third edition includes linux.

        Update: It seems there's now a Linux System Administrator's Handbook, which is cheaper, and more recently published, so I assume more up-to-date. (I haven't read it, but it's by the same authors (mostly))

Re: How do I start a perl daemon at boot time
by sgt (Deacon) on Feb 01, 2007 at 17:12 UTC

    First something general. You should probably take advantage of daemonizing code as it is not trivial. The two editions of the Perl cookbook have recommendations on this. Lincoln Stein's Perl network programming has code on this too. Search daemon on CPAN

    Second, in the init script you should use absolute paths for perl and the script it tries to run. For the stop part (that is important too!) you need something like a pidfile that keeps the pid of the started process, so that in the stop part you test the pid reading the file and kill the daemon if up.

    hth --stephan

      Regarding the stopping of a daemon, I've found it useful to use a /global/ variable set to true, loop while variable is true and let the daemon do its work.

      Then, install one or more appropriate signal handlers (lets say, for SIGHUP & friends) and have them undefine the global variable. This allows the daemon to properly to *finish* its operations; critical are, for example, IO operations.

      Without these precautions, some data could get corrupted.

        I finally did it. Thank you all for your support. I still did not understand half a thing I have done but I manage to make the computer do what I wanted from him to do. magnolia
Re: How do I start a perl daemon at boot time
by magnolia (Initiate) on Feb 01, 2007 at 16:42 UTC
    Hi, Is it me who post the questions. Thank you for your imput. I read the manual on chkconfig and I did not understand a thing. Is it me beeing stupid or linux is hard? I will try use this comand and came back to you.
      magnolia:

      It's not you being stupid or linux being hard. It's just a different way of doing things. So it's more like "you're speaking Polish and the computer is speaking French".

      For what it's worth, there are graphical tools for linux that let you do that in a few clicks of a mouse too. But when you're in the unix/linux world, you eventually learn that the mouse might seem like a nice way of doing things, but it isn't as powerful as the keyboard. So most unix/linux people I know tend to do everything in a shell window. You get more control over the system and you learn how things work (and work together!).

      An example: In Windows if you want to delete the file foo.bar, you just drag it to the recycle bin or right-click it in an explorer window and select delete. Simple! In a linux machine you type "rm foo.bar". Easy, but not as simple as a point & click, is it?

      Ah, but suppose you need to delete all backup and object files in a directory with a q in their name. In linux "rm *q*.{bak,o}". Simple! On a Windows machine, you click--and drag. Click--and drag. Click--and drag. What a drag!

      One other thing. Everyone I know who uses linux/unix finds it "hard" when they start. There are *so* many things to learn. But if you keep reading your man pages and working in it, you'll all of a sudden have a mental "click" and it's not hard anymore. You'll then find that you're much more productive in a good command shell, and having to do everything with the mouse will annoy you because it's just too slow for lots of tasks. So much so, that you'll want to find a good unix-like environment for Windows so you can do things the same way on both machines.

      --roboticus

      All you need to do is add two lines of comments to your /etc/init.d/SCRIPTNAME script that tell chkconfig at which runlevels it should run.

      from this manpage:

      For example, random.init has these three lines: # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation. This says that the random script should be started in levels 2 +, 3, 4, and 5, that its start priority should be 20, and that its stop + prior- ity should be 80. You should be able to figure out what the d +escrip- tion says; the \ causes the line to be continued. The extra sp +ace in front of the line is ignored.
      The runlevel determines at which runlevels the deamon should run. The system is always in one runlevel only. Usual runlevels are 3 for multi-user non-graphical (default mode for server machines), 5 for multi-user graphical mode (default mode for desktop machines) and 1 for single-user non-gui mode (for low-level system management and repair). what each runlevel is for can be changed, these are just usual configurations

      To see which run-level is the default for your system, take a look at /etc/inittab. There should be a line about that at the top somewhere. Mine looks like:

      # The default runlevel. id:3:initdefault:
      So my machine's default runlevel is 3 (though I also use 5)

      The start and end priorities determine the order in which deamons are started and stopped: higher start priority means it's started later than lower priority.

      After you've done that, run "chkconfig --add NAME_OF_SCRIPT_IN_INITRD" to add it to the list of deamons to start automatically.