Documents

Understanding and Managing Sessions (or, Using GXP in your script)

Introduction

When you type the first command in your shell, it says something like

 $ gxpc
 gxpc: no daemon found, create one

and brings up a daemon on the host. When you explore some other hosts, daemons start at those hosts, too. They are connected together (in a tree) and stay alive until you type 'gxpc quit'. While daemons are running, you can access them via the host you brought up the first daemon. You may access it in different terminals of the host, and even after you have once logged out and then logged back in again to the host. No matter where you are, the gxpc command automatically finds the running daemon on the host and issues your command to it.

While this behavior is convenient for typical interactive use, you will eventually wonder how you can run multiple such daemons from a single host. It most frequently occurs that you like to use GXP within your script without affecting, or not being affected by, another script or interactive session.

A Quick Example

To cut to the chase, you will probably find the following idiom useful in your script.

 #!/bin/bash -x
 export GXP_SESSION=`gxpc --create_daemon 1`
 
 ... whatever ...
 
 gxpc quit

This is for bash. For csh and friends, use the following instead.

 #!/bin/csh -x
 
 setenv GXP_SESSION `gxpc --create_daemon 1`
 
 ... whatever ...
 
 gxpc quit

Of course, they are regular shell script in which you can freely mix gxpc commands and others. To make them more robust, consider 'gxpc quit' upon keyboard interrupts. e.g.,

 #!/bin/sh
 
 GXP_SESSION=`gxpc --create_daemon 1`
 export GXP_SESSION
 trap 'gxpc quit; exit 1' 2
 
 ... whatever ...
 
 gxpc quit

Here is a small but complete example that brings up daemons, run a command, and quit. This script runs in isolation with an active interactive session or another script running.

 #!/bin/sh
 
 GXP_SESSION=`gxpc --create_daemon 1`
 export GXP_SESSION
 
 # handle Ctrl-C
 trap 'gxpc quit; exit 1' 2
 
 gxpc use ssh ... # fill for your needs 
 gxpc explore ... # fill for your needs 
 gxpc e hostname
 gxpc quit
 
 # exit 0

There are two keys in this example.

  • Option '--create_daemon 1' will create a new daemon even if there is another daemon running. This ensures there are no interference between this script and others.
  • Command 'gxpc --create_daemon 1', or in more general gxpc issued withtout any command simply prints its associated session name. Thus in the above example, GXP_SESSION holds the session name just created.
  • When GXP_SESSION environment is set (and is a non-empty string) gxpc uses it as the session (thus the daemon) to issue commands to.

A session name is a long path name like

 /tmp/gxp-<user>-default/gxpsession-...

and it is a regular file containing states associated with the session.

Theory

There are one or more daemons running. Normally one or more sessions are associated with each daemon, but a daemon having no associated sessions is in theory possible. Most of the time, there is a single daemon and a single session. GXP allows you to

  • control when a session or a daemon should be created, and
  • specify which session or daemon you like to work with

By allowing multiple sessions associated with a single daemon, you may have different contexts for different tasks, still sharing daemon processes running on hosts. This way, you may save time to bring up daemons (explore) on all hosts you like to work on. Each session independently keeps track of user-visible states, such as

  • current directory and environment variables of each host
  • hosts selected for execution

Here is the related options and environment variables. They are global options that apply to all gxpc commands and must be put before the gxpc command name (e.g., 'gxpc --create_session 1 e hostname').

  • --create_session -1/0/1 (default: 0)
  • --create_daemon -1/0/1 (default: 0)
  • --session and GXP_SESSION environment variable
  • --daemon and GXP_DAEMON environment variable

Option --create_session controls in which circumstances a new session should be created. -1 means 'never', 1 'always', and 0 'when not found'. Likewise for --create_daemon. The argument to --session option and the variable GXP_SESSION specifies the regular expression of session names this command must attach to. The option and the environment variable have the same effect, and when both are given, the option has the priority. Likewise, the argument to --daemon option and the variable GXP_DAEMON specify the regular expression of daemon names this command must attach to. A daemon name is a long pathname like

 /tmp/gxp-<user>-default/gxpd-...

and it is actually a UNIX domain socket the daemon listens to to get commands from gxpc.

Upon startup, gxpc determines the associated session and daemon in the following way.

  1. Unless the argument to --create_session is 1, it first searches for existing sessions given by --session or GXP_SESSION (default is to search for everything, or equivalent to giving '.*'). It will find session files in which the given regular expression is found. For example, the session name /tmp/gxp-tau-default/gxpsession-monako-tau-2008-07-20-11-41-56-7978-20058730 will be found by any of the following.
      --session 2008-07-20-11
      --session 20058730
    1. If more than one matching sessions are found, gxpc signals an error and reports existing sessions. Consider using more specific regular expressions.
    2. If exactly one matching session is found, it uses that session.
    3. If no matching sessions are found,
      1. gxpc raises an error if the argument to --create_session is -1.
      2. otherwise gxpc will create a new session, whose detailed process is described later.
  2. Once the session is determined one way or another, gxpc tries to connect to the daemon associated with that session.

Here is the process to create a new session.

  1. Unless the argument to --create_daemon is 1, it first searches for existing daemons given by --daemon or GXP_DAEMON (default is to search for everything, or equivalent to giving '.*'). It will find daemon files in which the given regular expression is found.
    1. If more than one matching daemons are found, gxpc signals an error and reports existing daemons. Consider using more specific regular expressions.
    2. If exactly one matching daemon is found, it uses that daemon and create a new session associated with it.
    3. If no matching daemons are found,
      1. gxpc raises an error if the argument to --create_daemon is -1.
      2. otherwise gxpc will create a new daemon.

Other Related Commands

  • 'gxpc stat' (or simply gxpc without commands) will print the session name it decides to work with.
  • 'gxpc quit' will terminate daemons by default. If multiple sessions are associated with that daemon, the other sessions will find daemons suddenly disappear.
  • Instead, 'gxpc quit --session_only' will only destroy the session, but leave the daemons alive.

トップ   編集 バックアップ 添付 複製 名前変更 リロード   新規 一覧 最終更新   最終更新のRSS
© 2007 Taura lab. All Rights Reserved.
Powered by Pukiwiki, styled by Kei_ / Last-modified: 2016-07-12 (火) 15:12:44 (856d)