run-in-pagsh improvements
[hcoop/scripts.git] / run-in-pagsh
CommitLineData
46a93550 1#!/usr/bin/pagsh.openafs
92bce8a1 2# -*- Shell-Script -*-
46a93550 3#
4# Usage:
5#
9bc6a43d 6# run-in-pagsh [--fg] name command [argument ...]
92bce8a1 7#
9bc6a43d 8# The argument `name' is a short description of the program to call,
9# without any spaces. It is used for making a PID file, which ensures
10# that only one instance of your command is running.
46a93550 11#
9bc6a43d 12# The arguments that come afterward specify the command to run and its
13# arguments, if any.
14#
15# If the command will run in the foreground, then you should use the
16# `--fg' argument, making sure that it is the very first argument to
17# run-in-pagsh.
18#
19# Examples:
46a93550 20#
92bce8a1 21# run-in-pagsh interchange ~/interchange/bin/interchange
46a93550 22#
9bc6a43d 23# run-in-pagsh --fg clean-mail ~/scripts/clean-mail
24#
46a93550 25# Make sure that the ~/.run directory exists and is writable.
26# See http://wiki2.hcoop.net/MemberManual/RunningUnattendedCommands
27# for instructions on how to accomplish this.
28
9bc6a43d 29# Allow user to specify "--fg" argument.
30if test "$1" = "--fg"; then
31 shift
32 BGFLAG=
33else
34 BGFLAG=-b
35fi
36
37# Sanity checks.
92bce8a1 38if test -z "$2"; then
39 echo "Error: not enough arguments."
9bc6a43d 40 echo "Usage: run-in-pagsh name [--fg] command [argument ...]"
46a93550 41 exit 1
5aa75225 42elif test ! -d "$HOME/.run"; then
46a93550 43 echo "Error: the ~/.run directory must exist before running this script."
44 exit 1
45fi
46
47# Use a different PID file for each program.
5aa75225 48K5PID=$HOME/.run/$1.pid
46a93550 49
50# The command to run.
92bce8a1 51shift
46a93550 52CMD="$*"
53
54# Your keytab file.
55KTAB=/etc/keytabs/user.daemon/$USER
56
57# Terminate current k5start process, if one is running.
58if test -f "$K5PID"; then
9bc6a43d 59 kill `cat -- "$K5PID"` 2>/dev/null
1e515d3d 60 /usr/bin/k5start -qtU -f "$KTAB" -- rm -f "$K5PID"
46a93550 61fi
62
9bc6a43d 63if test -n "$BGFLAG"; then
46a93550 64
9bc6a43d 65 # Start fresh k5start daemon which will be refreshing tickets
66 # every 540 min.
67 /usr/bin/k5start -U -b -K 540 -t -p "$K5PID" -f "$KTAB"
68
69 # Run actual service with arguments as provided on command line.
70 $CMD
71
72else
73
74 # Run the command once, in the foreground, cleaning up its PID
75 # file when done.
76 /usr/bin/k5start -U -K 540 -t -p "$K5PID" -f "$KTAB" -- $CMD
77 /usr/bin/k5start -qtU -f "$KTAB" -- rm -f "$K5PID"
78fi