(最简单的方式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脚本文件为服务的更多相关文章

  1. Linux下设置svn过滤文件类型

    1)修改客户端. 1.修改客户端 1)编辑文件家目录下自己账户下的.subversion/config文件 vim ~/.subversion/config 2)找到包含[miscellany]的一行 ...

  2. linux下设置计划任务执行python脚本

    linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...

  3. linux下生成core dump文件方法及设置

    linux下生成core dump文件方法及设置    from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...

  4. windows下建立文件的换行符^M导致linux下的shell脚本执行错误的解决方式

    常常在windows下编辑的文件远程传送到linux下的时候每行末尾都会出现^M.这将导致shell脚本执行错误,主要是由于dos下的编辑器和linux下的编辑器对文件末行的回车符处理不一致导致. 主 ...

  5. linux下为目录和文件设置权限

    摘:linux下为目录和文件设置权限 分类: Linux2012-05-09 03:18 7456人阅读 评论(1) 收藏 举报 linuxwordpressweb数据库serverfile linu ...

  6. Linux下查看Python安装了哪些脚本模块

    Linux下查看Python安装了哪些脚本模块 1.什么是rpm ? rpm 即RedHat Package Management,是RedHat的发明之一 .现在包括OpenLinux.fedora ...

  7. Linux下shell通用脚本启动jar(微服务)

    Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...

  8. 【Linux】windows下编写的脚本文件,放到Linux中无法识别格式

    注意:我启动的时候遇到脚本错误 » sh startup.sh -m standalone tanghuang@bogon : command not found : command not foun ...

  9. Linux下定时执行脚本(转自Decode360)

    文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog  老师(业精于勤而荒于嬉 ...

随机推荐

  1. Splash autoload() 方法

    autoload() 方法可以设置每个页面访问时自动加载的对象,比如自动加载 JavaScript 代码,自动加载 Ajax 代码等等 注意此方法只负责加载 JavaScript/Ajax 代码,不执 ...

  2. iOS中UIView翻转效果实现

    本文转载至  http://baishiyun.blog.163.com/blog/static/13057117920148228261747/ 新建一个view-based模板工程,在ViewCo ...

  3. [微信小程序]计算自己手机到指定位置的距离

    目的: 根据目的地的坐标计算自己手机的位置离目的地的距离的 核心思路: 后续操作必须等所有异步请求都返回了才能继续 使用 const qqmap = require("../../utils ...

  4. Linux 下如何安装 .rpm 文件

    执行以下命令安装: rpm -i your-file-name.rpm 详细的可参考: http://os.51cto.com/art/201001/177866.htm

  5. android make-standalone-toolchain.sh 使用说明

    #$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-24 --install-dir=./android ...

  6. 题目1208:10进制 VS 2进制(进制转换以及大数保存问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1208 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. sencha touch 在安卓中横屏、竖屏切换 应用崩溃问题

    答案来至于 Sencha Touch 交流 @周旭 这是由于横竖屏转换导致activity重跑onCreate方法导致的,有两种解决方案:1.横竖屏转换的时候不要重新跑onCreate方法,这个可以在 ...

  8. 织梦导航条dropdown.js的改进(2013-7-10)

    可以设置一个一直都显示的二级菜单,修复了没有二级菜单时鼠标移上去仍然显示上一个二级菜单的问题.支持一级菜单鼠标离开事件 html代码 <!DOCTYPE html PUBLIC "-/ ...

  9. 认识OpenStack中的flatnetwork

    目录 [隐藏] 1 Understanding FlatNetworking 1.1 FlatNetworking 1.1.1 Single Adapter, All in one setup 1.1 ...

  10. POJ 1117 Pairs of Integers

    Pairs of Integers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 Accepted: 1062 Des ...