Operating System - HP-UX
1819904 Members
2472 Online
109607 Solutions
New Discussion юеВ

Re: Creating a daemon using a shell script

 
SOLVED
Go to solution
unixdaddy
Trusted Contributor

Creating a daemon using a shell script

I'm having real trouble doing something that should be so simple. I need to create a daemon to run from boot time. I'm using the run control scripts to do this. So i've created a script called /sbin/rc3.d/S999testscript which reads:-

case $1 in
start)
echo start
/usr/local/adm/testscript1 /usr/local/adm/pid.log 2>&1 &
;;
stop)
echo stop
touch /usr/local/adm/killdaemon
;;
esac

This calls my daemon in the background so the boot can continue. My testscript1 reads:-

export PATH=$PATH:/usr/sbin:/usr/bin
echo $PATH
while true
do
echo "`date` - This is a test" >> /usr/local/adm/test.log 2>&1
sleep 10
done

When I run this from the command line it works fine. HOWEVER when it is executed via a reboot only one loop of the while loop takes place and then it just disappears. It doesn't run in the background. What am I doing wrong?
7 REPLIES 7
Massimo Bianchi
Honored Contributor
Solution

Re: Creating a daemon using a shell script

Hi,
i don't see anything evident.


Try two things (if no better advice comes...)

- specify the shell at the beginning

#!/sbin/sh

- start it with the nohup

nohup /usr/local/adm/testscript1 /usr/local/adm/pid.log 2>&1 &

HTH,
Massimo
A. Clay Stephenson
Acclaimed Contributor

Re: Creating a daemon using a shell script

Nohup will help but the shell is really a lousy choice to create a daemon. The fundamental problem is detaching a process from its controlling terminal -- this is essntial to 'daemonize' a process. Normally, you fork, let the parent exit, run setsid() to detach the controlling terminal, ignore or trap signals, especially SIGINT, SIGHUP, SIGPIPE, and SIGTERM. Typically, you want a SIGTERM to kill the daemon. For this reason, most daemons are done in C but you can do daemons very nicely in Perl -- and do them the right way.

See the attached daemon which is equivalent to your shell attempt. It took me longer to type this responce than it did to rip apart and paste together one of my existing daemons.

Regards, Clay
If it ain't broke, I can fix that.
unixdaddy
Trusted Contributor

Re: Creating a daemon using a shell script

The nohup seems to have done it and it is working now. Sometimes the simplest problems are the most fustrating to solve. Thanks for the perl idea, I've been meaning to learn perl for a long time, maybe now is the time to pick it up. The code for perl looks pretty straight forward so I think I'll look into using that long term.
unixdaddy
Trusted Contributor

Re: Creating a daemon using a shell script

The only thing I'm not sure about is whether perl comes installed on HP-UX by default. This code will also need to run on AIX and Sun, so perl maybe the best option as long as I don't have to install it on every platform, rather it comes installed already.
Shannon Petry
Honored Contributor

Re: Creating a daemon using a shell script

Perl is installed by default on every Unix I know of currently.

Be very cautious though, as different OS's ship with different versions of Perl.

Sincerely,
Shannon
Microsoft. When do you want a virus today?
Steven E. Protter
Exalted Contributor

Re: Creating a daemon using a shell script

After you determine the version and location of perl you have two options to make the code cross platform compatible.

lets say in HP-UX perl is at /usr/local/bin/perl

and in AIX its at /usr/local/perl

You can change the first line of your program to reflect that:

#!/usr/local/bin/perl

to

#!/usr/local/perl

Lets say you want the HP-UX script to work on all platforms unmodified.

Then on the AIX box, do this:

ln -s /usr/local/perl /usr/local/bin/perl

I use this kind of soft linking to avoid having to change perl scripts that run on many different OS's for my websites.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: Creating a daemon using a shell script

Avoiding Perl because it wasn't available on all platforms might have made sense as late as 5 or 6 years ago but now it's available on every flavor of UNIX. It's normally part of the default installation. Moreover, using the freely available Perls, your scripts will also run on Windows and if well-written will run without changes.
If it ain't broke, I can fix that.