Ubuntu14.04设置开机自启动脚本

方法一、编辑rc.loacl脚本 

  Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本。在 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.

echo "hello start up script!" > /home/sc/Desktop/mystart.txt

exit 0

  重启后可以在桌面上看到生成的mystart.txt文件。

  

方法二、添加一个Ubuntu的开机启动服务

  如果要添加为开机启动执行的脚本文件,可先将脚本复制或者软连接/etc/init.d/目录下,然后用:update-rc.d xxx defaults NN 命令(NN为启动顺序),将脚本添加到初始化执行的队列中去。

  1) 新建一个脚本文件 test.sh
#!/bin/bash
# command content
echo "hello start up script!" > /home/sc/Desktop/mystart.txt
exit 0

  2) 将脚本放置到启动目录/etc/init.d

sudo mv test.sh  /etc/init.d/
  3) 设置脚本文件的权限
cd /etc/init.d/
sudo chmod 755 test.sh

  4) 将脚本添加到启动脚本中

sudo update-rc.d  test.sh  defaults  90

  其中数字90是脚本启动的顺序号,数字越大表示执行的越晚,按照自己的需要相应修改即可。

  重启后可以在桌面上看到生成的mystart.txt文件。

  移除ubuntu开机脚本:
sudo update-rc.d -f test.sh remove

 

方法三、桌面环境下设置开机自启动

  从Xfce桌面菜单中选择"Settings Manager"(设置管理器)。在"Settings"(设置)窗口中,点击"Session and Startup"(会话和启动)图标。

  在"Application Autostart"(应用程序自动启动)选项卡下,点击底部的"Add"(添加)按钮,添加自启动程序。
  或者在终端中执行:gnome-session-properties,会弹出一个“启动应用程序首选项”的菜。点击添加会弹出下面的这个对话框:在名称中写入要启动应用的名字,命令是这个应用的可执行程序的位置,一般的可执行程序都在/usr/bin目录下。注释可写可不写:
 
  设置好后可以输入指令立即重启:sudo shutdown -r now ,开机后会自动执行用户编写的GUI程序:
 
   如果是控制台程序,可以在Command输入如下命令:
gnome-terminal -x  /home/sc/Desktop/test

  gnome-terminal打开一个新的终端,-x参数后面的内容是要执行的命令。重新启动后可以看到gnome终端模拟器中打印出Hello world!

  如果执行完后一闪而过,一种方法是修改terminal的配置,选择Edit→Profile Preferences→Title and Command,里面有一项When command exits,选为Hold the terminal open就可以了。
 
参考:
原文地址:https://www.cnblogs.com/21207-iHome/p/7472316.html