Linux下设置python脚本文件为服务
(最简单的方式nohup python xxx.py)
-------------------------------------------------------------------------------------------------------------------
Python脚本开机自动运行;本帖适用于使用systemd的Linux系统,现在流行的Linux发行版都使用systemd。
后台服务程序是随系统自启动的,我们只要把Python脚本配置为服务就行了。需要注意的一点是你Python脚本的启动时机,它依赖不依赖其他服务(网络连接、一些分区的挂载等等)。
#1 Python脚本
一个你要自启动的Python脚本,我使用 /home/snail/autorun.py为例。
#2 创建Unit配置文件
1
|
$ sudo vim /lib/systemd/system/autorun.service
|
写入如下内容:
1
2
3
4
5
6
7
8
9
10
|
[Unit]
Description=Test Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/snail/autorun.py
[Install]
WantedBy=multi-user.target
|
上面定义了一个叫 Test Service 的服务,它在multi-user环境起来之后运行;ExecStart参数指定我们要运行的程序;idle确保脚本在其他东西加载完成之后运行,它的默认值是simple。
注意使用绝对路径。
为了获得脚本的输出信息,我们可以重定向到文件:
1
|
ExecStart=/usr/bin/python /home/snail/autorun.py > /home/snail/autorun.log 2>&1
|
更改配置文件的权限:
1
|
$ sudo chmod 644 /lib/systemd/system/autorun.service
|
#3 使配置文件生效
1
2
|
$ sudo systemctl daemon-reload
$ sudo systemctl enable autorun.service
|
#4 重启
1
|
$ sudo reboot
|
#5 查看服务状态
1
|
$ sudo systemctl status autorun.service
|
#6 服务操作命令
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
任务 | 旧指令 | 新指令 |
使某服务自动启动 | chkconfig --level 3 httpd on | systemctl enable httpd.service |
使某服务不自动启动 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active) |
显示所有已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
停止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
实例
1.启动nfs服务
systemctl start nfs-server.service
2.设置开机自启动
systemctl enable nfs-server.service
3.停止开机自启动
systemctl disable nfs-server.service
4.查看服务当前状态
systemctl status nfs-server.service
5.重新启动某服务
systemctl restart nfs-server.service
6.查看所有已启动的服务
systemctl list -units --type=service
开启防火墙22端口
iptables -I INPUT -p tcp --dport 22 -j accept
如果仍然有问题,就可能是SELinux导致的
关闭SElinux:
修改/etc/selinux/config
文件中的SELINUX=””
为disabled,然后重启。
彻底关闭防火墙:
sudo systemctl status firewalld.service sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
转载于:https://www.cnblogs.com/fu-yong/p/9683129.html
Linux下设置python脚本文件为服务相关推荐
- python写linux脚本_Linux下设置python脚本文件为服务
(最简单的方式nohup python xxx.py) ------------------------------------------------------------------------ ...
- linux 定时运行脚本_教你如何在Linux下让Python脚本自启动和定时启动
本文简单介绍如何在Linux下设置Python脚本自启动和定时启动. 一.Python开机自动运行 假如Python自启动脚本为auto.py.那么用root权限编辑以下文件: sudo vim /e ...
- linux下执行shell脚本文件,Linux下使用shell脚本自动执行脚本文件
搜索热词 以下实例本人在Centos6.5 64位操作系统中使用 一.定时复制文件 a.在/usr/local/wfjb_web_back目录下创建 tomcatBack.sh文件 #将tomcat中 ...
- linux运行python脚本语句,如何在Linux下运行Python脚本
1.使用python的IDEL运行python 如果你的Linux安装了python,Ctrl+Alt+T打开Terminal后输入指令:python,会出现>>>,这个时候就可以在 ...
- linux下新建一个脚本文件,linux下新建并启动脚本文件
1.书写一个linux脚本文件: 2. 修改3个参数:Name,服务名称: Path:路径: Assembly:具体应用的程序: 3.进入目录/etc/init.d# (Linux启动项目): 4.将 ...
- 如何在Linux下运行Python脚本
1.使用python的IDEL运行python 如果你的Linux安装了python,Ctrl+Alt+T打开Terminal后输入指令:python,会出现>>>,这个时候就可以在 ...
- linux下执行python脚本并传入参数_实现在Java中执行python代码或py脚本(可传参数)...
写在前面 最近用Java写服务端程序时,遇到这样一个需求:需要把用python写的机器学习算法部署到服务器上,然后Java执行py脚本,并且取得算法执行的结果.在网上找了很久,有些跑不通,有些是基于w ...
- linux 下 `dirname $0` 脚本文件放置的目录
http://www.cnblogs.com/xupeizhi/archive/2013/02/19/2917644.html linux 下 `dirname $0` 在命令行状态下单纯执行 $ c ...
- Linux下安装Python,并在Linux下运行python代码文件
CentOS配置源 1.查看本地yum源 ll /etc/yum.repos.d/ 2.把默认yum源备份 mkdir /opt/centos-yum.bak mv /etc/yum.repos.d/ ...
最新文章
- HTML-CSS-JS-PHP的顺序及相关网址(转)
- python起步输入-[转载]python核心编程学习笔记-python起步
- macbook下载苹果版Photoshop cc2019 for mac
- 查看oracle系统信息,查看 ORACLE 系统级信息
- linux拒绝tcp链接,Linux 内核 TCP SACK 拒绝服务问题
- java sar包_linux下查看最占性能的JAVA进程
- 梦幻飞屋创意房产海报,温馨画面,梦中的家园
- 今天讲座的感悟--java
- AssemblyInfoTask-new
- 学习python:实例属性和类属性
- 下行文格式图片_下行文页面格式设置
- IOUtils常用方法的使用
- Labview2019安装
- CLUSTERDOWN Hash slot not served 记录一次线上redis插槽问题
- 国家官宣!硕士补贴30W、本科补贴20W!一线城市户口,最高5W生活补贴丨毕业去这些城市,太太太爽了!...
- Nosql - redis 的学习
- Linux 在bash.bashrc中添加 一个目录
- 戴尔R710服务器修改风扇转速,dell 服务器手动降低风扇转速,解决dell风扇转速高,噪音大问题...
- php 数组函数特点,php常见数组函数
- Linux 管道操作符详解