respawn option for bsd rc.d - linux

I run a small daemon and want it to be respawned when it is killed. I use "respawn" option in inittab on linux systems.(It is a small embedded platform.).
Now I am trying the same daemon on BSD. I have put my entry in "rc.d". But I could not find respawn option for BSD.
I can write a small program which respawns my daemon. But I was wondering if there must be something already built for BSD to restart killed services.
Do you know anything which I can use.
Thanks
P.S. I know I can do this thing in my daemon itself. But currently I dont have source for it.

The rc.d/init.d startup script convention does not provide for respawning daemons. That's one of the main reasons why alternatives like upstart and systemd have been created. On your embedded system, your best option is probably a small wrapper that monitors your daemon and restarts it when necessary.

Related

How exactly a 'shutdown -h' "HALT" differ from "shutdown" in linux

Suppose I have 20 process/deqamons running in my linux system,
How different the HALT will have an effect on my process/deamons, when compared to a SHUTDOWN
Generally, one uses the shutdown command. It allows a time delay and warning message before shutdown or reboot, which is important for system administration of multiuser shell servers; it can provide the users with advance notice of the downtime.
As such, the shutdown command has to be used like this to halt/switch off the computer immediately (on Linux and FreeBSD at least):
shutdown -h now
Or to reboot it with a custom, 30 minute advance warning:
shutdown -r +30 "Planned software upgrades"
After the delay, shutdown tells init to change to runlevel 0 (halt) or 6 (reboot). (Note that omitting -h or -r will cause the system to go into single-user mode (runlevel 1), which kills most system processes but does not actually halt the system; it still allows the administrator to remain logged in as root.)
Once system processes have been killed and filesystems have been unmounted, the system halts/powers off or reboots automatically. This is done using the halt or reboot command, which syncs changes to disks and then performs the actual halt/power off or reboot.
On Linux, if halt or reboot is run when the system has not already started the shutdown process, it will invoke the shutdown command automatically rather than directly performing its intended action. However, on systems such as FreeBSD, these commands first log the action in wtmp and then will immediately perform the halt/reboot themselves, without first killing processes or unmounting filesystems.
On POSIX systems the shutdown command switches runlevels, and executes the appropriate scripts.
On FreeBSD the "halt" command is an ACPI thing...
If you have particular concerns or would like to know things the general documentation wouldn't readily address, please feel free to refine your query.

Detect new process creation instantly in linux

I am trying to create an application in userspace that sets affinity of processes. I would like the program to be triggered immediately every time a new pid/tid is spawned by the kernel. I am attempting to write to a file node under /proc from the do_fork() method in the kernel but I feel that it may have too much overhead.
Does anyone know any alternatives to detect a new process creation immediately after it is spawned?
If monitoring do_fork() is the way to go, would a call back to an userspace program via a system call be faster that using a fs node to communicate?
Forkstat is a program that logs process fork() [among other things]
Install it:
$ sudo apt-get install forkstat
Use it to log "fork" events:
$ forkstat -e fork
Use a socket with NETLINK_CONNECTOR. The kernel will tell you about process events, including fork()s and exec()s. You must have CONFIG_CONNECTOR and CONFIG_PROC_EVENTS enabled in your kernel.
Here's a related question with more details:
Detect launching of programs on Linux platform
For a complete socket NETLINK_CONNECTOR example, see:
http://bewareofgeek.livejournal.com/2945.html
As an aside, Inotify doesn't work. It will not work on /proc/ to detect new processes:
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/454722
execsnoop can be a good alternative to show new processes and arguments.

What can cause SIGHUP to be generated?

We have about 40 computers running identical hardware and software. They all run Ubuntu 11.10. They all have just one user account to log in. The .profile file is set up to launch a daemon process. The code for the daemon is written in C.
Once in a few weeks, we get a report that the daemon is no longer running. This does not happen on all computers but just one or two. We cannot reproduce the problem consistently.
Looking at the code, the application quits when it receives either SIGHUP or SIGTERM.
As I understand, SIGHUP is generated when a user logs off. In our case, the user never logs off. I am wondering if it is possible that SIGHUP could have been generated for some other reason. Any other thought would be appreciated.
Well, there are a couple of things to note about SIGHUP. Firstly, its origin is from the concept of a hang-up, i.e. loss of connection to a console over something like a modem. In modern parlance this generally means it has lost its controlling tty. Unless you've taken care to detach from your tty, any program started in a given terminal will receive a SIGHUP when the terminal is closed. See here for details on how to do this in your program. Other options include:
running your program inside screen or tmux
run your program with nohup or some other daemonising framework
The other possibility is something is deliberately sending your process a SIGHUP which by "tradition" is often used to signal a process that it should re-read its configuration.
Signals can be sent using kill utility or kill syscall.
Of course, you can try and find out who is sending that signal or disconnecting your terminals or network connections, but there is simpler practical way to fix your problem.
When code is supposed to run as a daemon, but really isn't (just like yours), there is a wrapper that can turn any program into daemon. Surprise - this wrapper is called daemon! It has lots of options, probably most importantly for you, option to automatically restart your utility should it ever die for any reason.
If this command is not installed on your Ubuntu, just sudo apt-get install daemon, and man daemon to get started.

Watchdog for Linux

Are there any watchdog tools or libraries on Linux for the following purpose? I would like to build a watchdog executable which starts 2 processes and restarts them if:
processes crash
processes become unresponsive (e.g. hang for some reason)
Internet search found watchdog.c but I am not sure if that can be used for my purposes, it looks pretty low level.
I could run my processes as init programs (daemons) as suggested here, but I am not sure if Linux would then recognize that the process is hanging (e.g. due to a deadlock)
We use monit here: http://mmonit.com/monit/ it will let you do the restart thing it is also highly customizable regarding how to check and how to react via scripts

Run .exe with daemon or better solutions to get something similar to windows service (mac and linux)?

I was wondering if i can run a exe with daemons in mac and linux or do you have any other solutions to do something similar to a windows service that is a scheduler ? I know i can use crontab but i was wondering if there was other way to do it.
Thx
On OS X, the preferred way of doing things like this is with launchd daemons. You create a .plist file with information about what program to run, parameters to pass it, and what conditions to start it under (i.e. at certain times, when a network connection is received on a certain port, or just run always), and various other options. Lingon provides a handy GUI for creating the .plist, or just read the Apple LAUNCHD docs and create it yourself. Put the .plist in /Library/LaunchDaemons, and either reboot or activate it with sudo launchctl load /Library/LaunchDaemons/whatever.plist.
A warning about using launchd: most daemon-type programs for unix will "daemonize" themselves -- they drop into the background, and generally detach themselves from the program that started them. Launchd doesn't like this. It wants to keep watch over its children, so that it can monitor their status, relaunch them if necessary, etc. So you may either need to tell the program not to daemonize, or add an option to the .plist to tell launchd not to freak out if the program appears to quit.
Linux alternative to windows NT services are daemons. You can read a little more about it
here.
You also start executables by scripts located in "/etc/init.d" Just look at one of those scripts for reference. If you want to make a task or executable start at a given time use a crontab. It is made for this purpose and I don't see why use something else.
If you have a mono executable probably the easiest way is just to make a script in "init.d" if you want to start when system starts or make a crontab entry. It is realy easy. Here you can find a simple reference.

Resources