我的脚本在当前安装的目录下面名为report3.py 使用usr/bin/python3来运行可以参照下面的ExecStart 修改那里就行了
#!/bin/bash echo 当前进程id为 $$ SERVICE_NAME="report3.service" # 不能乱加空格 readonly SERVICE_NAME BASE_DIR="$(pwd)" #*************************删除旧文件*********************************# sudo rm ${SERVICE_NAME} #*************************创建report3.service*********************************# echo "[Unit] Description=gyz report ip mac to httpserver [Service] Type=simple WorkingDirectory=${BASE_DIR} Restart=on-failure ExecStart=/usr/bin/python3 ${BASE_DIR}/report3.py KillMode=process KillSignal=SIGINT TimeoutStopSec=600s [Install] WantedBy=multi-user.target Alias=${SERVICE_NAME} ">>${SERVICE_NAME} #*************************添加可执行权限*********************************# echo 正在安装${SERVICE_NAME}... service_path="${BASE_DIR}/${SERVICE_NAME}" sudo chmod 777 ${BASE_DIR}/*.* #*************************安装report3.service到系统目录*********************************# sudo ln -sf ${service_path} /lib/systemd/system/${SERVICE_NAME} #\ sudo systemctl daemon-reload sudo systemctl enable ${SERVICE_NAME} sudo systemctl start ${SERVICE_NAME} #***************************************************************# echo "结束" if [ $(ps -ef | grep -c "report" ) -ge 2 ]; #统计 then printf "服务正在启动...\n" else printf "服务正在安装失败\n" fi exit 0