python - 在启动时自动启动 python 脚本?

标签 python ubuntu cron startup

我遵循了 stackoverflow 上提供的多个关于在启动时启动 python 脚本的教程,但没有一个有效。

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

  1. 我试过了 初始化方法

我在/etc/init.d/中做了一个start.sh

#!/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 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. 定时任务方法

    sudo crontab -e

并附加

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

也没用,我哪里错了?

手动触发日志

(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。

对于 Systemd:

使用以下内容在 /lib/systemd/system/you_service_name.service 中创建单元文件(据我所知,您的 python 脚本在运行时不会生成新进程,因此 类型 应该是简单。更多信息 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:

sudo systemctl daemon-reload

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

sudo systemctl enable you_service_name.service

您应该看到 Systemd 在 enable 操作后创建了所需的符号链接(symbolic link)。

重新启动并查看它是否正常运行(ps aux | grep pythonsudo systemctl status you_service_name.service)。如果有什么奇怪的东西 - 检查 Systemd 日志:

sudo journalctl -xe

更新:

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

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

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

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

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

关于python - 在启动时自动启动 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025312/

相关文章:

java - 时间触发作业 Cron 或 Quartz?

Python获取ffmpeg进程

linux - 该内核需要 x86-64 CPU,但仅检测到 i686 CPU

ruby - 在 ROR 中运行后台作业的更好选择是什么

cronjob中的python多线程问题

ubuntu - 如何授予本地主机权限?

python - 调用数组时仅使用第一个元素?

python - 词典列表,如何获取 : intersection based on one value and symmetric difference based on another value

javascript - 在 JavaScript 中找不到文件

apache - 使用 Laravel 5.1 安装 phpMyAdmin