Start a python script at startup automatically?

笑着哭i 提交于 2020-01-30 08:14:23

问题


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

I need to activate a virtualenv then start a flask server

  1. I tried init.d method

I made an start.sh in /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"

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*

Went to /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

Didn't work

  1. cronjob method

    sudo crontab -e

and appended

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

Didn't worked either , where am I wrong?

Manual triggered logs

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


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.

For Systemd:

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

Save this file and reload systemd:

sudo systemctl daemon-reload

Then add your service to autostart:

sudo systemctl enable you_service_name.service

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

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:

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) 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.



来源:https://stackoverflow.com/questions/51025312/start-a-python-script-at-startup-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!