张泽天

张泽天的博客

他的个人主页  他的博客

How to start an application automatically on boot

张泽天  2018年04月26日 星期四 15:18 | 683次浏览 | 0条评论

Linux tips: Kernel 2.3.26

Note: In the example script below just add a call to the "start)" section to actually launch your program. You can test the script's functionality without rebooting the system: call it with the full path and giving it a parameter of "start" or "stop"

Here goes:

Debian uses a Sys-V like init system for executing commands when the system runlevel changes - for example at bootup and shutdown time.

If you wish to add a new service to start when the machine boots you should add the necessary script to the directory /etc/init.d/. Many of the scripts already present in that directory will give you an example of the kind of things that you can do.

Here's a very simple script which is divided into two parts, code which always runs, and code which runs when called with "start" or "stop".

  1. #! /bin/sh
  2. # /etc/init.d/blah
  3. #
  4.  
  5. # Some things that run always
  6. touch /var/lock/blah
  7.  
  8. # Carry out specific functions when asked to by the system
  9. case "$1" in
  10. start)
  11. echo "Starting script blah "
  12. echo "Could do more here"
  13. ;;
  14. stop)
  15. echo "Stopping script blah"
  16. echo "Could do more here"
  17. ;;
  18. *)
  19. echo "Usage: /etc/init.d/blah {start|stop}"
  20. exit 1
  21. ;;
  22. esac
  23.  
  24. exit 0
  25.  

Once you've saved your file into the correct location make sure that it's executable by running "chmod 755 /etc/init.d/blah".

Then you need to add the appropriate symbolic links to cause the script to be executed when the system goes down, or comes up.

The simplest way of doing this is to use the Debian-specific command update-rc.d:

  1. root@skx:~# update-rc.d blah defaults
  2. Adding system startup for /etc/init.d/blah ...
  3. /etc/rc0.d/K20blah -> ../init.d/blah
  4. /etc/rc1.d/K20blah -> ../init.d/blah
  5. /etc/rc6.d/K20blah -> ../init.d/blah
  6. /etc/rc2.d/S20blah -> ../init.d/blah
  7. /etc/rc3.d/S20blah -> ../init.d/blah
  8. /etc/rc4.d/S20blah -> ../init.d/blah
  9. /etc/rc5.d/S20blah -> ../init.d/blah

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号