Centos

使用 upstart 在啟動時執行 Python 腳本

  • July 19, 2014

我正在嘗試創建一個新貴腳本以在啟動時執行 python 腳本。從理論上講,它看起來很簡單,但我似乎無法讓它工作。我正在使用在這裡找到並更改的骨架腳本。

description "Used to start python script as a service"
author "Me <me@I.com.au>"

# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [016]

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
expect fork

# Start the process
script
 exec python /usr/local/scripts/script.py
end script

我希望它執行的測試腳本目前是一個簡單的 python 腳本,從終端執行時執行沒有任何問題。

#!/usr/bin/python2

import os, sys, time 
if __name__ == "__main__":  
   for i in range (10000):
       message = "UpstartTest " , i , time.asctime() , " - Username: " , os.getenv("USERNAME")
       #print message
   time.sleep(60)
       out = open("/var/log/scripts/scriptlogfile", "a")
       print >> out, message
       out.close()
  • location/var/log/scripts 的權限為 777
  • 文件 /usr/local/scripts/script.py 的權限為 775
  • 新貴腳本 /etc/init.d/pythonupstart.conf 的權限為 755

新貴腳本進入**/etc/init而不是/etc/init.d**,那一周是漫長的一周。:(

執行蟒蛇/usr/local/scripts/script.py

  • 顯示什麼/var/log/upstart/yourjob.log

引用自:https://serverfault.com/questions/451063