我有我认为是一个非常简单的脚本,我想在启动时运行,但是我对init.d脚本很新,也许有更好的方法来执行此操作.
基本上我希望我的脚本在系统启动时运行,所以我有一个ruby脚本,我已经转移到/usr/bin,并命名为consumer
为了简洁起见,它看起来像这样,但实际上做了一些事情:
- #!/usr/bin/env ruby
- # just example code
- puts "do stuff"
然后我将我的init.d脚本放在/etc/init.d中并命名为consumer.
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: consumer
- # required-Start: $remote_fs $syslog
- # required-Stop: $remote_fs $syslog
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: Start daemon at boot time
- # Description: Enable service provided by daemon.
- ### END INIT INF
- # /etc/init.d/consumer
- #
- # Some things that run always
- touch /var/lock/consumer
- # Carry out specific functions when asked to by the system
- case "$1" in
- start)
- echo "Starting Consumer"
- consumer &
- echo "Consumer started successfully."
- ;;
- stop)
- echo "Stopping Consumer"
- echo "Nothing happened..."
- ;;
- *)
- echo "Usage: /etc/init.d/consumer {start|stop}"
- exit 1
- ;;
- esac
- exit 0
现在如果我保存这个文件并且我只是运行sudo /etc/init.d/consumer start,它就完美了!它启动并给我所需的输出.那么我跑:
- $sudo update-rc.d consumer defaults
- Adding system startup for /etc/init.d/consumer ...
- /etc/rc0.d/K20consumer -> ../init.d/consumer
- /etc/rc1.d/K20consumer -> ../init.d/consumer
- /etc/rc6.d/K20consumer -> ../init.d/consumer
- /etc/rc2.d/S20consumer -> ../init.d/consumer
- /etc/rc3.d/S20consumer -> ../init.d/consumer
- /etc/rc4.d/S20consumer -> ../init.d/consumer
- /etc/rc5.d/S20consumer -> ../init.d/consumer
但是当重新启动系统时,我的脚本永远不会启动,任何想法?我不确定接下来会采取什么行动.我已经将所有脚本权限调整为775,并确保root拥有它们.
任何帮助都会非常有帮助.
做“消费者&”将简单地介绍任务并继续.如果拥有的shell终止,它将终止任何后台任务.您说该脚本在命令行上运行,但是如果您注销,您的守护程序将无法生存?
你想用start-stop-daemon之类的东西启动你的守护进程.
编辑:实际上,在再次阅读你的文字时,我不确定消费者是否是一个守护进程?如果你只想在启动时运行一些代码(例如清理房间),你可以在/etc/rc.local中写一行代码.
如果脚本需要很长时间才能运行,您可能想要否定它.即:
- consumer &
- disown %1
该脚本现在将在shell终止后继续存在.请注意,如果shell输出文本,它将保留相同的tty,这可能会导致问题,具体取决于它拥有的shell终止后发生的情况.