grizzled.os module
Overview
The grizzled.os
module contains some operating system-related functions and
classes. It is a conceptual extension of the standard Python os
module.
Functions
def daemonize(
no_close=False, pidfile=None)
Convert the calling process into a daemon. To make the current Python process into a daemon process, you need two lines of code:
from grizzled.os import daemonize daemonize.daemonize()
If daemonize()
fails for any reason, it throws a DaemonError
,
which is a subclass of the standard OSError
exception. also logs debug
messages, using the standard Python logging
package, to channel
"grizzled.os.daemon".
Adapted from: http://software.clapper.org/daemonize/
See Also:
- Stevens, W. Richard. Unix Network Programming (Addison-Wesley, 1990).
Parameters
no_close
(bool
): IfTrue
, don't close the file descriptors. Useful if the calling process has already redirected file descriptors to an output file. Warning: Only set this parameter toTrue
if you're sure there are no open file descriptors to the calling terminal. Otherwise, you'll risk having the daemon re-acquire a control terminal, which can cause it to be killed if someone logs off that terminal.pidfile
(str
): Path to file to which to write daemon's process ID. The string may contain a${pid}
token, which is replaced with the process ID of the daemon. e.g.:"/var/run/myserver-${pid}"
Raises
DaemonError
: Error during daemonizing
def find_command(
command_name, path=None)
Determine whether the specified system command exists in the specified path.
Parameters
command_name
(str
): (simple) file name of command to findpath
(str
or sequence): Path string (or sequence of path elements) to use. Defaults to environmentPATH
.
Returns
Full path to the command, or None
if not found.
def path_separator(
)
Get the path separator for the current operating system. The path separator is used to separate elements of a path string, such as "PATH" or "CLASSPATH". (It's a ":" on Unix-like systems and a ";" on Windows.)
:rtype: str :return: the path separator
def spawnd(
path, args, pidfile=None)
Run a command as a daemon. This method is really just shorthand for the following code:
from grizzled.os import daemonize import os daemonize(pidfile=pidfile) os.execv(path, args)
Parameters
path
(str
): Full path to program to runargs
(list ofstr
): List of command arguments. The first element in this list must be the command name (i.e., arg0).pidfile
(str
): Path to file to which to write daemon's process ID. The string may contain a${pid}
token, which is replaced with the process ID of the daemon. e.g.:"/var/run/myserver-${pid}"
def working_directory(
*args, **kwds)
This function is intended to be used as a with
statement context
manager. It allows you to replace code like this:
original_directory = _os.getcwd() try: _os.chdir(some_dir) what_you_want_to_do() finally: _os.chdir(original_directory)
with something simpler:
from grizzled.os import working_directory with working_directory(some_dir): what_you_want_to_do()
Parameters
directory
(str
): directory in which to execute your code
Classes
class DaemonError
Thrown by daemonize()
when an error occurs while attempting to create
a daemon.
Ancestors (in MRO)
- DaemonError
- builtins.OSError
- builtins.Exception
- builtins.BaseException
- builtins.object
Class variables
var args
var characters_written
var errno
var filename
var filename2
var strerror