Ubuntu搭建pykms并设置service脚本

编程

下载py-kms

mkdir mygit

git clone https://gitee.com/kwanxian/py-kms ~/mygit

设置操作脚本:start, stop, restart

cd mygit

mkdir script

cd script

vim start.sh

vim stop.sh

vim restart.sh

start.sh

#!/bin/sh -e

#/etc/rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

echo "KMS Start!"

python3 /home/"Your Name"/mygit/py-kms/pykms_Server.py > /home/"Your Name"/mygit/kms.log 2>&1 &

exit 0

stop.sh

#!/bin/sh -e

#/etc/rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

echo "Stop KMS!"

PID=$(ps -ef | grep "python3" | grep "kms" | grep -v grep | awk "{print $2}")

if ps -p $PID > /dev/null

then

kill -9 $PID

echo "KMS Stopped!"

# Do something knowing the pid exists, i.e. the process with $PID is running

else

echo "KMS Not Running!"

fi

exit 0

restart.sh

#!/bin/sh -e

#/etc/rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

echo "Restart KMS!"

PID=$(ps -ef | grep "python3" | grep "kms" | grep -v grep | awk "{print $2}")

if ps -p $PID > /dev/null

then

kill -9 $PID

echo "KMS Stopped!"

python3 /home/"Your Name"/mygit/py-kms/pykms_Server.py > /home/"Your Name"/mygit/kms.log 2>&1 &

echo "KMS Started!"

else

echo "KMS Not Running!"

python3 /home/"Your Name"/mygit/py-kms/pykms_Server.py > /home/"Your Name"/mygit/kms.log 2>&1 &

echo "KMS Started!"

fi

exit 0

设置service脚本

cd /etc/systemd/system/

sudo vim pykms.service

[Unit]

Description=KMS

After=network.target

[Service]

Type=forking

# run script

ExecStart=/bin/sh /home/"Your Name"/mygit/script/start.sh start

# stop script

ExecStop=/bin/sh /home/"Your Name"/mygit/script/stop.sh stop

# restart.sh script

ExecReload=/bin/sh /home/"Your Name"/mygit/script/restart.sh restart

RemainAfterExit=yes

User="Your Name"

Group="Your Name"

[Install]

WantedBy=multi-user.target

设置service

# 初始化脚本

sudo systemctl daemon-reload

# 启动

sudo systemctl start pykms.service

# 停止

sudo systemctl stop pykms.service

# 重启

sudo systemctl restart pykms.service

# 开机启动

sudo systemctl enable pykms.service

 

以上是 Ubuntu搭建pykms并设置service脚本 的全部内容, 来源链接: utcz.com/z/514833.html

回到顶部