forked from ragadeeshu/mp3printer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-service.sh
More file actions
executable file
·116 lines (102 loc) · 2.57 KB
/
install-service.sh
File metadata and controls
executable file
·116 lines (102 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh -e
[ -x "$(which systemctl)" ] || {
echo "systemctl not found. Are you not using Systemd?" >&2
exit 1
}
args=$(getopt -o g:h --long group:,help -- "$@")
eval set -- "$args"
printHelp() {
echo "Usage: $0 [-h|--help] [-g|--group <GROUP>] [-- <ARGS>]"
echo
echo "Install mp3 printer as a system service, running as the current user."
echo
echo "Arguments:"
echo " -h|--help Show this help and exit."
echo " -g|--group <GROUP> Run as the specified (by name or id) group, allowing said"
echo " group write access to the STDIN socket (for commands)."
echo " -- <ARGS> Run the service with the specified arguments."
}
dir=$(dirname $(realpath $0))
user=$(id -un)
uid=$(id -u)
group=$(id -gn)
gid=$(id -g)
stdin_mode=0200
while [ -n "$1" ]; do
case "$1" in
-g|--group)
getent=$(getent group $2)
group=$(echo $getent | cut -f 1 -d:)
gid=$(echo $getent | cut -f 3 -d:)
stdin_mode=0220
shift 2
;;
-h)
printHelp
exit 0
;;
--)
shift
break
;;
*)
echo "Unknown option '$1'!" >&2
printHelp
exit 1
;;
esac
done
args=
for x in "$@"; do
case "$x" in
*\ *|*\ *)
x="\"$(printf "%s\n" "$x" | sed -e 's/\\/\\\\\\\\/g' -e 's/"/\\"/g')\""
;;
esac
args="$args $x"
done
echo "# Will install service file for running mp3 printer with these settings:"
echo " - Working dir: $dir"
echo " - User: $user ($uid)"
echo " - Group: $group ($gid)"
echo " - Arguments:${args:- (none)}"
echo
printf "Continue? [y/N] "
read inp
[ "$inp" = "y" -o "$inp" = "Y" ] || exit 1
echo "# Stopping any running instance of the service..."
sudo systemctl stop mp3printer.service > /dev/null 2>&1 || :
echo "# Installing service file..."
sudo tee /etc/systemd/system/mp3printer.service > /dev/null << EOF
[Unit]
After=network-online.target
Description=mp3 Printer
[Service]
User=$uid
Group=$gid
WorkingDirectory=$dir
ExecStart=$(which python3) main.py$args
Sockets=mp3printer.socket
StandardInput=socket
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
echo "# Installing STDIN socket file..."
sudo tee /etc/systemd/system/mp3printer.socket > /dev/null << EOF
[Unit]
Description=mp3 Printer STDIN
PartOf=mp3printer.service
[Socket]
ListenFIFO=$dir/service.stdin
Service=mp3printer.service
SocketUser=$uid
SocketGroup=$gid
SocketMode=$stdin_mode
RemoveOnStop=yes
EOF
sudo systemctl daemon-reload
sudo systemctl enable mp3printer.service
echo "# Starting mp3printer service..."
sudo systemctl start mp3printer.service