如何使用脚本在开机时从终端里启动运行python程序
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
如何使用脚本在开机时从终端里启动运行python程序
如题:
小弟对脚本及linux的启动机理甚是陌生,我想实现开机时自动在gnome-terminal里面运行自己编写的python程序,不知道如何才可以实现,请高人指点。
小弟对脚本及linux的启动机理甚是陌生,我想实现开机时自动在gnome-terminal里面运行自己编写的python程序,不知道如何才可以实现,请高人指点。
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
脚本实验
自己写了一个python的实验程序:
位置:/home/myname/Desktop/
程序名:hello.py
程序代码:
#!/usr/bin/python
#-*-coding:utf8-*-
print "hello,this is a test!"
print "hello,this is a test!"
print "hello,this is a test!"
print "hello,this is a test!"
然后又实验写了一个shell脚本:
位置:/home/myname/Desktop/
脚本名:try.sh
脚本代码:
#!/bin/sh
Name="myname"
echo "My Name is :"$Name
python /home/mymane/Desktop/hello.py
脚本中 Name= ... $Name部分是一个打印语句实验,最后一句是通过脚本来运行python程序的。
实验:
在gnome-terminal里面通过cd命令转到两个文件所在的文件夹,输入sh try.sh,脚本运行,输出结果表明脚本启动了python程序。
下一步,解决如何在机器启动时加载脚本,达到运行python程序的目的
位置:/home/myname/Desktop/
程序名:hello.py
程序代码:
#!/usr/bin/python
#-*-coding:utf8-*-
print "hello,this is a test!"
print "hello,this is a test!"
print "hello,this is a test!"
print "hello,this is a test!"
然后又实验写了一个shell脚本:
位置:/home/myname/Desktop/
脚本名:try.sh
脚本代码:
#!/bin/sh
Name="myname"
echo "My Name is :"$Name
python /home/mymane/Desktop/hello.py
脚本中 Name= ... $Name部分是一个打印语句实验,最后一句是通过脚本来运行python程序的。
实验:
在gnome-terminal里面通过cd命令转到两个文件所在的文件夹,输入sh try.sh,脚本运行,输出结果表明脚本启动了python程序。
下一步,解决如何在机器启动时加载脚本,达到运行python程序的目的
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
在/etc/rc.local里添加语句
在这个文件的exit 0前添加如下语句:
/bin/try.sh
(在root权限下把try.sh拷进了bin目录下)添加完以后,保存rc.local,重启机器,进入myname用户,没有成功?why?
/bin/try.sh
(在root权限下把try.sh拷进了bin目录下)添加完以后,保存rc.local,重启机器,进入myname用户,没有成功?why?
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
参考
You can create startup script that gets run when the machine boots. At the terminal:
Code:
$ runlevel
N 2
This tells me the runlevel I am running at-- in this case, runlevel 2. That means when my system boots, it looks in /etc/rc2.d and runs the scripts it sees there. (For a different runlevel, it would be /etc/rc#.d -- most of the time that is irrelevant, since you will normally be booting into the same runlevel all the time).
In the directory, the scripts have names like "S20inetd", where there is the letter S followed by a number. The number tells what order the scripts are executed in. Lowest first, highest last. Ties, I am not 100% sure.
Anyway, all those scripts are probably sym-links pointing to real scripts. All you need to do is create a script called "S99local" and link it to your local script (I call mine /etc/init.d/rc.local). Just make sure to give it execute permissions.
Code:
$ runlevel
N 2
This tells me the runlevel I am running at-- in this case, runlevel 2. That means when my system boots, it looks in /etc/rc2.d and runs the scripts it sees there. (For a different runlevel, it would be /etc/rc#.d -- most of the time that is irrelevant, since you will normally be booting into the same runlevel all the time).
In the directory, the scripts have names like "S20inetd", where there is the letter S followed by a number. The number tells what order the scripts are executed in. Lowest first, highest last. Ties, I am not 100% sure.
Anyway, all those scripts are probably sym-links pointing to real scripts. All you need to do is create a script called "S99local" and link it to your local script (I call mine /etc/init.d/rc.local). Just make sure to give it execute permissions.
- xiooli
- 帖子: 6956
- 注册时间: 2007-11-19 21:51
- 来自: 成都
- 联系:
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
- xiooli
- 帖子: 6956
- 注册时间: 2007-11-19 21:51
- 来自: 成都
- 联系:
你的hello.py可有可执行权限?没有的话chmod +x hello.py先。lorenxu 写了:谢谢,我按照你的方法试了一下,未果!xiooli 写了:在系统》首选项》会话里面添加一条:gnome-terminal -e XX.py(自己写的话最好加上准确的脚本/程序路径)就行了。
ps:不用重启机器,重启X就可以看效果
我的文件路径: /home/xulog/Desktop/hello.py
会话里面是这样写的:
name:hello
command:gnome-terminal -e /home/xulog/Desktop/hello.py
logout重新登录,程序没有运行.
我是在普通用户下,runlevel为2
是否有什么地方不妥?
- solcomo
- 帖子: 2838
- 注册时间: 2007-04-25 13:12
- xiooli
- 帖子: 6956
- 注册时间: 2007-11-19 21:51
- 来自: 成都
- 联系:
- solcomo
- 帖子: 2838
- 注册时间: 2007-04-25 13:12
- solcomo
- 帖子: 2838
- 注册时间: 2007-04-25 13:12
-
- 帖子: 49
- 注册时间: 2007-10-29 12:04
用这个方法试了一下,不知道运行了没有,把hello.py做成一个死循环呢?xiooli 写了:你的hello.py可有可执行权限?没有的话chmod +x hello.py先。lorenxu 写了:谢谢,我按照你的方法试了一下,未果!xiooli 写了:在系统》首选项》会话里面添加一条:gnome-terminal -e XX.py(自己写的话最好加上准确的脚本/程序路径)就行了。
ps:不用重启机器,重启X就可以看效果
我的文件路径: /home/xulog/Desktop/hello.py
会话里面是这样写的:
name:hello
command:gnome-terminal -e /home/xulog/Desktop/hello.py
logout重新登录,程序没有运行.
我是在普通用户下,runlevel为2
是否有什么地方不妥?