-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-service.sh
More file actions
52 lines (44 loc) · 1.29 KB
/
create-service.sh
File metadata and controls
52 lines (44 loc) · 1.29 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
#!/bin/bash
# TODO: I didn't test it:
export USER_NAME=my_name
export GROUP_NAME=${USER_NAME}
export WORKING_DIR=/home/my_name/my_project_name
export SERVICE_NAME=my_project_name
export FILE=index.ts
# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -qs "dash"; then
echo "This script needs to be run with bash, not sh"
exit 1
fi
if [[ "$EUID" -ne 0 ]]; then
echo "Sorry, you need to run this as root"
exit 2
fi
echo "Good for you?: user/group: $USER_NAME/$GROUP_NAME, service:$SERVICE_NAME, working dir: $WORKING_DIR, server:$FILE"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "creating file"; break;;
No ) echo "please update the file with correct informations"; exit;;
esac
done
echo "
[Unit]
Description=$SERVICE_NAME
[Service]
PIDFile=/tmp/$SERVICE_NAME.pid
User=$USER_NAME
Group=$GROUP_NAME
Restart=always
KillSignal=SIGQUIT
WorkingDirectory=$WORKING_DIR
ExecStart=$WORKING_DIR/node_modules/.bin/ts-node $FILE
[Install]
WantedBy=multi-user.target
" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
sudo systemctl enable ${SERVICE_NAME}
echo "
# Now you can use the following command:
sudo systemctl stop $SERVICE_NAME.service
sudo systemctl start $SERVICE_NAME.service
sudo journalctl -fu $SERVICE_NAME.service
"