(最简单的方式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下设置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 老师(业精于勤而荒于嬉 ...
随机推荐
oracle 编译中一个关于clntsh 库的一个 帖子 ,收藏!
oracle 编译中一个关于clntsh 库的一个 帖子 ,收藏! ------------------------------------------------------------------ ...
oneThink安装出错解决
在Wampserver3.0.0(apache2.4.17+php5.6.15+mysql5.7.9)版本中oneThink安装用1.1github版,不要用1.1开发版,不然安装的时候数据库导入时b ...
两个异步处理AsyncTask和Handler的优缺点
AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操 ...
【原】android通过adb wireless的使用
开发android程序,总是需要插拔插拔的,usb口都给弄坏掉了,现在adb可以通过无线网,链接处在同一个局域网下面的android设备 1.将电脑和移动设备链接到同一个无线网下 2.在android ...
apache下配置虚拟主机案例详解
案例说明: 域名: www.tianyik.com /var/html/www blog.tianyik.com /var/html/blog bb ...
HDU1007 Quoit Design 【分治】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
个性化WinPE封装方法----制作过程需要了解的“命令”
1.在现有的Windows7条件下,自动在E盘建立mywinpe文件夹,设置 Windows PE 构建环境,并保存到E:\mywinpe下 copype.cmd x86 E:\mywinpe 2.将 ...
Flask消息验证与提示
一,消息提示基本语法. 1,先新建一个Flask工作空间. 2,新建后自动得到一个app.py文件,直接运行可以看到基本效果.然后引入 from flask import flash.使用这个flas ...
vs编译过程中出的错
如果运行时出现这样的错,关闭正在运行的.exe文件,即可正常运行
vue中的axios封装
import axios from 'axios'; import { Message } from 'element-ui'; axios.defaults.timeout = 5000;axios ...