创建systemd服务和自动创建环境

代码:

#!/bin/bash

workdir=`cd "$(dirname "$0")";pwd`
venv_dir=venv
tuna=https://pypi.tuna.tsinghua.edu.cn/simple
azkaban=/usr/lib/systemd/system/azkaban.service


if ! [ -x "$(command -v python3)" ]; then echo "There is no python3." exit 1; fi
if ! [ -x "$(command -v pip3)" ]; then echo "There is no pip3." exit 1; fi

cd $workdir

# 创建虚拟环境。
if ! [ -d $venv ]; then
    if ! $(python -c "import virtualenv"); then pip3 install -i $tuna virtualenv ; fi
fi
virtualenv venv
source venv/bin/activate
pip install -i $tuna -r requirements.txt && deactivate

systemctl status azkaban &>/dev/null

if [ $? -eq 0 ]; then systemctl stop azkaban; fi

cat > $azkaban << EOF 
[Unit]
Description=Azkaban Python Api
After=syslog.target

[Service]
User=root
Group=root
WorkingDirectory=$workdir
# ExecStartPre=source $workdir/venv/bin/activate
ExecStart=$workdir/venv/bin/python3 server.py
# ExecStopPost=deactivate
Restart=always
StandardError=syslog

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload
systemctl start azkaban

posted @ 2022-01-12 13:55  咕咚!  阅读(266)  评论(2编辑  收藏  举报