如何在启动时运行命令序列?

我需要在启动时运行一系列命令。 此序列也需要root密码。 我怎样才能做到这一点?

这尤其是我需要运行的命令序列:

virginia@asus-F552CL:~$ cd MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00/ virginia@asus-F552CL:~/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00$ sudo su [sudo] password for virginia: root@asus-F552CL:/home/virginia/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00# ./load.sh root@asus-F552CL:/home/virginia/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00# exit virginia@asus-F552CL:~/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00$ 

既然你已经提到过要做sudo su,我假设你想要在登录后执行这些命令。

为此,您必须将命令放在脚本文件中,例如myCom.sh 。 内容如下所示:

 cd MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00/ echo myPassword | sudo -S su && sudo su -c ./load.sh 

请注意,不需要退出,因为每个命令都以root身份运行,脚本在完成后返回。

“myPassword”也将以纯文本forms提供。

然后使用KDE / Ubuntu的启动管理器启动此脚本。 或者您可以将此文件直接放在~/.profile

另一方面,如果要在启动时运行这些命令而不是登录,则可以使用init脚本。 你不必在那里使用sudo su ,因为一切都将在root上下文中运行。

您可以修改在运行级别启动序列中作为最后一个服务执行的/etc/rc.local文件,并授予root权限。

这是一个例子:

 #!/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. cd /home/virginia/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00 /bin/bash load.sh exit 0 

使用此命令validation您的更改:

 sudo /etc/init.d/rc.local start 

请注意以下几点:

  1. load.sh将以root身份运行
  2. 环境受到限制,因此您可能需要定义缺失的变量
  3. 如果你想登录syslog ,请使用logger "message"

如果可能的话,我建议你像这样执行你的脚本:

  /bin/bash /home/virginia/MT7630E_Wi-Fi_BT_Source_Release_20140625/rt2x00/load.sh 

filesystem and static-network-up事件发生时,在Ubuntu运行级别启动时。 如果需要在特定事件中启动load.sh ,那么最好定义upstart任务 。