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 ]; then
26 echo "Session daemon PIDDIR not there: $PIDDIR" >&2
27 exit 2
28 fi
29
30 mkdir -p $LOGDIR
31 if [ $? -ne 0 ]; then
32 echo "Session daemon LOGDIR not there: $LOGDIR" >&2
33 exit 2
34 fi
35
36 if [ -e "$PIDFILE" ] ; then
37 PID=`cat "$PIDFILE"`
38 echo "Session daemon '$NAME' PID found: $PID"
39
40 if pgrep -F "$PIDFILE" "${NAME}" >/dev/null ; then
41 echo "Session daemon '$NAME' was already running with PID $PID" >&2
42 exit 3
43 fi
44 else
45 echo "Session daemon '$NAME' PIDFILE NOT found: $PIDFILE"
46 fi
47
48
49 echo "Session daemon running $COMMAND $* > $LOGFILE 2>&1"
50 $COMMAND $* > "$LOGFILE" 2>&1 &
51 PID=$!
52 ERR=$?
53 sleep 1
54
55 if [ "$PID" ] && [ $ERR == 0 ] && kill -0 "$PID" 2>/dev/null; then
56 echo -n $PID > "$PIDFILE"
57 echo "Session daemon '$NAME' started with PID $PID"
58 else
59 echo "Session daemon '$NAME' did not start or finished early. PID: $PID, ERR: $ERR" >&2
60 cat "$LOGFILE" >&2
61 exit 4
62 fi