在Ubuntu中启动时运行Python脚本 [英] Run Python script at startup in Ubuntu
问题描述
我有一个简短的Python脚本,需要在启动时运行-Ubuntu 13.10.我已经尝试了所有我能想到的但无法运行的方法.脚本:
#!/usr/bin/python
import time
with open("/home/username/Desktop/startup.txt", 'a') as f:
f.write(str(time.time()) + " It worked!")
(实际脚本有点不同,因为我只是出于测试目的而使用它,但是您明白了.)
我尝试了以下所有方法,但是没有运气:
-
以普通用户和
sudo
的身份将命令 -
将命令
python /home/username/Documents/startuptest.py
放入/etc/rc.local
-
打开Ubuntu的启动应用程序并将命令放在此处
-
完成上述所有操作,并将命令放入外壳脚本中 然后调用该shell脚本
python startuptest.py
作为@reboot
python /home/username/Documents/startuptest.py
放入crontab
中.
没有任何效果.我感觉到我缺少一些简单的东西.有任何想法吗? (如果我只是从终端运行命令,则脚本可以正常运行.)
将此放在/etc/init
中(在Ubuntu 15.x中使用/etc/systemd
)
mystartupscript.conf
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py
通过将该conf文件放在此处,您可以挂接到ubuntu的 upstart 服务,该服务在启动时运行服务. /p>
手动启动/停止完成
sudo service mystartupscript start
和
sudo service mystartupscript stop
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
incrontab
, as@reboot python /home/username/Documents/startuptest.py
, both as the regular user and assudo
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
这篇关于在Ubuntu中启动时运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!