bib2html.py: Add incollection bibtype
[shutils.git] / xsession-daemon
1 #!/bin/sh
2 # Stolen from and based on Robert Cutajar
3 # https://askubuntu.com/a/421718
4
5 if [ "$#" == "0" ] ;
6 then
7 echo "Session daemon missing command" >&2
8 echo "USAGE: $0 command [args...]"
9 echo "Executes given command with args only once per XDG seat (session)."
10 echo "Keeps PID and log of the executed command."
11 exit 1
12 fi
13
14 COMMAND=$1
15 shift
16
17 NAME="${COMMAND##*/}"
18 PIDFILE=~/.cache/xsession-daemon/$NAME-$XDG_SEAT.pid
19 LOGFILE=~/.cache/xsession-daemon/$NAME-$XDG_SEAT.log
20 PID=""
21 PIDDIR="${PIDFILE%/*}"
22 LOGDIR="${LOGFILE%/*}"
23
24 mkdir -p $PIDDIR
25 if [ $? -ne 0 ];
26 then
27 echo "Session daemon PIDDIR not there: $PIDDIR" >&2
28 exit 2
29 fi
30
31 mkdir -p $LOGDIR
32 if [ $? -ne 0 ];
33 then
34 echo "Session daemon LOGDIR not there: $LOGDIR" >&2
35 exit 2
36 fi
37
38 if [ -e "$PIDFILE" ] ;
39 then
40 echo "Session daemon '$NAME' PIDFILE found: $PIDFILE"
41 PID=`cat "$PIDFILE"`
42 echo "Session daemon '$NAME' PID found: $PID"
43 else
44 echo "Session daemon '$NAME' PIDFILE NOT found: $PIDFILE"
45 fi
46
47 if [ "$PID" ] && kill -0 "$PID" 2>/dev/null ;
48 then
49 echo "Session daemon '$NAME' was already running with PID $PID" >&2
50 exit 3
51 fi
52
53 echo "Session daemon running $COMMAND $* > $LOGFILE 2>&1"
54 $COMMAND $* > "$LOGFILE" 2>&1 &
55 PID=$!
56 ERR=$?
57 sleep 1
58
59 if [ "$PID" ] && [ $ERR == 0 ] && kill -0 "$PID" 2>/dev/null;
60 then
61 echo -n $PID > "$PIDFILE"
62 echo "Session daemon '$NAME' started with PID $PID"
63 else
64 echo "Session daemon '$NAME' did not start or finished early. PID: $PID, ERR: $ERR" >&2
65 cat "$LOGFILE" >&2
66 exit 4
67 fi