当前位置:   article > 正文

ubuntu开机启动python_在Ubuntu中启动时运行Python脚本

ubuntu 启动时运行python程序

I have a short Python script that needs to run at startup - Ubuntu 13.10. I have tried everything I can think of but can't get it to run. The script:

#!/usr/bin/python

import time

with open("/home/username/Desktop/startup.txt", 'a') as f:

f.write(str(time.time()) + " It worked!")

(The actual script is a bit different, as I'm just using this for testing purposes, but you get the idea.)

I've tried all of the following, with no luck:

Put the command python startuptest.py in crontab, as @reboot

python /home/username/Documents/startuptest.py, both as the regular user and as sudo

Put the command python /home/username/Documents/startuptest.py in /etc/rc.local

Opened Ubuntu's Startup Applications and put the command there

Done all of the preceding, putting the command into a shell script

and calling that shell script instead

Nothing works. I get the feeling I'm missing something simple. Any ideas? (The script runs fine if I just run the command from a terminal.)

解决方案

Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)

mystartupscript.conf

start on runlevel [2345]

stop on runlevel [!2345]

exec /path/to/script.py

By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.

manual starting/stopping is done with

sudo service mystartupscript start

and

sudo service mystartupscript stop

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55008?site
推荐阅读
相关标签
  

闽ICP备14008679号