Operating System - HP-UX
1833323 Members
2951 Online
110051 Solutions
New Discussion

Re: automatic service startup while booting

 
SOLVED
Go to solution
Marco Müller
Occasional Advisor

automatic service startup while booting

Hello community,

I try to start a deamon process (written in perl) during system startup. On my linux machines I'm using the following code in one of my startup scripts:

#!/bin/sh
#
...
...
...
if [ "$START_NOVAD" -eq 1 -a -x $novad ]; then
if [ $debugfile != "syslog" ];then
$novad 1>$debugfile 2>&1 &
else
$novad | /bin/logger -i -t novad &
fi
set_return
echo "$productname $programname starting up"
set_pid
else
rval=3
fi
...
...
...

On my Linux machines the process starts and logs every message to either syslog (via /bin/logger) or in the logfile.

Under HP-UX this seems not to work! Can you help me within these two questions?

1. If I use exactly this code the process stops emediately after starting. :-( Manually executing the startscript as root works fine. Why?

2. I modified the code and tried to start the deamon with "/bin/nohup $novad". That works! The deamon starts but only if I _don't_ use " | /bin/logger...". It works fine while executing it manually as root - also with "| /bin/logger...". Why?

Regards
Marco
Marco Mueller
9 REPLIES 9
doug mielke
Respected Contributor

Re: automatic service startup while booting

If nohup makes it work then the shell that starts the process is exiting, killing all shell processes (nohup's excepted)

How do you start this during startup? A guess would be that the startup script that you placed this in has completed, and closed it's shell.

You could place this script in one of the rc directories and have it start as a daemon during boot.
Marco Müller
Occasional Advisor

Re: automatic service startup while booting

Hi Doug,

> If nohup makes it work then the shell that > starts the process is exiting, killing all > shell processes (nohup's excepted)

Yes, sounds good - but: It works under Linux this way. Why not in HP-UX?

> How do you start this during startup? A
> guess would be that the startup script
> that you placed this in has completed,
> and closed it's shell.

I have a script /sbin/init.d/novad which is linked to /sbin/rc3.d/S990novad. This script contains my posted lines.

So if you mean the only way to start my daemon during bootup is doing it via nohup, could you tell me why my daemon won't work completely when piping it through logger? I cannot! ;-)

I think I should post a summary for the moment:

1. $novad: does not work during bootup
2. nohup $novad: works during startup
3. nohup $novad | logger: does not work during bootup

-> But every of the three versions of the script are working fine while executing them as logged in root user.

I'm confused
Marco
Marco Mueller
Bill Hassell
Honored Contributor

Re: automatic service startup while booting

For the first questions:

1. Startup is a VERY different environment from a login. At startup, there is no controlling terminal and the env values are quite different. Startup scripts require 4 separate parameter tests and are required to reject anything else. The file /sbin/init.d/template gives you the details. Startup scripts must also handle shutdown properly, ideally terminating the process(es). In the template, you'll see that start,stop,star_msg,stop_msg must be handled and that return codes 0,1,2,3,4 must be handled. Pay particular attention to return code 4 in the beginning comments of the template.

But it's important to understand that all startup scripts must run to completion in a short period. If it did not, the machine would take a very long time to bootup.

2. You must use nohup to protect your process that you put into the background. If you do not, then the end of the script (which is the parent) will kill the process. However, in your example, you used nohup to protect your porcess but then connected it with a pipe to logger and put logger into the background. You need to put the entire command pipe into a subshell so it is treated as a single entity:

nohup ($novad | /bin/logger -i -t novad) &

A couple of scripting notes:

Using fullpaths is good but makes scripts hard to read. A shorter method to protect scripts from bad PATH values is to replace PATH in the script:

export PATH=/usr/bin

Another note is that HP-UX has followed the V.4 filesystem layout standard for more than a decade. There is no /bin or /lib directory. The correct location is /usr/bin and /usr/lib. Your script works because HP has included transition links (man tlinstall) that point legacy directories and filenames to the V.4 standard names. There is no guarentee that any HP-UX system has the links installed (man tlremove) and at some future revision, the default will be no transition links.

And to answer the last question about Linux versus HP-UX: when it comes to startup, shutdown, I/O and system configuration, there are very few standards so assume nothing is common between systems in those areas and you won't be disappointed. Although HP-UX started shipping in the mid-1980's, I doubt that Linus Torvalds consulted HP or HP-UX manuals when he designed Linux back in the early 1990's.


Bill Hassell, sysadmin
Marco Müller
Occasional Advisor

Re: automatic service startup while booting

Great,

> 1. ... The file /sbin/init.d/template gives you the details. Startup scripts

I know this script and tried to write mine the same way.

> must also handle shutdown properly, ideally terminating the process(es).

Yes, I also have the other sections in my script. I haven't posted them because they're working fine. ;-)

> 2. You must use nohup to protect your process that you put into the
> background. If you do not, then the end of the script (which is the parent)
> will kill the process.

That was new for me. I've written many scripts for linux and thought I can do it on HP-UX the same way.

> However, in your example, you used nohup to protect your porcess but then
> connected it with a pipe to logger and put logger into
> the background. You need to put the entire command pipe into a subshell
> so it is treated as a single entity:

Sounds very good! I'll try that.

> nohup ($novad | /bin/logger -i -t novad) &

I've tried it, but I got a "syntax error: "(" is not expected"

> A couple of scripting notes:

> ... There is no /bin or /lib directory. The correct
> location is /usr/bin and /usr/lib. Your script works because HP has included
> transition links (man tlinstall) that point legacy directories and filenames
> to the V.4 standard names. There is no guarentee that any HP-UX system has
> the links installed (man tlremove) and at some future revision, the default will
> be no transition links.

Oops, thanks for this hint!

Marco
Marco Mueller
Marco Müller
Occasional Advisor

Re: automatic service startup while booting

found another notice in the man page:

- It is frequently desirable to apply nohup to pipelines or lists of commands. This can be done only by placing pipelines and command lists in a single file, called a shell script. To run the script using nohup:

nohup sh file

Do they mean it is possible to execute "nohup (command1; command2)" wihich is shown in the man page but quits with a synthax error, but not "nohup (command1 | command2)"? I can't belive it. :-(
Marco Mueller
Peter Nikitka
Honored Contributor
Solution

Re: automatic service startup while booting

Hi Marco,

concerning the 'nohup' - problem, you can try something like this:

nohup sh -c "$novad | logger ..."

This should work.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: automatic service startup while booting

Rather than fooling around with nohup a far better approach would be to properly code your daemon so that it fork()'s, does a setsid(), chdir()'s to / (so that no filesystem is busy in case a umount is needed --- good daemon manners). Since you are already using Perl, you probably just need to add a few lines and then your code becomes a proper daemon.

Attached is a example of a daemon; don't be concerned about what it does but just examine the section of code around the fork(). Note that the real work of the daemon is done by the child process after the fork() and the parent simply exits so that the process (your rc.d stuff) does not hang.
If it ain't broke, I can fix that.
Marco Müller
Occasional Advisor

Re: automatic service startup while booting

@Peter Nikitka:

That is exactly what I was searching for! It works fine and so I do not need another script to execute the two commands. Thank you!

@A. Clay Stephenson:

Yes, you're right. The daemon isn't written by myself and I'm not a real good "perler". But I forwarded the thread to the company the maintain the daemon for us. So they'll say me what to do to let the daemon act like a real daemon. :-)

@all:

Thanks for all your suggestions. Never found a forum like this! Go on with your good hints!
Marco Mueller
Marco Müller
Occasional Advisor

Re: automatic service startup while booting

Thanks everybody!
Marco Mueller