(最简单的方式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. c语言学习笔记---预编译

    专题三: 1)       预编译 处理所有的注释,以空格代替, 将所有的#define删除,并且展开所有的宏定义, 处理条件编译指令#if,#ifdef,#elif,#else,#endif 处理# ...

  2. JQuery学习的尾声

    今天是最后一天学习JQuery,上周我们在狠狠的学习JavaScript,然后在这周我们又把JQuery扼杀在了摇篮里面,纵然学习的太快我们导致我们知识不牢固,可是我们没有那么多的时间学习的如此详细, ...

  3. 深入浅出MFC——MFC程序的生死因果(三)

    1. 本章主要目的:从MFC程序代码中检验出一个Windows程序原本该有的程序进入点(WinMain).窗口类注册(RegisterClass).窗口产生(CreateWindow).消息循环(Me ...

  4. VS中快捷键修改以及快捷键的查看

    eclipse用习惯了一直想把VS中的alt+/改为自动补全,同时自定义一下C#环境下自动加入命名控件的快捷键,前段时间摸索了一下,找到了比较好的方法 首先是vs中修改快捷键的方法:工具->选项 ...

  5. 【技术分享会】 @第七期 android开发基础

    前言 Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发. Android 软件系列包括操作系统.中间 ...

  6. Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)

    Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...

  7. 【大数据系列】hadoop脚本分析

    一.start-all.sh hadoop安装目录/home/hadoop/hadoop-2.8.0/ libexec/hadoop-config.sh     ---设置变量 sbin/start- ...

  8. 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)

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

  9. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  10. [转]Linux sendmail 详解

    Internet上最基本的服务,现在应该大部分人都有自己的邮箱吧,用的人多,但理解的人估计没多少,我自己以前也是常常用,但对其原理并不操心.今天就来操心下,进行个小总结 一.邮件服务的基本流程     ...