分页: 1 / 2
如何使用脚本在开机时从终端里启动运行python程序
发表于 : 2008-06-21 16:57
由 lorenxu
如题:
小弟对脚本及linux的启动机理甚是陌生,我想实现开机时自动在gnome-terminal里面运行自己编写的python程序,不知道如何才可以实现,请高人指点。
脚本实验
发表于 : 2008-06-21 17:25
由 lorenxu
自己写了一个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程序的目的
在/etc/rc.local里添加语句
发表于 : 2008-06-21 17:43
由 lorenxu
在这个文件的exit 0前添加如下语句:
/bin/try.sh
(在root权限下把try.sh拷进了bin目录下)添加完以后,保存rc.local,重启机器,进入myname用户,没有成功?why?
参考
发表于 : 2008-06-21 18:07
由 lorenxu
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.
发表于 : 2008-06-21 18:20
由 xiooli
在系统》首选项》会话里面添加一条:gnome-terminal -e XX.py(自己写的话最好加上准确的脚本/程序路径)就行了。
ps:不用重启机器,重启X就可以看效果
发表于 : 2008-06-21 18:49
由 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
是否有什么地方不妥?
发表于 : 2008-06-21 18:54
由 lorenxu
有这样一个情况:
我使用了session的保存当前会话的选项,所以机器启动后总是有一个终端会话窗口出现,是否是这个影响?我如何禁止这个会话?
发表于 : 2008-06-21 19:01
由 xiooli
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
是否有什么地方不妥?
你的hello.py可有可执行权限?没有的话chmod +x hello.py先。
发表于 : 2008-06-21 19:02
由 solcomo
xterm -c -e /home/xulog/Desktop/hello.py
发表于 : 2008-06-21 19:10
由 xiooli
哦,想起来了,执行成功以后终端就自己退出了。。。。。。。。。
试试ls的,不过没有xterm咋办?
发表于 : 2008-06-21 19:11
由 solcomo
...有X就有xterm
发表于 : 2008-06-21 19:13
由 xiooli
solcomo 写了:...有X就有xterm
不一定哦
代码: 全选
<(︶︿︶)>[~]:xterm
程序 'xterm' 尚未安装。 您可以通过输入以下命令安装:
sudo apt-get install xterm
bash: xterm:找不到命令
<(︶︿︶)>[~]:
难道以前被我删掉了?

发表于 : 2008-06-21 19:15
由 solcomo

我是装完xorg就有的啊...
发表于 : 2008-06-21 19:37
由 lorenxu
xiooli 写了: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
是否有什么地方不妥?
你的hello.py可有可执行权限?没有的话chmod +x hello.py先。
用这个方法试了一下,不知道运行了没有,把hello.py做成一个死循环呢?
发表于 : 2008-06-21 19:38
由 lorenxu
xterm 是有的