ubuntu – init.d脚本不能在启动时启动

前端之家收集整理的这篇文章主要介绍了ubuntu – init.d脚本不能在启动时启动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有我认为是一个非常简单的脚本,我想在启动时运行,但是我对init.d脚本很新,也许有更好的方法来执行此操作.

基本上我希望我的脚本在系统启动时运行,所以我有一个ruby脚本,我已经转移到/usr/bin,并命名为consumer

为了简洁起见,它看起来像这样,但实际上做了一些事情:

  1. #!/usr/bin/env ruby
  2.  
  3. # just example code
  4. puts "do stuff"

然后我将我的init.d脚本放在/etc/init.d中并命名为consumer.

  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: consumer
  4. # required-Start: $remote_fs $syslog
  5. # required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start daemon at boot time
  9. # Description: Enable service provided by daemon.
  10. ### END INIT INF
  11.  
  12. # /etc/init.d/consumer
  13. #
  14.  
  15. # Some things that run always
  16. touch /var/lock/consumer
  17.  
  18. # Carry out specific functions when asked to by the system
  19. case "$1" in
  20. start)
  21. echo "Starting Consumer"
  22. consumer &
  23. echo "Consumer started successfully."
  24. ;;
  25. stop)
  26. echo "Stopping Consumer"
  27. echo "Nothing happened..."
  28. ;;
  29. *)
  30. echo "Usage: /etc/init.d/consumer {start|stop}"
  31. exit 1
  32. ;;
  33. esac
  34.  
  35. exit 0

现在如果我保存这个文件并且我只是运行sudo /etc/init.d/consumer start,它就完美了!它启动并给我所需的输出.那么我跑:

  1. $sudo update-rc.d consumer defaults
  2. Adding system startup for /etc/init.d/consumer ...
  3. /etc/rc0.d/K20consumer -> ../init.d/consumer
  4. /etc/rc1.d/K20consumer -> ../init.d/consumer
  5. /etc/rc6.d/K20consumer -> ../init.d/consumer
  6. /etc/rc2.d/S20consumer -> ../init.d/consumer
  7. /etc/rc3.d/S20consumer -> ../init.d/consumer
  8. /etc/rc4.d/S20consumer -> ../init.d/consumer
  9. /etc/rc5.d/S20consumer -> ../init.d/consumer

但是当重新启动系统时,我的脚本永远不会启动,任何想法?我不确定接下来会采取什么行动.我已经将所有脚本权限调整为775,并确保root拥有它们.

任何帮助都会非常有帮助.

做“消费者&”将简单地介绍任务并继续.如果拥有的shell终止,它将终止任何后台任务.您说该脚本在命令行上运行,但是如果您注销,您的守护程序将无法生存?

你想用start-stop-daemon之类的东西启动你的守护进程.

编辑:实际上,在再次阅读你的文字时,我不确定消费者是否是一个守护进程?如果你只想在启动时运行一些代码(例如清理房间),你可以在/etc/rc.local中写一行代码.

如果脚本需要很长时间才能运行,您可能想要否定它.即:

  1. consumer &
  2. disown %1

该脚本现在将在shell终止后继续存在.请注意,如果shell输出文本,它将保留相同的tty,这可能会导致问题,具体取决于它拥有的shell终止后发生的情况.

猜你在找的Ubuntu相关文章