Ubic::Daemon - toolkit for creating daemonized process
version 1.33_01
use Ubic::Daemon qw(start_daemon stop_daemon check_daemon); start_daemon({bin => '/bin/sleep', pidfile => "/var/lib/something/pid"}); stop_daemon("/var/lib/something/pid"); $daemon_status = check_daemon("/var/lib/something/pid");
This module can safely start and daemonize any binary or any perl coderef.
Main source of knowledge if daemon is still running is pidfile, which is locked all the time after daemon was created.
Pidfile format is unreliable and can change in future releases (it's actually even not a file, it's a dir with several files inside it), so if you need to get daemon's pid, use check_daemon() result.
Stop daemon which was started with $pidfile.
$pidfile
It sends SIGTERM to process with pid specified in $pidfile until it will stop to exist (according to check_daemon() method).
check_daemon()
If it fails to stop process after several seconds, exception will be raised (this should never happen, assuming you have enough grants).
Options:
Number of seconds to wait before raising exception that daemon can't be stopped.
Return value: not running if daemon is already not running; stopped if daemon is stopped by SIGTERM.
not running
stopped
Start daemon.
Throws exception if anything fails.
Successful completion doesn't mean much, though, since daemon can fail any moment later, and we have no idea when its initialization stage finishes.
Parameters:
Binary which will be daemonized.
Can be string or arrayref with arguments. Arrayref-style values are recommended in complex cases, because otherwise exec() can invoke sh shell which will immediately exit on sigterm.
exec()
Function which will be daemonized. One and only one of function and bin must be specified.
Function daemonization is a dangerous feature and will probably be deprecated and removed in future.
Name of guardian process. Guardian will be named "ubic-guardian $name".
If not specified, bin's value will be used, or anonymous when daemonizing perl code.
anonymous
Pidfile is a dir in local filesystem which will be used as a storage of daemon's info.
It will be created if necessary, assuming that its parent dir exists.
Write all daemon's output to given file. If not specified, all output will be redirected to /dev/null.
/dev/null
Write all daemon's error output to given file. If not specified, all stderr will be redirected to /dev/null.
Optional filename of ubic log. Log will contain some technical information about running daemon.
If not specified, this logging facility will be disabled.
Change working directory before starting a daemon. Optional.
Modify environment before starting a daemon. Optional. Must be a plain hashref if specified.
Can contain integer number of seconds to wait between sending SIGTERM and SIGKILL to daemon.
Zero value means that guardian will send sigkill to daemon immediately.
Default is 10 seconds.
Check whether daemon is running.
Returns instance of Ubic::Daemon::Status class if daemon is alive, and false otherwise.
Probably. But it definitely is ready for production usage.
This module is not compatible with Windows by now. It can be fixed by implementing correct Ubic::Daemon::OS::Windows module.
Ubic::Daemon::OS::Windows
If you wonder why there are ubic-guardian processes in your ps output, see Ubic::Manual::FAQ, answer is there.
ubic-guardian
ps
Ubic::Service::SimpleDaemon - simplest ubic service which uses Ubic::Daemon
There are also a plenty of other daemonizers on CPAN:
MooseX::Daemonize, Proc::Daemon, Daemon::Generic, Net::Server::Daemonize.
Vyacheslav Matyukhin <mmcleric@yandex-team.ru>
This software is copyright (c) 2011 by Yandex LLC.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Ubic, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Ubic
CPAN shell
perl -MCPAN -e shell install Ubic
For more information on module installation, please visit the detailed CPAN module installation guide.