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

Abh*_*hay 1 python ubuntu cron startup

我遵循了许多关于stackoverflow的教程,这些教程关于在启动时启动python脚本,但是它们都不起作用。

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

  1. 我尝试了init.d方法

我做了一个start.sh/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"
Run Code Online (Sandbox Code Playgroud)

它的权限是chmod在+ x

ion@aurora:/etc/init.d$ ll start.sh
-rwxr-xr-x 1 root root 625 Jun 25 19:10 start.sh*
Run Code Online (Sandbox Code Playgroud)

去了 /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
Run Code Online (Sandbox Code Playgroud)

没工作

  1. cronjob方法

    须藤crontab -e

并附加

@reboot sh '/etc/init.d/start.sh'
Run Code Online (Sandbox Code Playgroud)

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

手动触发的日志

(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
Run Code Online (Sandbox Code Playgroud)

Art*_*ski 5

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

对于Systemd:

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

[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
Run Code Online (Sandbox Code Playgroud)

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

sudo systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

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

sudo systemctl enable you_service_name.service
Run Code Online (Sandbox Code Playgroud)

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

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

sudo journalctl -xe
Run Code Online (Sandbox Code Playgroud)

UPD:

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

ExecStart=/venv_home/path/to/python /venv_home/path/to/your/script.py
Run Code Online (Sandbox Code Playgroud)

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

@reboot /bin/bash /path/to/script.sh
Run Code Online (Sandbox Code Playgroud)

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