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 老师(业精于勤而荒于嬉 ...
随机推荐
- 目前学习.net时间让我摸不着头脑的事情
呜呜,不太喜欢做笔记,只喜欢把自己不懂的和预习时间有麻烦的简单记下,所以这里也是能可以让我写下我们的学习点滴··· 今天学习了<1>.变量的作用域,在想是不是之前听过的局部变量和全局变量? ...
- Java读取maven目录下的*.properties配置文件
public class ReadProperties{ private static String proFileName = "/config/MQSubjectId.propertie ...
- MegaCli 使用
安装: wget ftp://rpmfind.net/linux/Mandriva/devel/cooker/x86_64/media/non-free/release/megacli-8.02.21 ...
- Android开发训练之第五章第三节——Transferring Data Without Draining the Battery
Transferring Data Without Draining the Battery GET STARTED DEPENDENCIES AND PREREQUISITES Android 2. ...
- Android开发训练之第五章——Building Apps with Connectivity & the Cloud
Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...
- 【H5】使用h5实现复制粘贴功能
方案一 : 可满足大部分浏览器正常使用 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- UVA 10120 - Gift?!(搜索+规律)
Problem D. Gift?! The Problem There is a beautiful river in a small village. N rocks are arranged ...
- c++ 友元类 与 友元类派生类
定义: 当一个类B成为了另外一个类A的“朋友”时,那么类A的私有和保护的数据成员就可以被类B访问.我们就把类B叫做类A的友元. 用法: 在A类中加入: friend class B; 下面这个程序说明 ...
- smali-2.2.4.jar & baksmali-2.2.4.jar
https://bitbucket.org/JesusFreke/smali/downloads/
- javascript 闭包学习
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...