简体   繁体   English

ubuntu ec2-在启动时使用参数运行python脚本

[英]ubuntu ec2 - run python script at startup with arguments

I have a python script i'd like to start on startup on an ubuntu ec2 instance but im running into troubles. 我有一个python脚本,我想在ubuntu ec2实例上启动时启动,但是我遇到了麻烦。

The script runs in a loop and takes care or exiting when its ready so i shouldn't need to start or stop it after its running. 该脚本在循环中运行,当其准备就绪时会小心或退出,因此我在运行后无需启动或停止它。

I've read and tried a lot of approaches with various degrees of success and honestly im confused about whats the best approach. 我已经阅读并尝试了许多方法,这些方法都取得了不同程度的成功,并且老实说,我对最佳方法感到困惑。 I've tried putting a shell script that starts the python script in /etc/init.d, making it executable and doing update-rc.d to try to get it to run but its failed at every stage. 我试过将可启动python脚本的shell脚本放在/etc/init.d中,使其可执行,并执行update-rc.d尝试使其运行,但在每个阶段均失败。

here's the contents of the script ive tried: 这是我尝试过的脚本的内容:

#!/bin/bash

cd ~/Dropbox/Render\ Farm\ 1/appleseed/bin
while :
do
    python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
done

i then did 然后我做了

sudo chmod +x /etc/init.d/script_name
sudo sudo update-rc.d /etc/init.d/script_name defaults

This doesn't seem to run on startup and i cant see why, if i run the command manually it works as expected. 这似乎无法在启动时运行,而且我看不到原因,如果我手动运行命令,它将按预期运行。

I also tried adding a line to rc.local to start the script but that doesn't seem to work either 我还尝试在rc.local中添加一行以启动脚本,但这似乎也不起作用

Can anybody share what they have found is the simplest way to run a python script in the background with arguments on startup of an ec2 instance. 任何人都可以分享他们发现的是在ec2实例启动时在后台运行带参数的最简单方法的最简单方法。

UPDATE: ---------------------- 更新:----------------------

I've since moved this code to a file called /home/ubuntu/bin/watch_folder_start 此后,我已将此代码移至名为/home/ubuntu/bin/watch_folder_start

#!/bin/bash

cd /home/ubuntu/Dropbox/Render\ Farm\ 1/appleseed/bin
while :
do
    python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
done

and changed my rc.local file to this: 并将我的rc.local文件更改为:

nohup /home/ubuntu/bin/watch_folder_start &

exit 0

Which works when i manually run rc.local but wont fire on startup, i did chmod +x rc.local but that didn't change anything, 当我手动运行rc.local但在启动时不会启动时,它可以工作,我做了chmod +x rc.local但没有任何改变,

Your /etc/init.d/script_name is missing the plumbing that update-rc.d and so on use, and won't properly handle stop , start , and other init-variety commands, so... 您的/etc/init.d/script_name缺少使用update-rc.d等的管道,并且无法正确处理stopstart和其他init-variety命令,因此...

For initial experimentation, take advantage of the /etc/init.d/rc.local script (which should be linked to by default from /etc/rc2/S99rc.local ). 对于初始实验,请利用/etc/init.d/rc.local脚本(默认情况下应从/etc/rc2/S99rc.local链接至该脚本)。 The gets you out of having to worry about the init.d conventions and just add things to /etc/rc.local before the exit 0 at its end. 这样您就不必担心init.d约定,只需在exit 0末尾的/etc/rc.local exit 0添加内容即可。

Additionally, that ~ isn't going to be defined, you'll need to use a full pathname - and furthermore the script will run as root. 另外,将不会定义~ ,您将需要使用完整的路径名-而且脚本将以root身份运行。 We'll address how to avoid this if desired in a bit. 如果有需要,我们将解决如何避免这种情况。 In any of these, you'll need to replace "whoeveryouare" with something more useful. 无论采用哪种方式,您都需要用更有用的内容替换“ whoeveryouare”。 Also be warned that you may need to prefix the python command with a su command and some arguments to get the process to run with the user id you might need. 还请注意,您可能需要在python命令前加上su命令和一些参数,以使进程以您可能需要的用户ID运行。

You might try (in /etc/rc.local ): 您可以尝试(在/etc/rc.local ):

( if cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' ; then
      while : ; do
           # This loop should respawn watchfolder18.py if it dies, but
           # ideally one should fix watchfolder18.py and remove this loop.
           python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
      done
  else
      echo warning: could not find watchfolder 1>&2
  fi
) &

You could also put all that in a script and just call it from /etc/rc.local . 您也可以将所有内容放在脚本中,然后从/etc/rc.local调用它。

The first pass is roughly what you had, but if we assume that watchfolder18.py will arrange to avoid dying we can cut it down to: 第一遍大致就是您所拥有的,但是如果我们假设watchfolder18.py可以避免死亡,我们可以将其缩减为:

( cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' \
     && exec python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/ ) &

These aren't all that pretty, but it should let you get your daemon sorted out so you can debug it and so on, then come back to making a proper /etc/init.d or /etc/init script later. 这些并不是全部漂亮,但是它应该让您整理好守护程序,以便对其进行调试等,然后稍后再制作正确的/etc/init.d/etc/init脚本。 Something like this might work in /etc/init/watchfolder.conf , but I'm not yet facile enough to claim this is anything other than a rough stab at it: 这样的事情可能在/etc/init/watchfolder.conf起作用,但是我还不够灵活,不能声称这是一个粗暴的尝试:

# watchfolder - spawner for watchfolder18.py
description     "watchfolder program"

start on runlevel [2345]
stop on runlevel [!2345]

script
   if cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' ; then
     exec python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/0
   fi
end script

I found that the best solution in the end was to use 'upstart' and create a file in etc/init called myfile.conf that contained the following 我发现最后最好的解决方案是使用“ upstart”并在etc/init创建一个名为myfile.conf的文件,其中包含以下内容

description "watch folder service"
author      "Jonathan Topf"

start on startup

stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    HOST=`hostname`
    chdir /home/ubuntu/Dropbox/Render\ Farm\ 1/appleseed/bin
    exec /usr/bin/python ./watchfolder.py -t ./appleseed.cli -u $HOST ../../data/  >> /home/ubuntu/bin/ec2_server.log 2>&1
    echo "watch_folder started"
end script

More info on using the upstart system here 有关在这里使用新贵系统的更多信息

http://upstart.ubuntu.com/ http://upstart.ubuntu.com/

https://help.ubuntu.com/community/UbuntuBootupHowto https://help.ubuntu.com/community/UbuntuBootupHowto

http://blog.joshsoftware.com/2012/02/14/upstart-scripts-in-ubuntu/ http://blog.joshsoftware.com/2012/02/14/upstart-scripts-in-ubuntu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM