python脚本在centos7下作为服务运行
1.在此目录下创建服务文件
/usr/lib/systemd/system/xxx.service
2.xxx.service文件内容为
[Unit
]
Description
=erpServer Service
After
=multi-user.target
[Service
]
Type
=idle
Restart
=on-failure
PIDFile
=/tmp/erpServer.pid
ExecStart
=/usr/local/conda3/envs/ERPServer/bin/python3.7 /usr/local/ERPServer/Main.py
ExecStop
='ps -ef |grep erpServer |awk '{print
$2}' | xargs kill -9 '
RestartSec
=10s
[Install
]
WantedBy
=multi-user.target
3.更改配置文件的权限
sudo chmod 644 /usr/lib/systemd/system/myddns.service
4.使配置文件生效
sudo systemctl daemon-reload
sudo systemctl
enable myddns.service
sudo systemctl start myddns.service
sudo systemctl status myddns.service
或者重启查看能否自启动。
reboot
以下为原文:
首先登入centos 7 ,然后输入命令:
sudo vim /usr/lib/systemd/system/myddns.service
将python脚本添加为centos7服务
进入命令后输入i 进入插入模式
复制粘贴以下内容:
[Unit
]
Description
=myddns Service
After
=multi-user.target
[Service
]
Type
=idle
ExecStart
=/root/.pyenv/versions/3.7.2/bin/python /data/own/ddns.py
[Install
]
WantedBy
=multi-user.target
说明:
[Unit
]
Description
=myddns Service
After
=multi-user.target
[Service
]
Type
=idle
ExecStart
=/root/.pyenv/versions/3.7.2/bin/python /data/own/ddns.py
[Install
]
WantedBy
=multi-user.target
将python脚本添加为centos7服务
4
保存并退出。
按ESC键推出插入模式,然后输入:wq 保存并退出
5
更改配置文件的权限:
sudo chmod 644 /usr/lib/systemd/system/myddns.service
6
使配置文件生效
sudo systemctl daemon-reload
sudo systemctl
enable myddns.service
sudo systemctl start myddns.service
sudo systemctl status myddns.service
或者重启查看能否自启动。
reboot