Add freeze, frozen_shell, hcoop-kprop.
[hcoop/scripts.git] / bin / run-in-pagsh
1 #!/usr/bin/pagsh.openafs
2 # -*- Shell-Script -*-
3 #
4 # Usage:
5 #
6 # run-in-pagsh [--fg] name command [argument ...]
7 #
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.
11 #
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:
20 #
21 # run-in-pagsh interchange ~/interchange/bin/interchange
22 #
23 # run-in-pagsh --fg clean-mail ~/scripts/clean-mail
24 #
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
29 # Allow user to specify "--fg" argument.
30 if test "$1" = "--fg"; then
31 shift
32 BGFLAG=
33 else
34 BGFLAG=-b
35 fi
36
37 # Sanity checks.
38 if test -z "$2"; then
39 echo "Error: not enough arguments."
40 echo "Usage: run-in-pagsh [--fg] name command [argument ...]"
41 exit 1
42 elif test ! -d "$HOME/.run"; then
43 echo "Error: the ~/.run directory must exist before running this script."
44 exit 1
45 fi
46
47 # The distinct name of this command.
48 NAME=$1
49
50 # The command to run.
51 shift
52 CMD="$*"
53
54 # Try to deduce the user we're running as.
55 if test -z "$USER"; then
56 if test -n "$LOGNAME"; then
57 USER=$LOGNAME
58 elif test -n "$HOME"; then
59 USER=$(basename "$HOME")
60 else
61 echo "Error: cannot deduce your username"
62 exit 1
63 fi
64 fi
65
66 # Use a different PID file for each program.
67 K5PID=$HOME/.run/$NAME.pid
68
69 # Your keytab file.
70 KTAB=/etc/keytabs/user.daemon/$USER
71
72 # Credentials cache: we want this to be distinct, so that running
73 # kdestroy and unlog do not wipe out the tickets for your session, if
74 # running this manually.
75 CACHE=/tmp/run-in-pagsh.$USER.$NAME
76
77 # Terminate current k5start process, if one is running.
78 if test -f "$K5PID"; then
79 kill `cat -- "$K5PID"` 2>/dev/null
80 fi
81
82 # Destroy old tokens.
83 KRB5CCNAME=FILE:$CACHE /usr/bin/kdestroy 2>/dev/null || :
84 KRB5CCNAME=FILE:$CACHE /usr/bin/unlog
85
86 # Remove old PID file
87 if test -f "$K5PID"; then
88 /usr/bin/k5start -qtU -k "$CACHE" -f "$KTAB" -- rm -f "$K5PID"
89 fi
90
91 if test -n "$BGFLAG"; then
92
93 # Start fresh k5start daemon which will be refreshing tickets
94 # every 540 min.
95 /usr/bin/k5start -U -b -K 540 -k "$CACHE" -t -p "$K5PID" -f "$KTAB"
96
97 # Run actual service with arguments as provided on command line.
98 $CMD
99
100 else
101
102 # Run the command once in the foreground, cleaning up its PID
103 # file when done.
104 /usr/bin/k5start -U -K 540 -k "$CACHE" -t -p "$K5PID" -f "$KTAB" -- $CMD
105 /usr/bin/k5start -qtU -k "$CACHE" -f "$KTAB" -- rm -f "$K5PID"
106 fi