为什么我的shell脚本不能在启动时执行

我希望我的openSuse 11.1服务器在启动时运行一个小的shell脚本。 我已经创build了一个脚本,并在/etc/init.d/rc5.d/中为该脚本添加了一个符号链接。 然而,脚本不会被执行。

这是我试图运行的脚本:

#!/bin/bash #monitor RAID array /sbin/mdadm --monitor --daemonize [email protected] /dev/md0 /dev/md1 /dev/md2 /dev/md3 #start SVN server /usr/bin/svnserve -d 

当我从命令行自己运行脚本时,脚本工作正常(svn服务器和mdadm被按照预期守护进程)。 但是,重启服务器似乎并没有启动。

这可能会帮助你弄清楚我做错了什么:

 linuxplug:~ # ls -l /etc/init.d/rc5.d/exe* lrwxrwxrwx 1 root root 25 May 22 11:43 /etc/init.d/rc5.d/executeatstartup.sh -> /root/executeatstartup.sh linuxplug:~ # ls -l /root/executeatstartup.sh -rwxr-xr-x 1 root root 172 Apr 12 13:43 /root/executeatstartup.sh linuxplug:~ # which svnserve /usr/bin/svnserve 

我究竟做错了什么?

编辑:我已经将链接重命名为shell脚本,如下所示:

 linuxplug:~ # ls -l /etc/init.d/rc5.d/ lrwxrwxrwx 1 root root 25 May 22 12:03 S99executeatstartup -> /root/executeatsta rtup.sh 

但是,该脚本仍然不会在启动时运行。

你有没有尝试在/etc/init.d/boot.local中添加对脚本的调用? 尝试一下,看看是否更好。 还有其他几个启动文件在启动的不同部分运行。

你应该有你的链接命名为S99something,其中S代表开始,99代表该目录中的脚本开始的顺序。 看到该目录中的其他链接…

此外,您可能需要重写脚本以遵守启动/停止参数,并添加K99something符号链接以确保在离开此运行级别时服务已正确停止。

你是否已经按照opensuse网站上列出的步骤? 这应该肯定工作。

Cool Solutions也有相当好的解释

你应该把这个命令放在/etc/rc.local(或者在opensuse中等价)

initscripts必须有一定的语法。 你可以检查其中的一个例子,但你需要的是/etc/rc.local

确实应该有一种方法来从/ etc / rc *中执行所需的操作。 你可能想要考虑的另一种select是cron。 使用vixie-cron,可以使用crontab中的@reboot关键字指定要在重新启动时运行的任务:

 Instead of the first five fields, one of eight special strings may appear: string meaning ------ ------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *". 

上表来自Jeremy Zawodny的博客文章,我不允许发布链接作为新用户。 在Google上search“vixie-cron在启动时指定任务”,这是第一个结果。

正如文章中提到的那样,允许用户在启动时运行命令而不授予root用户权限是一种很好的方式。

还有一个值得检查/ etc / inittab以确保你默认启动到运行级别5,寻找一行:

ID:2:initdefault的:

这是从我的debian笔记本电脑,并显示我默认启动到运行级别二(这是debian的默认)。

在RedHat衍生产品(如openSuSE)上,runlevel five是运行X11时的默认值,runlevel three是默认启动到控制台。

是否打开使用selinux? 如果是这样,打开脚本的上下文是否与其他启动脚本相同? 尝试getenforce ,看看是否安装了selinux打开。 试试ls -lZ /etc/init.d/rc5.d/来查看脚本的上下文