赞
踩
很多时候,我们期望某个/某几个应用程序能随着操作系统启动而启动。如下是几种ubuntu系统下可行的方法。
方法1:
在/etc/rc.local中添加需要开机启动的命令。
Ubuntu中的/etc/rc.local是系统启动时会自动执行的脚本,只要将对应的指令写入该脚本中,Ubuntu开机时就会自动执行该指令,从而实现开机自启动,一般的/etc/rc.local是以exit 0结束的,这是因为只有当返回值为0时,系统才判定该脚本执行正确,我们要做的就是将需要执行的指令写在exit 0 之前即可。
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi # 在这里增加你期望开机自动执行的程序,例如: # 这里假设可执行文件路径为/home/pi/,文件名为helloworld.out # &表示程序在后台运行 /home/pi/helloworld.out & exit 0
注:其中的&表示不在终端显示指令执行的结果(后台运行),这里就需要注意,如果自启动的程序是一个不会自动结束的程序就一定要添加&,不然启动系统时执行到该条指令就会一直停在这里,而不会进入图形界面。
方法2:利用sh脚本实现自启动
将需要执行的命令写成sh脚本,再在/etc/rc.local中设置开机自启动sh脚本。
也可以将需要指定的执行写在一个sh脚本里,然后再在/etc/rc.local添加执行该脚本的指令。以自启动/home/pi/helloworld.out为例:
sh脚本(helloworld.sh):
#!/bin/sh -e
/home/pi/helloworld.out
其中的#!/bin/sh必须要加上,用来指定执行文件中的指令的程序为/bin/sh,还需要注意的是,在一个sh脚本中最好只写一行指令,因为当有多条指令时,如果第一条指令没有结束就不会去执行下一条指令。
rc.local:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi # 在这里增加你期望开机自动执行的程序,例如: # 这里假设可执行文件路径为/home/pi/,sh文件名为helloworld.sh # &表示程序在后台运行 /home/pi/helloworld.sh & exit 0
参考文档:
https://zhuanlan.zhihu.com/p/43542649
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。