如何使用脚本在开机时从终端里启动运行python程序

sh/bash/dash/ksh/zsh等Shell脚本
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

如何使用脚本在开机时从终端里启动运行python程序

#1

帖子 lorenxu » 2008-06-21 16:57

如题:
小弟对脚本及linux的启动机理甚是陌生,我想实现开机时自动在gnome-terminal里面运行自己编写的python程序,不知道如何才可以实现,请高人指点。
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

脚本实验

#2

帖子 lorenxu » 2008-06-21 17:25

自己写了一个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程序的目的
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

在/etc/rc.local里添加语句

#3

帖子 lorenxu » 2008-06-21 17:43

在这个文件的exit 0前添加如下语句:
/bin/try.sh

(在root权限下把try.sh拷进了bin目录下)添加完以后,保存rc.local,重启机器,进入myname用户,没有成功?why?
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

参考

#4

帖子 lorenxu » 2008-06-21 18:07

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.
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

#5

帖子 xiooli » 2008-06-21 18:20

在系统》首选项》会话里面添加一条:gnome-terminal -e XX.py(自己写的话最好加上准确的脚本/程序路径)就行了。
ps:不用重启机器,重启X就可以看效果
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

#6

帖子 lorenxu » 2008-06-21 18:49

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
是否有什么地方不妥?
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

#7

帖子 lorenxu » 2008-06-21 18:54

有这样一个情况:

我使用了session的保存当前会话的选项,所以机器启动后总是有一个终端会话窗口出现,是否是这个影响?我如何禁止这个会话?
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

#8

帖子 xiooli » 2008-06-21 19:01

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先。
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#9

帖子 solcomo » 2008-06-21 19:02

xterm -c -e /home/xulog/Desktop/hello.py
♜♞♝♛♚♝♞♜
♟♟♟♟♟♟♟♟
♙♙♙♙♙♙♙♙
♖♘♗♕♔♗♘♖

☠☯⚔⚓☣☦☃☕
☹☻☪☭☬⚖⚛⚜
ℜℳℬ™ ℋℯℓ℘ ℳℭ
sƂɐʍ рǀɹoʍ əɥʇ oS
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

#10

帖子 xiooli » 2008-06-21 19:10

哦,想起来了,执行成功以后终端就自己退出了。。。。。。。。。
试试ls的,不过没有xterm咋办?
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#11

帖子 solcomo » 2008-06-21 19:11

...有X就有xterm
♜♞♝♛♚♝♞♜
♟♟♟♟♟♟♟♟
♙♙♙♙♙♙♙♙
♖♘♗♕♔♗♘♖

☠☯⚔⚓☣☦☃☕
☹☻☪☭☬⚖⚛⚜
ℜℳℬ™ ℋℯℓ℘ ℳℭ
sƂɐʍ рǀɹoʍ əɥʇ oS
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

#12

帖子 xiooli » 2008-06-21 19:13

solcomo 写了:...有X就有xterm
不一定哦

代码: 全选

<(︶︿︶)>[~]:xterm
程序 'xterm' 尚未安装。  您可以通过输入以下命令安装:
sudo apt-get install xterm
bash: xterm:找不到命令
<(︶︿︶)>[~]:
难道以前被我删掉了? :shock:
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#13

帖子 solcomo » 2008-06-21 19:15

:shock: 我是装完xorg就有的啊...
♜♞♝♛♚♝♞♜
♟♟♟♟♟♟♟♟
♙♙♙♙♙♙♙♙
♖♘♗♕♔♗♘♖

☠☯⚔⚓☣☦☃☕
☹☻☪☭☬⚖⚛⚜
ℜℳℬ™ ℋℯℓ℘ ℳℭ
sƂɐʍ рǀɹoʍ əɥʇ oS
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

#14

帖子 lorenxu » 2008-06-21 19:37

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做成一个死循环呢?
lorenxu
帖子: 49
注册时间: 2007-10-29 12:04

#15

帖子 lorenxu » 2008-06-21 19:38

xterm 是有的
回复