在启动时自动启动python脚本吗? [英] Start a python script at startup automatically?

查看:172
本文介绍了在启动时自动启动python脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了许多关于stackoverflow的教程,这些教程关于在启动时启动python脚本,但是都没有用。

I followed multiple tutorials available on stackoverflow about starting a python script at startup but none of them works.

我需要激活virtualenv然后启动flask服务器

I need to activate a virtualenv then start a flask server


  1. 我尝试了
    init.d方法

我在 /etc/init.d /

#!/bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $portmap
# Should-Stop:       $portmap
# X-Start-Before:    nis
# X-Stop-After:      nis
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

cd /home/ion/
source /home/ion/py35/bin/activate
cd /home/ion/Desktop/flask/
nohup python main.py &
echo "Done"

它的权限是chmod为+ x

Its permissions are chmod at +x

ion@aurora:/etc/init.d$ ll start.sh
-rwxr-xr-x 1 root root 625 Jun 25 19:10 start.sh*

转到 /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.
/etc/init.d/start.sh


exit 0

没用


  1. cronjob方法

  1. cronjob method

sudo crontab -e

sudo crontab -e

并附加

@reboot sh '/etc/init.d/start.sh'

也没有工作,我在哪里错了?

Didn't worked either , where am I wrong?

手动触发的日志

(py35) ion@aurora:~/Desktop/flask$ python main.py 
WARNING:tensorflow:From /home/ion/Desktop/flask/encoder.py:57: calling l2_normalize (from tensorflow.python.ops.nn_impl) with dim is deprecated and will be removed in a future version.
Instructions for updating:
dim is deprecated, use axis instead
2018-06-25 19:34:05.511943: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://localhost:5505/ (Press CTRL+C to quit)
 * Restarting with stat


推荐答案

1)不要使用旧的 init.d方法。使用现代的东西。如果您具有Ubuntu 15.04和更高版本,则可以使用Systemd创建守护程序,该守护程序将在启动时自动启动。例如,如果您的Ubuntu早于15.04,请使用Upstart。

1) Don't use old "init.d" method. Use something modern. If you have Ubuntu 15.04 and higher, you can use Systemd to create daemon that will be started automatically at startup. If you have for example Ubuntu older than 15.04 - use Upstart.

对于Systemd:

/ lib / systemd / system /中创建单位文件you_service_name.service 具有以下内容(据我所知,您的python脚本在运行时不会生成新进程,因此 Type 应该是简单。更多信息此处):

Create unit file in /lib/systemd/system/you_service_name.service with the following content (as far as I can see your python script doesn't spawn new process while running, so Type should be simple. More info here):

[Unit]
Description=<your_service_name>
After=network.target network-online.target

[Service]
Type=simple
User=<required_user_name>
Group=<required_group_name>
Restart=always
ExecStartPre=/bin/mkdir -p /var/run/<your_service_name>
PIDFile=/var/run/<your_service_name>/service.pid
ExecStart=/path/to/python_executable /path/to/your/script.py

[Install]
WantedBy=multi-user.target

保存此文件并重新加载systemd:

Save this file and reload systemd:

sudo systemctl daemon-reload

然后将您的服务添加到自动启动:

Then add your service to autostart:

sudo systemctl enable you_service_name.service

您应该看到在 enable 操作之后,Systemd创建了所需的符号链接。

you should see that Systemd created required symlinks after enable action.

重新启动,看看它是否已启动并正在运行( ps aux | grep python sudo systemctl status you_service_name.service )。如果有些奇怪,请检查系统日志:

Reboot and see if it's up and running (ps aux | grep python or sudo systemctl status you_service_name.service). If there is something weird - check Systemd journal:

sudo journalctl -xe

UPD:

要在所需的virtualenv中启动python脚本,只需在您的服务单元文件中使用此表达式:

To launch your python script in desired virtualenv, just use this expression in your service unit file:

ExecStart=/venv_home/path/to/python /venv_home/path/to/your/script.py

2)您也可以使用 crontab ,但是您需要在其中指定所需shell的完整路径,例如:

2) You can also use crontab, but you need to specify full path for desired shell there, for example:

@reboot /bin/bash /path/to/script.sh

如果您需要其他帮助-请在此处告诉我。

If you need additional help - just let me know here.

这篇关于在启动时自动启动python脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆