summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xdot/local/bin/quickserve59
1 files changed, 59 insertions, 0 deletions
diff --git a/dot/local/bin/quickserve b/dot/local/bin/quickserve
new file mode 100755
index 0000000..a07156d
--- /dev/null
+++ b/dot/local/bin/quickserve
@@ -0,0 +1,59 @@
1#!/bin/sh
2service_template()
3{
4 cat <<EOF
5[Unit]
6Description=${service_name}
7
8[Service]
9# Type=forking
10
11Environment=DISPLAY=$DISPLAY
12Environment=XAUTHORITY=$XAUTHORITY
13Environment=SSH_AUTH_SOCK=%t/keyring/ssh
14
15ExecStart=${exe} ${args}
16Restart=always
17
18[Install]
19WantedBy=default.target
20EOF
21}
22
23set -e
24exe=$1
25shift
26args=$*
27
28case "$exe" in
29 sudo) exit 1;;
30 /*) ;;
31 */*) exe=$(realpath -e "$exe") ;;
32 *) exe=$(which "$exe") ;;
33esac
34
35if [ "$(id -u)" = 0 ]; then
36 service_dir=/etc/systemd/system
37 user=
38else
39 service_dir=$HOME/.config/systemd/user
40 user=y
41fi
42
43service_name=${exe##*/}
44service_file=${service_dir}/${service_name}.service
45
46[ -d "$service_dir" ] || mkdir -p "$service_dir"
47
48systemctl="systemctl ${user:+--user}"
49
50if [ -e "$service_file" ]; then
51 "${EDITOR:-/usr/bin/vi}" "$service_file"
52 $systemctl daemon-reload
53 $systemctl status "$service_name"
54else
55 service_template > "$service_file"
56 $systemctl daemon-reload
57 $systemctl enable "$service_name"
58 $systemctl start "$service_name"
59fi