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
Linux下设置python脚本文件为服务的更多相关文章
- Linux下设置svn过滤文件类型
1)修改客户端. 1.修改客户端 1)编辑文件家目录下自己账户下的.subversion/config文件 vim ~/.subversion/config 2)找到包含[miscellany]的一行 ...
- linux下设置计划任务执行python脚本
linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...
- linux下生成core dump文件方法及设置
linux下生成core dump文件方法及设置 from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...
- windows下建立文件的换行符^M导致linux下的shell脚本执行错误的解决方式
常常在windows下编辑的文件远程传送到linux下的时候每行末尾都会出现^M.这将导致shell脚本执行错误,主要是由于dos下的编辑器和linux下的编辑器对文件末行的回车符处理不一致导致. 主 ...
- linux下为目录和文件设置权限
摘:linux下为目录和文件设置权限 分类: Linux2012-05-09 03:18 7456人阅读 评论(1) 收藏 举报 linuxwordpressweb数据库serverfile linu ...
- Linux下查看Python安装了哪些脚本模块
Linux下查看Python安装了哪些脚本模块 1.什么是rpm ? rpm 即RedHat Package Management,是RedHat的发明之一 .现在包括OpenLinux.fedora ...
- Linux下shell通用脚本启动jar(微服务)
Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...
- 【Linux】windows下编写的脚本文件,放到Linux中无法识别格式
注意:我启动的时候遇到脚本错误 » sh startup.sh -m standalone tanghuang@bogon : command not found : command not foun ...
- Linux下定时执行脚本(转自Decode360)
文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog 老师(业精于勤而荒于嬉 ...
随机推荐
- mybais 之parameterType ="list"
<!-- 根据货品编号获取商品价格和库存 --> <select id="getGoodsPriceAndStockByGoodsNo" resultMap=&q ...
- phpQuery的用法
一.phpQuery的hello word! 下面简单举例: include 'phpQuery.php'; phpQuery::newDocumentFile('http://www.phper.o ...
- java 对 汉字排序(按照拼音字母排序)
业务场景: 一个list集合,里面add了若干个实体类,针对该实体类排序的属性为String. 使用技术,自定义list排序(JDK自带),重写Comparator接口的compare方法,汉字转拼音 ...
- bigdecimal 与long int 之间转换
BigDecimal与Long.int之间的互换 在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigD ...
- 【转载】Yui.Compressor高性能ASP.NET开发:自动压缩CSS、JS
在开发中编写的js.css发布的时候,往往需要进行压缩,以减少文件大小,减轻服务器的负担.这就得每次发版本的时候,对js.js进行压缩,然后再发布.有没有什么办法,让代码到了服务器上边,它自己进行压缩 ...
- WP8.1学习系列(第十七章)——交互UX之输入和反馈模式
如果你将 Windows 应用商店应用设计为触摸交互,则可免费获取对触摸板.鼠标.笔和键盘交互的支持.你的用户可以从一种输入法切换到另一种,而不会丧失应用体验的感觉.将键盘插入平板电脑?没问题.你的应 ...
- VC++生成不同的随机数
其用法是先调用srand函数,如 srand( (unsigned)time( NULL ) ) 这样可以使得每次产生的随机数序列不同.假如计算伪随机序列的初始数值(称为种子)相同,则计算出来的伪随机 ...
- 关于 CommonJS AMD CMD UMD 规范的差异总结(转)
根据CommonJS规范,一个单独的文件就是一个模块.每一个模块都是一个单独的作用域,也就是说,在一个文件定义的变量(还包括函数和类),都是私有的,对其他文件是不可见的. // foo.js var ...
- ngnix +tomcat7 简单配置
1.下载tomcat7 修改conf 文件夹下的server.xml文件 1.<Server port="8205" shutdown="SHUTDOWN" ...
- [原]openstack-kilo--issue(三) openstack-nova-issue-systemctl start openstack-nova-compute.service
本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. execute systemctl start openstack-nova-c ...