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