Last active
January 3, 2020 02:10
-
-
Save lesstif/93da213e6abed79841e181f12064c96e to your computer and use it in GitHub Desktop.
add or remove daemon server on systemd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function usage { | |
echo "USAGE: $0 param.."; | |
echo -e "\t-s SERVICE: service full path"; | |
echo -e "\t-d DESCRIPTION: service desc"; | |
echo -e "\t-r RUNAS : specify the Unix user/group of processes"; | |
echo -e "\t-p PARAM: param to pass service running"; | |
echo -e "\t-t TARGET: param to pass target(default multi-user)"; | |
echo "" | |
echo "EXMAPLE:" | |
echo -e "\t $0 -s /usr/local/bin/minio -d \"my minio server\" \ | |
-p \"--server /var/opt/storage\" -r myloginid:mygroup" \ | |
-t \"multi-user\" ; | |
exit 0; | |
} | |
if [ "$(id -u)" != "0" ]; then | |
echo "ERROR: This script must be run as root." 1>&2 | |
exit 1 | |
fi | |
SYSTEMD_DIR=/etc/systemd/system/ | |
if [ "$#" -lt 1 ]; then | |
echo "$# is Illegal number of parameters." 1>&2 | |
usage; | |
exit 1; | |
fi | |
PARAM="s:d:p:hr:t:"; | |
OPT_RUNAS_USER=root | |
OPT_RUNAS_GROUP=root | |
OPT_TARGET="multi-user" | |
while getopts $PARAM opt; do | |
case $opt in | |
s) | |
#echo "-s option was supplied. OPTARG: $OPTARG" >&2 | |
OPT_SERV=$OPTARG; | |
;; | |
d) | |
#echo "-d option was supplied. OPTARG: $OPTARG" >&2 | |
OPT_DESC=$OPTARG; | |
;; | |
p) | |
#echo "-p option was supplied." >&2 | |
OPT_PARAM=$OPTARG; | |
;; | |
r) | |
#echo "-p option was supplied." >&2 | |
OPT_RUNAS_USER=`echo $OPTARG | cut -d':' -f1`; | |
OPT_RUNAS_GROUP=`echo $OPTARG | cut -d':' -f2`; | |
;; | |
t) | |
OPT_TARGET=$OPTARG; | |
;; | |
h) | |
usage; | |
;; | |
*) | |
## default | |
usage; | |
exit 1; | |
esac | |
done | |
SERVICE_NAME=`basename ${OPT_SERV}` | |
## systemd name pattern is /etc/systemd/system/${SERVICE_NAME}.service | |
## e.g: /etc/systemd/system/minio.service | |
SYSTEMD_CONF="${SYSTEMD_DIR}/${SERVICE_NAME}.service" | |
## need to pass options to the docker container | |
BLOCK=" | |
## see https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files | |
[Unit] | |
Description=${OPT_DESC} | |
#Requires=docker.service | |
After=syslog.target network.target | |
### your service must be started after the this service is ready. | |
#After=docker.service | |
[Service] | |
### This service type is used when the service forks a child process, exiting the parent process almost immediately. | |
### for example, atlassian's bamboo. default value is simple | |
## Type=forking | |
### systemd will be restart this service if the program exists. | |
#Restart=always | |
ExecStart=${OPT_SERV} ${OPT_PARAM} | |
#ExecStop=/usr/bin/docker rm -f %i | |
User=${OPT_RUNAS_USER} | |
Group=${OPT_RUNAS_GROUP} | |
## no limit. | |
LimitNOFILE=infinity | |
LimitMEMLOCK=infinity | |
### Environment Variable | |
##Environment=SOME_ENV_KEY=ENV_VALUE | |
[Install] | |
WantedBy=${OPT_TARGET}.target | |
" | |
echo "created ${SYSTEMD_CONF}" | |
echo "${BLOCK}" > "${SYSTEMD_CONF}" | |
systemctl daemon-reload | |
echo "enable ${SERVICE_NAME}.service" | |
systemctl enable "${SERVICE_NAME}.service" | |
echo "start ${SERVICE_NAME}.service" | |
systemctl start "${SERVICE_NAME}.service" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elastic kibana 를 서비스에 등록하는 방법
bash -x systemd-service-contrl.sh -s /opt/elastic-search/kibana-7.4.2-linux-x86_64/bin/kibana -r lesstif:lesstif -d "Kibana server"