ubuntu-18.04 设置开机启动python脚本

开机启动折腾了好久,搞定开机启动但是启动python项目时又遇到问题搞了半天。

一、配置开机启动
ubuntu-18.04没有rc.local,不能通过rc.local来设置开机启动脚本,可以自己建一个。

  1. 建立rc-local.service文件
    sudo vi /etc/systemd/system/rc-local.service
    然后将下面内容复制进去

    [Unit]
    Description=/etc/rc.local Compatibility
    ConditionPathExists=/etc/rc.local
     
    [Service]
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    StandardOutput=tty
    RemainAfterExit=yes
    SysVStartPriority=99
     
    [Install]
    WantedBy=multi-user.target
    
  2. 创建文件rc.local
    sudo vi /etc/rc.local
    然后将下面内容复制进去

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    #后台启动python工程,结果重定向到log.txt
    /usr/bin/nohup /usr/bin/python start_person.py > log.txt 2>&1 &
    exit 0
    
  3. 给rc.local加上权限

    sudo chmod +x /etc/rc.local
    
  4. 重启看看日志就可以.

二、解决python启动无法导入第三方模块问题

  1. 看日志发现导入模块失败,但是本地直接执行脚本没有任何问题
    ImportError: No module named torch

  2. 分析
    1)在本地进入python交互环境,查看python搜索路径

    nvidia@nvidia-desktop:~/data/projects/dt_product$ python
    Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
    [GCC 7.3.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', '/home/nvidia/.local/lib/python2.7/site-packages', '/home/nvidia/data/projects/dt_product', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-aarch64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
    >>>
    

    2)新建一个python脚本设置为开机启动,在此脚本中获取python搜索路径

    !test.py
    
    import sys
    print sys.path
    

    重新启动环境查看启动重定向得日志 log.txt :

    ['/home/nvidia/data/projects/dt_product', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-aarch64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
    

    对比发现少了一个路径:'/home/nvidia/.local/lib/python2.7/site-packages',环境上看此路径为无法import 包得安装路径;应该是安装这些包时留的坑。

  3. 解决
    一种是安装时注意下路径,一种是把这个路径加到环境变量中。
    加入环境变量网上搜有几种,但是试了都不行,因为启动python项目时改环境变量未生效,所以最简单得就是在启动python项目前加入该变量。

    打开刚才创建得启动python项目得文件,启动前加入环境变量

    sudo vi /etc/rc.local
    
    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    #加入环境变量
    export PYTHONPATH=/home/nvidia/.local/lib/python2.7/site-packages:$PYTHONPATH
    
    #后台启动python工程,结果重定向到log.txt
    /usr/bin/nohup /usr/bin/python start_person.py > log.txt 2>&1 &
    exit 0
    

    重启再查看日志是否启动。

你可能感兴趣的:(Linux,python)